Wednesday 29 June 2016

Mortal Fibonacci Rabbits

In this Rosalind problem we are revisiting the Fibonacci Rabbits. This time the bunnies sadly aren't immortal and will die after m months. Apart from that they behave as in the previous problem but they only produce one pair of offspring per month in adulthood. This leaves us with two different recurrence relations to calculate the amounts of rabbits present in any given month n. The months before any rabbits start dying off, i.e. when n  m can be described by the recurrence relation of the Fibonacci numbers. The months after this, i.e. when n > m, can be described by the following formula:

Fn = Fn-1 + Fn-2 - Fn-(m+1)

To calculate the number of rabbits after 96 months, when the rabbits are dying after 17 months, I wrote the following program:

n = 96                                                                         
m = 17                                                                         
bunnies = [1, 1]                                                               
months = 2                                                                     
while months < n:                                                              
    if months < m:                                                             
        bunnies.append(bunnies[-2] + bunnies[-1])                              
    elif months == m or count == m + 1:                                        
        bunnies.append(bunnies[-2] + bunnies[-1] - 1)                          
    else:                                                                      
        bunnies.append(bunnies[-2] + bunnies[-1] - bunnies[-(                  
            m + 1)])                                                           
    months += 1                                                                
print(bunnies[-1])                                                             

So, care to take a guess what the result after 96 months was? 51159459138167757395. Pairs...

5 comments:

  1. Hi! I love your blog!

    I've been working through Rosalind as a means to teach myself R, and I was completely stuck on this problem!

    Turns out R has a restriction on the size of numbers it can handle, which I didn't realize until I found your blog post, and tested my code against the example you had, and found that every time I even tried to get R to store "51159459138167757395" as an integer, it would spit back "51159459138167758858".

    I ended up using the gmp library which has a bigz class that lets you store real big numbers, but without having you as a sanity check, I'm not sure how long it would have taken me to realize what was happening.

    Congrats on finding employment!

    ReplyDelete
  2. It looks as if this person may have used your code without attributing it to you: https://cenkcelik.info/2020/04/27/mortal-fibonacci-rabbits/

    ReplyDelete
  3. Also, you never initialized the "count" variable (just wanted to point that out in case you can update this blog's code).

    ReplyDelete
    Replies
    1. Count doesn't seem to do anything. It doesn't get iterated and works fine without it and its conditional.

      Delete
  4. Hi there. I was wondering how to derive a formula, could you give me a hint?

    ReplyDelete