-3

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.

Have done this far, but still getting error. Where does it need to be fixed?

fib = [0,1,1]
result = 0
while result <=n:
    result=fib[-1]+fib[-2]
    if result <=n and result>=m:
        fib.append(result)
Hee Ra Choi
  • 17
  • 1
  • 5
  • Before you ask the question please do a quick search about the topic at least on google and StackOverflow. This question asked and answered well in [stack](https://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python) itself – Nithin Varghese Oct 30 '17 at 03:14
  • 1
    Possible duplicate of [How to write the Fibonacci Sequence in Python](https://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python) – Scransom Oct 30 '17 at 03:40

3 Answers3

1
# create a fibonacci series from 0 to n
f = [0,1]
for i in range(1, n+1):
    x = f[i]+f[i-1]
    if x > n:
       break
    else:
        f.append(x)
# copy only those numbers of series that are in range m to n
fib = []
for i in range(0, len(f)):
    if f[i] >= m and f[i] <= n:
        fib.append(f[i])
0

You could use the code below.

fib = [0,1,1]
fibrange = [] # keeps your digits that are in  range m < n
result = 0
n = 80
m = 2
while result <=n:
    result =fib[-1]+fib[-2]
    fib.append(result)
    if result <=n and result>=m:
        fibrange.append(result)
print fibrange
Gumboy
  • 457
  • 4
  • 8
0

fib=[0,1]

i=0

while i <=n:

i=fib[-1]+fib[-2]

if i<=n:

    fib.append(i)
Ridhi
  • 105
  • 8