在以下序列中,每个数字(前两个除外)是前两个数字的总和:0,1,1,2,3,5,8,13 ......这个序列称为Fibonacci序列。
给定正整数m和n(m 将列表与变量fib。 已经做到这一点,但仍然出现错误。
它需要在哪里修复?fib = [0,1,1]
result = 0
while result <=n:
result=fib[-1]+fib[-2]
if result <=n and result>=m:
fib.append(result)
答案 0 :(得分: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])
答案 1 :(得分:0)
您可以使用以下代码。
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
答案 2 :(得分:0)
fib = [0,1]
i = 0
而我<= n:
i=fib[-1]+fib[-2]
if i<=n:
fib.append(i)