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
  • Subject: [mg90234] RE: [mg90163] For loop problem in mathematica
  • From: "Jose Luis Gomez" <jose.luis.gomez at>
  • Date: Thu, 3 Jul 2008 06:09:49 -0400 (EDT)
  • References: <>

PhysNova, I hope this is useful for you:

FIRST: It is Not a good idea to use a For loop in Mathematica, Mathematica
is functional and list-based, therefore a For loop will be very inefficient
in Mathematica. Instead of For loops, you should use functional programming
constructs, like Nest, NestWhile, Map, Apply, etc.

This document might be interesting for you: 

SECOND: It is Not the same to write 1/(i^2) and to write 1.0/(i^2.0)
In the first case Mathematica will use slow exact arithmetic, while in the
second case Mathematica will use fast approximate floating point numbers
(which is what most other programs do)


-----Mensaje original-----
De: PhysNova [mailto:skhoshbinfar at] 
Enviado el: Martes, 01 de Julio de 2008 05:59
Para: mathgroup at
Asunto: [mg90163] For loop problem in mathematica

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]//

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?


  • Prev by Date: Re: For loop problem in mathematica
  • Next by Date: Re: Re: Subscript Bug?
  • Previous by thread: Re: For loop problem in mathematica
  • Next by thread: Re: For loop problem in mathematica