You are not logged in.

- Topics: Active | Unanswered

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.

'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

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

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)*

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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline

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?

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline

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)*

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline

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?

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline

Only stefy's code is correct

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline

```
(%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?

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline

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

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

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline

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)*

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline

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

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline

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.

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline

Hmm, you are correct. Python 2.7.3 gave a MemoryError

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline

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

'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

**bobbym****Administrator**- From: Bumpkinland
- Registered: 2009-04-12
- Posts: 105,272

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.****No great discovery was ever made without a bold guess. **

Offline