HP Forums
Fibonacci sequence by recursive algorithm fail - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Fibonacci sequence by recursive algorithm fail (/thread-3684.html)

Pages: 1 2

RE: Fibonacci sequence by recursive algorithm fail - MikeOShea - 10-04-2015 05:33 AM

Tail recursive version. Runs quick.
Example Run:

if N>0 then
return fibotail(J,I+J,N-1);
return J;

return fibotail(0,1,N);

RE: Fibonacci sequence by recursive algorithm fail - brickviking - 10-04-2015 09:10 PM

(04-21-2015 05:13 AM)Gerald H Wrote:  
(04-20-2015 11:45 PM)Bill_G Wrote:  TIM and compsystems:

The program runs fine using RETURN (i.e., in capitals).


Font type differentiation of names is a trap that catches all practitioners.

I must have got lucky, I typed all keywords in capitals because of previous experience with picky parsers that weren't case-insensitive. So far I haven't been bitten yet, but no doubt I'll have it happen some day.

(Post 38)

RE: Fibonacci sequence by recursive algorithm fail - cyrille de brébisson - 10-05-2015 06:03 AM


The cas evaluator is recursive.
ie, each recursive function call that you make in a CAS program does result in a stack layer in the native program stack. And they add up quickly. And there is no way to control them (ie, know that you are running out of RAM). Your native program (which executes your CAS program) will crash, fast if the native program stack is small. This is why the CAS limits recursive user calls.

The Home evaluator, on the other hand is NON recursive. It handles evaluated programs in a while loop and handles user program recursivity using a manually managed stack. This results in a much more complex chunk of code, and might even be slower, BUT, it takes less RAM AND it allows the controled evaluation of deep recursive programs. Even low memory detection and proper error reporting.

Bernard's XCAS, I believe does have a non recursive evaluator, BUT I am not sure if it is implemented on Prime and even less sure of how to call it.


RE: Fibonacci sequence by recursive algorithm fail - parisse - 10-05-2015 11:23 AM

The non recursive eval activates itself automatically on system where PTHREAD_T is enabled, when the stack is about to be exhausted. Otherwise you need specific code, there is currently no specific code for the Prime but it could probably be added (like for RTOS_THREADX in gen.cc in gen::in_eval).