MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: For loop problem in mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg90237] Re: [mg90163] For loop problem in mathematica
  • From: DrMajorBob <drmajorbob at att.net>
  • Date: Thu, 3 Jul 2008 06:10:22 -0400 (EDT)
  • References: <11203311.1214991822381.JavaMail.root@m08>
  • Reply-to: drmajorbob at longhorns.com

I see nothing "catastrophic" about a 10-million-long For taking  
considerable time to complete.

(x = 0; For[i = 1, i < 10000000, i++, x = N[x + 1/(i^2), 53]];
   N[Sqrt[6*x], 25]) // Timing

{67.2293, 3.141592558096821157357751}

(x = Sum[1/i^2, {i, 1, n}];
   N[Sqrt[6 x /. n -> Infinity], 25]) // Timing

{0.004447, 3.141592653589793238462643}

N[Pi, 25] // Timing

{0.000024, 3.141592653589793238462643}

As written, your code created Rational numbers with very large  
denominators. For instance:

(x = 0; For[i = 1, i < 100, i++, x = x + 1/(i^2)];
   N[Sqrt[6*x], 25]) // Timing

{0.002248, 3.131980747244362801857815}

N@Log[10, #] &@Through[{Numerator, Denominator}@x]

{81.9002, 81.6867}

or

(x = 0; For[i = 1, i < 1000, i++, x = x + 1/(i^2)];
   N[Sqrt[6*x], 25]) // Timing

{0.015289, 3.140637100985938301795880}

N@Log[10, #] &@Through[{Numerator, Denominator}@x]

{865.922, 865.706}

and

(x = 0; For[i = 1, i < 10000, i++, x = x + 1/(i^2)];
   N[Sqrt[6*x], 25]) // Timing

{0.211607, 3.141497154397622340592830}

N@Log[10, #] &@Through[{Numerator, Denominator}@x]

{8692.74, 8692.52}

Notice that almost 9 thousand digit in the denominator isn't enough to get  
25-digit accuracy in Pi.

Bobby

On Tue, 01 Jul 2008 05:59:02 -0500, PhysNova <skhoshbinfar 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
>
>



-- 
DrMajorBob at longhorns.com


  • Prev by Date: Re: Re: Printing, WYSIWYG, and Window Magnification?
  • 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