Skip to content

Instantly share code, notes, and snippets.

@einarwh
Created December 4, 2011 20:52
Show Gist options
  • Select an option

  • Save einarwh/1431241 to your computer and use it in GitHub Desktop.

Select an option

Save einarwh/1431241 to your computer and use it in GitHub Desktop.
Calculating the number of offspring for field voles.
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