Well let's try and derive the sequence, which is also known as a multi-nacci sequence of order k. See this paper for some more interesting tidbits.
The basic rule is the same: it takes 1 month to for a pair to mature
Therefore at:
Month 0: 1 pair exists (young)
Month 1: 1 pair exists (adult)
Month 2: 1 pair exists (adult) + k pairs exist (young) = 1 + k total
Month 3: 1 + k pairs exist (adult) + k pairs exist (young) = 1 + 2k total
Month 4: 1 + 2k pairs exist (adult) + k (1 + k) pairs exist (young) = 1 + k(3 + k) total
Month 5: 1 + 3k + k^2 pairs exist (adult) + k (1 + 2k) pairs exist (young) = 1 + k(4 + 3k) total
and so on...
Notice that with each month, the total number of rabbit pairs is k * number of adults (which are two generations old) + number of young pairs (which are 1 generation old). Therefore the amount of rabbits at generation n is k * number of rabbits at generation n - 2 (as these are now all adults) + number of rabbits at generation n - 1
Mathematically: f(0) = 1, f(1) = 1, f(n) = f(n-1) + k * f(n-2).
Code:
def fibnum(n, k):
if n < 0:
raise ValueError("n must be a positive value")
if n is 0 or n is 1:
return 1
else:
return (fibnum(n-1, k) + k * fibnum(n-2, k))
Notes:
This code is 0-based: so 5th month is n=4 and so on.
WARNING: this sequence grows ridiculously fast with larger k (so calculating fibnum(100, 100) may take a while). Memoization is advised to provide a significant speedup for increasing n.
Update: Changed code to accept k as a parameter, and cleaned up my code formatting a bit. Added reference to multinacci sequence
f(0) = f(1) = k, andf(n+2) = ((n+2)choose k) * (f(n+1) + f(n))?