I try to understand recursion and caching much better, and yet I make interesting progress (sometimes I'm a little slow). I have slight problems understanding this code:
# Fibonacci recursive with result storage class FibonacciStorage: _storage = { 0:1, 1:1 } @staticmethod def _fib(number): try: # is this already calculated return FibonacciStorage._storage[number] #searches dict, and if value exists then return it except KeyError: result = FibonacciStorage._fib(number-1) + FibonacciStorage._fib(number-2) #this is the actual Fibonacci Formula FibonacciStorage._storage[number] = result #adds result to dictionary #print FibonacciStorage._storage #this shows the storage list growing with each iteration. return result @staticmethod def fib(number): #first function, it has two asserts to basically make sure number is whole/positive and if its okay passes it to _fib(where the real work is done) # only do the assert statements once assert(isinstance(number,int)),"Needs a whole number" assert(number>0),"Needs a positive whole number" return FibonacciStorage._fib(number) # use a more readable name getFib = FibonacciStorage.fib print getFib(50)
I found this code on the net and tried to comment out each line to understand what it actually does. I don’t understand how this recursion is, it executes a code loop until the correct result is specified, but I don’t see how to call it myself.
At first, I thought it was a line: result = FibonacciStorage._fib(number-1) + FibonacciStorage._fib(number-2) , but I'm confused because my print FibonacciStorage._storage line print FibonacciStorage._storage shows that the cache storage is growing and growing but the line below it is a return function. I suspect I don’t understand how the return result actually somehow starts the recursion again.
I'm also not sure how the storage dictionary makes this program so fast. My simple fiber sequence recursions take a lot of time (even when I save the data), but in this case, if you execute print getFib(200) its instant. How does caching do this so fast?
So to summarize, two questions:
1) How does recursion work? Should my print statement access each cycle?
2) How does caching speed it up over other sequences of fibers? Is the class structure or @staticmethod excellent? For example, this seems instantaneous, while those at http://en.literateprograms.org/Fibonacci_numbers_(Python ) have a slight delay.
3) Maybe, also because of the interval, can the alpha-type fibnocci scale (split and work in parallel) only because of interference or is limited to one process?
Any insight would be helpful.