Message Boards Message Boards

0
|
21947 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Why does thos code generate a recursion limit error?

Posted 9 years ago

It evaluates the function correctly. (Why would the recursion level exceed 30?) Generates a red box of error information.

Thanks for helping!

Clear[f];
f[0] = 1000000;
f[x_] := f[x] = f[x - 1]*1.05 - 41000;

f[10]
Out[143]= 1.1132*10^6

In[144]:= f[30]
Out[144]= 1.59795*10^6
POSTED BY: Dirk Cotton
2 Replies
Posted 9 years ago

Clearing the session with Quit [ ] and restarting made the problem go away. A fluke I can't explain. But all that ends well. . .

POSTED BY: Updating Name

The definition is recursive, f[30] evaluates here (Mathematica 10.1, Window 7 64 Bit Home Premium) without messages, but in general a big enough argument invokes the reclim message, if the $RecursionLimit is not set to Infinity (then RAM tends to become short)

In[1]:= (* comm490447 *)
Clear[f];
f[0] = 1000000;
f[x_Integer /; x > 0] := f[x] = f[x - 1]*1.05 - 41000

In[4]:= f /@ Table[o + 10^o, {o, 0, 8}]
During evaluation of In[4]:= $RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of 8983>0. >>
During evaluation of In[4]:= $RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of f[8983]. >>
Out[4]= {1.009*10^6, 1.12786*10^6, 2.69164*10^7, 3.2221*10^26, Hold[f[8983]], f[100005], f[1000006], f[10000007], f[100000008]}

(* this quits the kernel because of RAM exhaustion *)
Block[{$RecursionLimit = 10^8 + 9}, f /@ Table[o + 10^o, {o, 0, 8}]]

In[5]:= Block[{$RecursionLimit = 10^4 + 5}, f /@ Table[o + 10^o, {o, 0, 4}]]
Out[5]= {1.009*10^6, 1.12786*10^6, 2.69164*10^7, 3.2221*10^26, 1.7101*10^217}

In[6]:= $RecursionLimit
Out[6]= 1024
POSTED BY: Udo Krause
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract