MathGroup Archive 2006

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

Search the Archive

Re: Re: $RecursionLimit Bug?


Hi Pratik,
This will consum too much memory.
One can make a compromise if the systems stack limit is known (can be
revealed with some experimentation). Store intermediate results only in
known points that will prevent the stack overflow
The following can serve as an example
f[0]=1;
f[x_]:=If[Mod[x,10000]==0,f[x]=x f[x-1],x f[x-1]]
$RecursionLimit=Infinity
so
Table[f[x],{x,10000,100000,10000}]
will store the results in the points determined by the Table iterator and
the definition of the function

This way of a forward calculation let youget results for f[10*^5 and above].

The payment is ofcourse with computation time (the condition is checked in
each steps) but this gives results in a very resonable time.
regards
yehuda

On 1/24/06, Pratik Desai <pdesai1 at umbc.edu> wrote:
>
> W. Craig Carter wrote:
>
> >Hello,
> >I am using Mathematica 5.2 on MacOS 10.4.4
> >
> >I was trying to construct a cannonical timing example by writing
> >a memoryless factorial function.
> >
> >If I do this (fresh kernel):
> >
> >$RecursionLimit = $IterationLimit = 10^6
> >fact[0] = 1;
> >fact[n_] := n fact[n - 1]
> >Timing[fact[10^6]]
> >?fact
> >This generates no output, and the ?fact input line becomes In[1], as
> >if the kernel died after Timing[fact[10^5]], but no messages are
> >generated.
> >
> >If I bury the recursion limit inside a Block, then I get
> >fact[0] = 1;
> >fact[n_] := n fact[n - 1]
> >Timing[Block[{$RecursionLimit = Infinity}, Return[fact[10^5]]]]
> >-----> $RecursionLimit::reclim : Recursion depth of 256 exceeded
> >?fact (* now is In[1] *)
> >-----? Symbol fact not found.
> >
> >
> >Is this a bug?  I couldn't find a list of known bugs on the
> >mathematica site....
> >
> >
> >Thanks, WCC
> >
> >
> >
> >W. Craig Carter
> >Lord Foundation Professor of Materials Science and Engineering
> >MIT, Dept. of Materials Science and Engineering 13-5018  77 Massachusetts
> Ave, Cambridge, MA 02139-4307 USA
> >617-253-6048  ccarter at mit.edu http://pruffle.mit.edu/~ccarter
> http://pruffle.mit.edu/~ccarter/FAQS/
> http://pruffle.mit.edu/~ccarter/I_do_not_use_microsoft.html
> >
> >
> >
> There is an example similar to this in the Mathematic Book in section
> 2.5.9
> 2.5.9 Functions That Remember Values They Have Found
> Here is one that I modified a bit with RecursionLimit set to infinity
>
> In[27]:=
> Clear[f,x]
> $RecursionLimit=Infinity
> f[x_]:= f[x]=x*f[x- 1]
> f[0]=f[1]=1
> Timing[f[10^4]]//Short
>
> Out[28]=
> ∞
>
> Out[30]=
> 1
>
> Out[31]//Short=
> {0.23 Second,2846259\[LeftSkeleton]35645\[RightSkeleton]00000000}
>
> Hope this helps
>
> Pratik
>
>


  • Prev by Date: Re: Re: show workings
  • Next by Date: Re: Re: $RecursionLimit Bug?
  • Previous by thread: Re: $RecursionLimit Bug?
  • Next by thread: Re: Re: $RecursionLimit Bug?