462,632 views
44 votes
44 votes
The nth Fibonacci number Fn is defined as follows: F0 = 1, F1 = 1 and Fn = Fn−1 + Fn−2 for n > 1.

In other words, each number is the sum of the two previous numbers in the sequence. Thus the first several Fibonacci numbers are 1, 1, 2, 3, 5, and 8. Interestingly, certain population growth rates are characterized by the Fibonacci numbers. If a population has no deaths, then the series gives the size of the poulation after each time period.
Assume that a population of green crud grows at a rate described by the Fibonacci numbers and has a time period of 5 days. Hence, if a green crud population starts out as 10 pounds of crud, then after 5 days, there is still 10 pounds of crud; in 10 days, there is 20 pounds of crud; in 15 days, 30 pounds of crud; in 20 days, 50 pounds of crud, and so on.

Write a program that takes both the initial size of a green crud population (in pounds) and some number of days as input from the keyboard, and computes from that information the size of the population (in pounds) after the specified number of days. Assume that the population size is the same for four days and then increases every fifth day. The program must allow the user to repeat this calculation as long as desired.
Please note that zero is a valid number of days for the crud to grow in which case it would remain at its initial value.
You should make good use of functions to make your code easy to read. Please use at least one user-defined function (besides the clearKeyboardBuffer function) to write your program.

basically I've done all the steps required except the equation in how to get the final population after a certain period of time (days). if someone would help me with this, I'll really appreciate it.

User Neva
by
2.8k points

1 Answer

11 votes
11 votes

In Python, it can be expressed as follows. Using the recursive function type, we find the sum of the previous term and the sum of the two previous terms.

Python:

x=int(input("Initial size: "))

y=int(input("Enter days: "))

mod=int(y/5)-1

def calc(n):

gen_term = [x,2*x]

for i in range(2, n+1):

gen_term.append(gen_term[i-1] + gen_term[i-2])

return gen_term[n]

if(mod==0):

print("After",y,"days, the population is",x)

else:

print("After",y,"days, the population is",calc(mod))

The nth Fibonacci number Fn is defined as follows: F0 = 1, F1 = 1 and Fn = Fn−1 + Fn-example-1
User Pryda
by
3.2k points