Example 20: Wolf and Rabbit Population

Learn how to calculate the population of wolves and rabbits.

Problem

Consider an ecological simulation of wolf and rabbit populations. Rabbits eat grass while wolves eat rabbits. There is plenty of grass, so wolves are the only obstacle to the rabbit population increase. The wolf population increases with the population of rabbits. The following formulae can express the day-by-day changes in the rabbit population R and the wolf population W:

R(tomorrow)=(1+a)R(today)cR(today)W(today)R(tomorrow) = (1 + a)*R(today) - c*R(today)*W(today)

W(tomorrow)=(1b)W(today)+cdR(today).W(today)W(tomorrow) = (1 - b)*W(today) + c*d*R(today).W(today)

a = 0.01; It is a fractional increase in rabbit population without threat from wolves (0.01 means 1 % increase).

b = 0.005; It is a fractional decrease in the wolf population without a rabbit to eat.

c = 0.00001; It is the likelihood that a wolf will encounter and eat a rabbit.

d = 0.01; It is a fractional increase in wolf population attributed to a devoured rabbit.

Assume that initially, there are 10,000 rabbits and 2000 wolves. Write a program to calculate the populations of rabbits and wolves over a 100 day period.

Have the program print the populations every 25 days.

Example

Input(rabbits, wolves) Output
10000, 2000 7958, 1800 6654, 1602 5832, 1427 5337, 1266

Note: Each pair of values represents the rabbit and wolf population after every 25 days period.

Try it yourself

Try to solve this question on your own in the code widget below. If you get stuck, you can always refer to the solution provided.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.