Re: For loop problem in mathematica

• To: mathgroup at smc.vnet.net
• Subject: [mg90205] Re: For loop problem in mathematica
• From: Bob F <deepyogurt at gmail.com>
• Date: Wed, 2 Jul 2008 05:30:30 -0400 (EDT)
• References: <g4d2qf\$emo\$1@smc.vnet.net>

```On Jul 1, 5:01 am, PhysNova <skhoshbin... at gmail.com> wrote:
> Hi,
> i wrote a simple program of evaluating Pi number in M6 to test cpu
> computation timing, to do this a simple for loop
>
> was used:
>
> x=0;For[i = 1, i < 10,000,000, i++, x = x + 1/(i^2)];N[Sqrt(6*x),25=
]//
> Timing
>
> the result was catastrophe! it take few minuates. but i first expect
> to do this very simple job in few
>
> seconds.computation time is just satisfactory up to 100000 cycle.
>
> could anyone interperet this falut?
> our get an idea to improve the result?
>
> Tanks

I think you have a couple of typo's. The Sqrt(6*x) should be
Sqrt[6*x], and the comma's in the 10,000,000 should not be there
arguments to the function than is allowed. Also the //Timing you have
will only be for the Sqrt[] function, not the loop. Is that what you
wanted? Try Timing[...] I am guessing that you were just typing
something in rather than pasting a Mathematica expression.

This is a little more than the built in way to compute Pi -- compare
this to N[Pi, 25]//Timing which only took about 0.000032 seconds on my
computer.

On my system doing it your way for 100000 (one-hundred thousand)
iterations took about 15.2 seconds and it's square root took an
were about 1379.1 and 34.5 seconds -- I would guess the 10000000 (ten
million) would take about 100 times longe which was a bit longer than
I was willing to wait ;)  Am curious why this takes so long -- anyone
got any ideas?

Try using the Mathematica Benchmark program to get an idea of your
Mathematica/System Information/Benchmark with MathematicaMark" on a
Mac or "Help/About Mathematica/System Information/Benchmark with
MathematicaMark" on a Windows system - this runs 15 different tests
systems (Windows, Linux and Mac systems with various CPU's).

A very efficient and fast converging algorithm for computing Pi is the
Chudnovsky algorithm (see Wikipedia article at
http://en.wikipedia.org/wiki/Chudnovsky_algorithm -- for more
details). Mathematica itself uses a Chudnovsky algorithm, but not sure
it it's the same as the Wikipedia or another one? I did this in
Mathematica and since it converges so fast only did 1 terms of the sum
and was within -3.08x10^-28 which took only .000303 seconds and 5
terms which took .000576 seconds and was within -3.6x10^-85, and 100
terms took 0.015829 seconds and was within 5.05x10^-1433 so you can
see how much faster converging and better this is ...

Here is the Chudnovsky algorithm in Mathematica:

Timing[\[Pi] -
N[1/(12*Sum[((-1)^k *
Factorial[6*k] * (13591409 + 545140134 * k)) / (Factorial[
3 * k] * Factorial[k]^3 * 640320 ^(3*k + 3/2)), {k, 0,
5}]), 1000]]

Have fun...

-Bob

```

• Prev by Date: Re: For loop problem in mathematica
• Next by Date: Re: Possible Bug in Mathematica 6
• Previous by thread: Re: For loop problem in mathematica
• Next by thread: Re: For loop problem in mathematica