Skip to content

Commit

Permalink
Rabbits and Recurrence Relations
Browse files Browse the repository at this point in the history
  • Loading branch information
jdp committed Mar 30, 2013
1 parent 026c51e commit 09626e4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Empty file added fib/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions fib/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""
http://rosalind.info/problems/fib/
Given: Positive integers n≤40 and k≤5.
Return: The total number of rabbit pairs that will be present after n months if
each pair of reproduction-age rabbits produces a litter of k rabbit pairs in
each generation (instead of only 1 pair).
>>> problem(5, 3)
19
"""

import itertools
import os


def problem(n, k):
def rabbits(k):
xs = [1, 1]
for x in xs:
yield x
while True:
xs.append(xs[-1] + xs[-2] * k)
# print xs
yield xs[-1]
return next(itertools.islice(rabbits(k), n - 1, None), None)

if __name__ == '__main__':
import doctest
doctest.testmod()

dataset = open(os.path.dirname(__file__) + "/rosalind_fib.txt").read()
n, k = map(int, dataset.strip().split())
print n, k, problem(n, k)
1 change: 1 addition & 0 deletions fib/rosalind_fib.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
34 2

0 comments on commit 09626e4

Please sign in to comment.