Services & Resources / Wolfram Forums / MathGroup Archive
-----

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: [mg90188] Re: [mg90163] For loop problem in mathematica
  • From: Sseziwa Mukasa <mukasa at jeol.com>
  • Date: Wed, 2 Jul 2008 05:27:16 -0400 (EDT)
  • References: <200807011059.GAA14895@smc.vnet.net>

On Jul 1, 2008, at 6:59 AM, PhysNova 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?

You are using exact arithmetic, Mathematica will constantly try to  
preserve precision, and simplify the expression x + 1/i^2 which adds  
computational complexity.

> our get an idea to improve the result?

If you don't care about exact results, use approximate numbers:

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

Regards,

Ssezi


  • Prev by Date: Re: Possible Bug in Mathematica 6
  • Next by Date: Re: Draw two functions in a graph, one of them does also have a parameter.
  • Previous by thread: For loop problem in mathematica
  • Next by thread: Re: For loop problem in mathematica