184k views
0 votes
In the following sequence, each number (except the first two) is the sum of the previous two number: 0, 1, 1, 2, 3, 5, 8, 13, .... This sequence is known as the Fibonacci sequence. Given the positive integers m and n (with m < n) create a list consisting of the portion of the Fibonacci sequence greater than or equal to m and less than or equal to n. For example, if m is 3 and n is 6, then the list would be [3, 5] and if m is 2 and n is 20, then the list would be [2, 3, 5, 8, 13]. Associate the list with the variable fib.

1 Answer

3 votes

Answer:

Fibonacci series ranging from m to n where m<n assuming n=200 and m=100 .

Explanation:

#you have to produce a fibonacci series from 0 to n as follows:

k = [0,1]

fib=[]

n=200

m=100

for p in range(1, n+1):

y = k[p]+k[p-1]

if x > n:

break

else:

k.append(y)

# Now to single out those fibonacci series between m and n in a list #associated with the variable fib, do the following:

for p in range(0, len(k)): # p ranging from 0 to the last index of k

if k[p] >= m and k[p] <= n:

fib.append(k[p]) #append only those series within m and n to the

#variable fib

print fib #this will display the list created

User James Leonard
by
5.0k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.