MathGroup Archive 1995

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

Search the Archive

Re: How to write a loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg2549] Re: [mg2538] How to write a loop
  • From: Allan Hayes <hay%haystack at smc.vnet.net>
  • Date: Mon, 20 Nov 1995 01:13:19 -0500

Enrico C. Walker <ewalke1 at gl.umbc.edu>
in [mg2538] How to write a loop
writes:
> I need to raise a number to some large exponent - i.e. 245 ^ 500.
> I know that I may run into a problem by plugging it directly. so
> I'd like to find the result by using some sort of programming  
loop > such.
>
>     DO 250 times
>         begin
>              x = x * 245 ^ 2
>         end
>     PRINT X
>
>Can I do such programming in MATHEMATICA? Please advise.

Enrico:
In the following I have timed the computations for comparison,
and deleted the outputs from the email in the interest of brevity.
You can also use functions While and For in Mathematica.
Allan Hayes
hay at haystack.demon.co.uk


This does what you want in the way that you suggest
In[428]:=
(x = 1;
Do[x = x (245^2), {250}];
Print[x];
)//Timing
{0.3 Second, Null}
<print deleted

But Mathematica has no problem with the direct calculation. This is  
faster and the output is directly useable in other calculations,  
not dead print.

   (245 ^ 500)//Timing
      { 0.0166667 Second, <deleted answer> }

You could get the same, live, output from Do by omitting the Print  
command

   (x=1;
      Do[x = x (245^2), {250}];
    x
   )//Timing

      {0.166667 Second,<deleted answer>}

Mathematica has also the following way

   Nest[(# 245^2)&, 1, 250]//Timing

      {0.166667 Second,<deleted answer>}




  • Prev by Date: Problem: Plotting list of {InterpolatingFunction[]}
  • Next by Date: Bug: too subtle for me
  • Previous by thread: Re: How to write a loop
  • Next by thread: how to write a program loop