Fibonacci: How to think recursively

Baba raoulbia at gmail.com
Sat Aug 28 19:12:36 EDT 2010


Level: beginner

I would like to know how to approach the following Fibonacci problem:
How may rabbits do i have after n months?

I'm not looking for the code as i could Google that very easily. I'm
looking for a hint to put me on the right track to solve this myself
without looking it up.

my brainstorming so far brought me to a stand still as i can't seem to
imagine a recursive way to code this:

my attempted rough code:

def fibonacci(n):
    # base case:
        result = fibonacci (n-1) + fibonacci (n-2)
>> this will end up in a mess as it will create overlapping recursions

OR

def fibonacci(n):
    # base case:
        fibonacci(n+2) - fibonacci(n+1) - n = 0
>> this too would create overlapping recursions


How to go about this?

Thanks
Baba




More information about the Python-list mailing list