Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2013-11-16 01:18:54

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Teach us some Maxima!

Hi anonimnystefy;

Could you teach me some Maxima? I could not find any tutorial on the web that is appropiate.

Let us start with Project Euler 1 which is a pretty simple one and does not hurt the spirit of the Eulerians

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#2 2013-11-16 01:47:11

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Teach us some Maxima!

Hi Agnishom

makelist(lambda([x],if (mod(x,3)=0)or(mod(x,5)=0) then 1 else 0)(i),i,1,1000).makelist(i,i,1,1000);

If we should not include 1000, then put 999 instead of 1000 in both makelists.

Last edited by anonimnystefy (2013-11-16 01:49:33)


“Here lies the reader who will never open this book. He is forever dead.
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment
The knowledge of some things as a function of age is a delta function.

Offline

#3 2013-11-16 01:52:23

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

Hi;

Here is how to do it using Maxima as a CAS:


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#4 2013-11-16 01:58:01

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Teach us some Maxima!

anonimnystefy wrote:

Hi Agnishom

makelist(lambda([x],if (mod(x,3)=0)or(mod(x,5)=0) then 1 else 0)(i),i,1,1000).makelist(i,i,1,1000);

If we should not include 1000, then put 999 instead of 1000 in both makelists.

How does the code work? Could you explain more?

Thanks.

Hi bobbym;

If that is the usage of Maxima as a CAS then what about his implementation?


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#5 2013-11-16 02:00:56

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

His implementation is using it as a programming language. It is fine to use either way but you must learn both. Most of the time the CAS way will be quicker and use less memory. Also you will be able to mathematically prove your program will work.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#6 2013-11-16 02:09:21

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Teach us some Maxima!

bobbym wrote:

Hi;

Here is how to do it using Maxima as a CAS:

(%i1) sum(3*k, k, 1, 333) + sum(5*k, k, 1, 199) - sum(15*k, k, 1, 66);
(%o1)                               233168

Why am I getting a different answer?

Last edited by bobbym (Today 00:00:00)

Last edited by Agnishom (2013-11-16 02:53:46)


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#7 2013-11-16 02:11:02

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

Put 999 instead of 1000 as he said.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#8 2013-11-16 02:17:38

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Teach us some Maxima!

Still, they are not equal.

Since you said that a CAS and a standard programming language are very different, I would ask that the summation could also be done as in post 3 using any ordinary language. Why is the CAS special?


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#9 2013-11-16 02:18:45

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

First please check that answer at Euler.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#10 2013-11-16 02:37:37

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Teach us some Maxima!

Only stefy's code is correct


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#11 2013-11-16 02:38:23

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

What answer do you get?


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#12 2013-11-16 02:42:28

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Teach us some Maxima!

(%i7) makelist(lambda([x],if (mod(x,3)=0)or(mod(x,5)=0) then 1 else 0)(i),i,1,999).makelist(i,i,1,999);
(%o7) 233168

that.

Why is it important?


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#13 2013-11-16 02:48:10

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

I have fixed the error, I missed that little "or." Everything is fine now.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#14 2013-11-16 02:51:17

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Teach us some Maxima!

Exactly what I had a doubt about. Look I fixed my post too!

Last edited by Agnishom (2013-11-16 02:54:37)


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#15 2013-11-16 02:55:27

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

But to show you the differences in the approaches supposing I said less than 100 000 000 000. Try the makelist way now.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#16 2013-11-16 03:00:57

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Teach us some Maxima!

Ran out of memory

(%i1) makelist(lambda([x],if (mod(x,3)=0)or(mod(x,5)=0) then 1 else 0)(i),i,1,99999999999).makelist(i,i,1,99999999999);
Maxima encountered a Lisp error:
 Error in PROGN [or a callee]: The storage for FIXNUM is exhausted.
Currently, 64754 pages are allocated.
Use ALLOCATE to expand the space.
Automatically continuing.
To enable the Lisp debugger set *debugger-hook* to nil.

Last edited by Agnishom (2013-11-16 03:03:02)


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#17 2013-11-16 03:07:05

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

That is right but doing it the CAS way uses no more memory and is just as fast even though the problem is 100 million times larger!


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#18 2013-11-16 03:25:39

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Teach us some Maxima!

It also requires a better understanding of math but we are going farther away from the main topic of the thread.

Moreover, that was not the answer to #8


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#19 2013-11-16 03:36:22

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

We are not going away. An ordinary language can not use the makelist idea for the larger problem a CAS can.

It also requires a better understanding of math

The CAS does the math for you!

Enter this into maxima

Sum (3 n, n, 1, a) + Sum (5 n, n, 1, b) - Sum (15 n, n, 1, c)

you will get this

(3 / 2 * a^(2)) + (3 / 2 * a) + (5 / 2 * b^(2)) + (5 / 2 * b) - (15 / 2 * c^(2)) - (15 / 2 * c)

Enter

a:=33333333333
b:=19999999999
c:=6666666666

rerun this

(3 / 2 * a^(2)) + (3 / 2 * a) + (5 / 2 * b^(2)) + (5 / 2 * b) - (15 / 2 * c^(2)) - (15 / 2 * c)

and you immediately get the answer

2333333333316666666668

no time and no memory!


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#20 2013-11-16 03:57:24

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Teach us some Maxima!

The CAS does the math for you!

No, only the computation.

We could write a routine for doing the sum in any ordinary language too.


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#21 2013-11-16 04:00:30

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

Nope, it does the math too. You can not write it in any other language. You can write loops but the math makes the loops go away!

Look at this line

(3 / 2 * a^(2)) + (3 / 2 * a) + (5 / 2 * b^(2)) + (5 / 2 * b) - (15 / 2 * c^(2)) - (15 / 2 * c)

that is a formula, all you have to do is plug in for any a,b or c. It took the three loops and turned them into a single formula.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#22 2013-11-16 04:07:29

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Teach us some Maxima!

Hmm, you are correct. Python 2.7.3 gave a MemoryError


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#23 2013-11-16 04:13:35

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

The secret is right in here:

Sum (3 n, n, 1, a) + Sum (5 n, n, 1, b) - Sum (15 n, n, 1, c) that represents in math talk 3 loops
that go from 1 to 33333333333  and from 1 to 19999999999 and from 1 to  6666666666. Maxima being a CAS and
knowing how to do a summation changed the 3 loops into 1 formula that is executed 1 time! 120 billion iterations has been changed into
1 iteration. This is why it is much faster and uses only a fraction of the memory.

A CAS combines programming, mathematics and computation. Sometimes you use only one of them, sometimes all three. No language can do that.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

#24 2013-11-16 04:27:55

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,974
Website

Re: Teach us some Maxima!

Just like you cannot do without Inform 7 in the world of IF, you cannot do without a CAS in math


'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
I'm not crazy, my mother had me tested.

Offline

#25 2013-11-16 04:37:01

bobbym
bumpkin
From: Bumpkinland
Registered: 2009-04-12
Posts: 109,606

Re: Teach us some Maxima!

They are used for doing ordinary programming tasks too.


In mathematics, you don't understand things. You just get used to them.
If it ain't broke, fix it until it is.
Always satisfy the Prime Directive of getting the right answer above all else.

Offline

Board footer

Powered by FluxBB