Last active
August 13, 2016 19:51
-
-
Save allgenesconsidered/ad24da305db625923e8f849f66d8a39a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fibBase(n,m): | |
| """ | |
| Calcultates the number of rabbit pairs alive at month n assuming | |
| rabbits live for m months. | |
| Input: Number of months n and rabbit lifespan m | |
| Output: Number of rabbit pairs alive at month n | |
| """ | |
| memo = {0:0,1:1} # dict of (month:# of pairs) | |
| return fibMortal(n,m,memo) | |
| def fibMortal(n, m, memo): | |
| if not n in memo: | |
| if(n <= m): | |
| memo[n] = fibMortal(n-1, m, memo) + fibMortal(n-2, m, memo) # assume normal fib | |
| elif (n == (m + 1)): | |
| memo[n] = fibMortal(n-1, m, memo) + fibMortal(n-2, m, memo) - 1 # first rabbit dies | |
| else: | |
| memo[n] = (fibMortal(n-1, m,memo) + fibMortal(n-2, m, memo)) - (fibMortal(n-(m + 1), m, memo)) # continuous death | |
| return memo[n] | |
| print fibBase(91,20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment