Created
December 4, 2011 20:52
-
-
Save einarwh/1431241 to your computer and use it in GitHub Desktop.
Calculating the number of offspring for field voles.
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
| public class Voles | |
| { | |
| private static int _daysBeforeFirst = 25; | |
| private static int _daysBetween = 20; | |
| private static Dictionary<int, long> _cache = | |
| new Dictionary<int, long>(); | |
| public static long F(int days) { | |
| if (!_cache.ContainsKey(days)) { | |
| _cache[days] = F0(days); | |
| } | |
| return _cache[days]; | |
| } | |
| private static long F0(int days) { | |
| int end = days - _daysBeforeFirst; | |
| if (end < 0) { | |
| return 1; | |
| } | |
| int start = end % _daysBetween; | |
| long count = 0; | |
| for (int d = start; d <= end; d += _daysBetween) { | |
| count += F(d) + 1; | |
| } | |
| return 1 + 4 * count; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment