MUD's rats reproduce fast! They reach sexual maturity in 35
days and give birth to 14
pups every 21
days. If you took one
newborn rat home with you, how many rats would you have after 98
days?
Such a riddle is encountered in MUD1 - probably the oldest game of "multi-user dungeon" type (a kind of text quest) which is still playable, particularly at british-legends.com.
Not regarding the answer to the original puzzle, let's capitalize on it, turning it into general programming problem (supposing these rats reproduce in "vegetative" manner, just like gremlins)...
Input data consist of 4
numbers:
M
is number of days for newborn rat to reach maturity (in other words, beget first offsprings)P
is breeding periodN
is how many children are born each timeD
is the observation time, after which we want to count all ratsFor example if we, given M=35
, P=21
, N=14
would observe them for D=70
days, it is easy to find that on day 35
we found ourselves
lucky owners of 15
rats instead of single one, situation worsens on day 56
when they become 29
in number but real feast comes on
day 70
when the 3-rd
generation comes, bringing total to 225
.
Answer is expected to contain a single value - total amount of rats at the last day of experiment.
Example
input:
35 21 14 98
answer:
645