Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: a better fibonacci subroutine

by herby1620 (Monk)
on Dec 15, 2006 at 21:28 UTC ( [id://590121]=note: print w/replies, xml ) Need Help??


in reply to a better fibonacci subroutine

You have two ways of getting the result. The iterative case (Haskell's) and the recursive case (yours). The Haskell method is very easy to follow, just write the values for $a and $b while it iterates. The recursive solution works as well, but (big but here!) it calls the 'fib' function greater than the fibonacci times for every number you wish to get (except for the first two cases). This number grows VERY quickly. It is similar to a fibonacci sequence, but with different starting points
Call      Number of calls   Calls
fib(1)    0                 Internal
fib(2)    0                 Internal
fib(3)    2                 fib(2), fib(1)
fib(4)    4                 fib(3), [fib(2), fib(1)], fib(2)
fib(5)    6                 fib(4)[4]... fib(3)[2]...
fib(6)   10                 fib(5)[6]... fib(4)[4]...
fib(7)   16                 fib(6)[10]... fib(5)[6]...
fib(8)   26                 fib(7)[16]... fib(6)[10]...
While this DOES demonstrate recursion, it can consume resources. Being a bit of an 'old school' type programmer who grew up with machines with limited resources using an iterative solution is perfered (to me!).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://590121]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-25 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found