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...
Hi! I love your blog!
ReplyDeleteI'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!
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/
ReplyDeleteAlso, you never initialized the "count" variable (just wanted to point that out in case you can update this blog's code).
ReplyDeleteCount doesn't seem to do anything. It doesn't get iterated and works fine without it and its conditional.
DeleteHi there. I was wondering how to derive a formula, could you give me a hint?
ReplyDelete