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 |
Fibonacci sequence by recursive algorithm fail - compsystems - 04-20-2015 06:42 PM The Fibonacci sequence by recursive algorithm fail in program type CAS, why? http://en.wikipedia.org/wiki/Fibonacci_number HPPrime CAS CODE Code:
fibo(16) → "Error: Bad argument count" HPPrime NUM CODE Code: EXPORT fibo(n) TI68K CODE Code:
other algorithms http://www.hpmuseum.org/forum/thread-3162.html?highlight=Fibonacci RE: Fibonacci sequence by recursive algorithm fail - Tim Wessman - 04-20-2015 07:57 PM The CAS is case sensitive. There is no function or keyword called "Return" Code:
RE: Fibonacci sequence by recursive algorithm fail - compsystems - 04-20-2015 09:26 PM catalog shows only one, in capitals RETURN, plus the code with tabulation fails, but left aligned says that syntax OK, but fails to run again. Who has run successfully? Please improve part of tabulation (compiler) RE: Fibonacci sequence by recursive algorithm fail - Didier Lachieze - 04-20-2015 09:58 PM (04-20-2015 09:26 PM)compsystems Wrote: Who has run successfully?I just entered it on my Prime and it's working fine with one addition: there is a missing ";" after the first "end" in your code. And I used the space key for the indentation and didn't get any error. RE: Fibonacci sequence by recursive algorithm fail - Bill_G - 04-20-2015 11:45 PM TIM and compsystems: The program runs fine using RETURN (i.e., in capitals). Be sure to put the ; as mentioned by Didier. Bill_G RE: Fibonacci sequence by recursive algorithm fail - Alberto Candel - 04-21-2015 12:25 AM I think that one other thing to take into account is that, for the CAS function, the recursive function calls setting (CAS settings, page 2, line 3) must be set to a number at least that of the parameter value n in fibo(n). Default setting seems to be 9, to fibo(10) returns "Running non recursive evaluator." RE: Fibonacci sequence by recursive algorithm fail - Gerald H - 04-21-2015 05:13 AM (04-20-2015 11:45 PM)Bill_G Wrote: TIM and compsystems: Font type differentiation of names is a trap that catches all practitioners. RE: Fibonacci sequence by recursive algorithm fail - Gerald H - 04-21-2015 05:49 AM (04-20-2015 07:57 PM)Tim Wessman Wrote: The CAS is case sensitive. There is no function or keyword called "Return" Font type differentiation of names is a trap that catches all practitioners. RE: Fibonacci sequence by recursive algorithm fail - DrD - 04-21-2015 01:57 PM I think a case could be made that a CAS program is not a good construct for this kind of algorithm. Personally, I think that CAS programs are better suited for CAS exclusive purposes (remaining within the CAS environment). I get that recursive activity is the point of this presentation, but the steps involved are better implemented in a Home program, rather than a CAS variable. The CAS approach, in this case, is a bit like building a pipeline to a beer brewery across town, rather than just using containers to get the desired beer home. Here is a simple means to the same end, which can be embellished as necessary, or if needed as a subroutine, all the while avoiding CAS restrictions : Code:
RE: Fibonacci sequence by recursive algorithm fail - compsystems - 04-21-2015 02:20 PM Sorry for my bad English I found the real problem, works well (CAS PROG) if everything is written in lowercase or all uppercase and, in single shift and does not work (if then else end → If Then Else End) HP TEAM must review the compiler (CAS PROG) for next versión of firmware, the following code is successfully compiled, but fails to run, you must verify that the commands are well written Code: #CAS fibo_cas(16) → "Error: Bad argument count" ? What affects capitalized If, the algorithm? ------- I found a serious problem, if two functions prog HOME or prog CAS, has the same name appears only version HOME. The only solution is to support working directories for next versión of firmware (DIR nameDir .... EndDir) do you agree? PROBLEM WITH TWO FOLLOWING CODES, try it yourself Code: #CAS Code: export fibo(n) --------------------- OK codes Code: #CAS fibo_cas(16) → 987.0 Code: export fibo_home(n) Or Code: export fibo_home2(n) (04-21-2015 01:57 PM)DrD Wrote: I think a case could be made that a CAS program is not a good construct for this kind of algorithm. Personally, I think that CAS programs are better suited for CAS exclusive purposes (remaining within the CAS environment). Just to use one type CAS program if the output is symbolic is obligatory, if only numerical output is optional For example the following code can not code as HOME program Code: #CAS a tip, if the return command have parentheses, returns the output is in exact format, otherwise the approx Code: #CAS RE: Fibonacci sequence by recursive algorithm fail - DrD - 04-21-2015 04:00 PM (04-21-2015 02:20 PM)compsystems Wrote:(04-21-2015 01:57 PM)DrD Wrote: I think a case could be made that a CAS program is not a good construct for this kind of algorithm. Personally, I think that CAS programs are better suited for CAS exclusive purposes (remaining within the CAS environment). For me it is not completely clear what you are trying to do. However, I synthesized your larger program into just this fragment, which (I think) is what you may be asserting, and it IS in HOME mode. The general idea is interaction with string / expression / number. You can accomplish the rest of your program with similar workflow: Code:
RE: Fibonacci sequence by recursive algorithm fail - Joe Horn - 04-22-2015 02:37 AM (04-21-2015 01:57 PM)DrD Wrote: I think a case could be made that a CAS program is not a good construct for this kind of algorithm. Personally, I think that CAS programs are better suited for CAS exclusive purposes (remaining within the CAS environment). I get that recursive activity is the point of this presentation, but the steps involved are better implemented in a Home program, rather than a CAS variable. The CAS approach, in this case, is a bit like building a pipeline to a beer brewery across town, rather than just using containers to get the desired beer home. Actually, CAS is REQUIRED for calculating Fibonacci numbers with more than 12 digits. CAS supports integers of almost unlimited length. Home is limited to a mere 12 digits. RE: Fibonacci sequence by recursive algorithm fail - Marcus von Cube - 04-22-2015 10:20 AM (04-21-2015 01:57 PM)DrD Wrote: Here is a simple means to the same end, which can be embellished as necessary, or if needed as a subroutine, all the while avoiding CAS restrictions : Fibonacci numbers and the factorial are different, aren't they? RE: Fibonacci sequence by recursive algorithm fail - DrD - 04-22-2015 12:41 PM Quite right. Code:
RE: Fibonacci sequence by recursive algorithm fail - compsystems - 04-23-2015 02:21 AM Code: #CAS nfibo_cas(28) → 317811 nfibo_cas(30) → BUSY → 832040 nfibo_cas(100) → running non recursive evaluator What means the above output? nfibo_cas(100) → ¨ERROR MEMORY¨ (ti68k calcs) RE: Fibonacci sequence by recursive algorithm fail - parisse - 04-23-2015 06:44 AM If you run a recursive program, at each recursive call, the environment is saved on a stack. But this stack can not grow dynamically, therefore if you want to do more embedded recursive calls, you must change the way evaluation is done (and save environment in the heap). RE: Fibonacci sequence by recursive algorithm fail - compsystems - 04-23-2015 02:09 PM but better display a message that says, ¨stack overflow¨ or something similar. I consider it a BUG that the program editor, says that the compilation was successful without checking the syntax of each command, each are sensitive to lowercase and uppercase, I took a long time to detect that had to write, If Then End as if then end or IF THEN END RE: Fibonacci sequence by recursive algorithm fail - parisse - 04-23-2015 06:58 PM (04-23-2015 02:09 PM)compsystems Wrote: but better display a message that says, ¨stack overflow¨ or something similar.Stack overflow would be misinterpreted as an error. RE: Fibonacci sequence by recursive algorithm fail - compsystems - 04-24-2015 08:37 PM ¨Runtime Error: Maximum recursion depth (stack) exceded¨ Who proposes another best output message? RE: Fibonacci sequence by recursive algorithm fail - parisse - 04-25-2015 06:12 AM No, because running non recursive evaluator is not a runtime error, your program will still run (it will run slower). You can't expect a recursive Fibonacci program to finish with n=100 (unless you have some remember table), because the time required is proportionnal to Fibonacci(100) that's about 1e20. |