I just started learning algorithms and I am stuck with the problem of finding a huge Fibonacci number. My example input is 5949. The output should be calculated in less than 5 seconds.
Here is my attempt:
def calc_fib(n):
if n < 0:
print ("Error. Bad input")
elif n <= 2:
return 1
else:
F = []
for i in range (2,n):
F[i] = F[i-1]+F[i-2]
return F[n]
n = int(input())
print(calc_fib(n))
But I get an error on line with arrays: IndexError: list index out of range
答案 0 :(得分:2)
you created a empty list and are indexing position in the list which do not exist. Also use append as you are adding new elements
def calc_fib(n):
if n < 0:
print ("Error. Bad input")
elif n <= 2:
return 1
else:
F = [0,1] # <- change
for i in range (2,n):
F.append(F[i-1]+F[i-2]) # <- change
return F[n]
n = int(input())
print(calc_fib(n))
答案 1 :(得分:1)
You need to initialize the first two elements of your array: when i=2
, the line F[i]=F[i-1]+F[i-2]
is really F[2]=F[1]+F[0]
. But F[1]
and F[0]
don't exist: the array is empty!
答案 2 :(得分:1)
正如其他人所说,您的错误是由于尝试访问列表中不存在的元素。使用[]
创建的新的Python列表没有元素,您必须先添加元素才能安全地索引它。
正如我在评论中提到的,你不需要在这里创建一个列表,除非你想保留一张可以随机访问的Fibonacci数字表。
要计算单个Fibonacci数,Ibrahim的矩阵乘法算法非常快,但计算F(5949)并不是必需的。在我的旧2GHz机器上,一个简单的for
循环可以在不到0.06秒内完成。
from __future__ import print_function
def fib(n):
a, b = 0, 1
for i in range(n):
a, b = a + b, a
return a
# Test
for i in range(6):
print(i, fib(i))
<强>输出强>
0 0
1 1
2 1
3 2
4 3
5 5
如果您在Python 2上执行此操作,请将range
中的fib
替换为xrange
以节省内存。
答案 3 :(得分:0)
You're getting an IndexError
because you create an empty array, and in the next step you try to access its last two elements. You must initialize it with (at least) two elements.
答案 4 :(得分:0)
There are already some answers here explaining what's wrong with your approach. However, if you're looking for an alternative idea, here's a really fast approach to find fibonacci numbers. It uses matrix multiplication and completes in O(log N) time. Using this approach, you can find Fibonacci of 5949 in milliseconds.
def matrix_mul(A, B):
return ([A[0][0] * B[0][0] + A[0][1] * B[1][0],
A[0][0] * B[0][1] + A[0][1] * B[1][1]],
[A[1][0] * B[0][0] + A[1][1] * B[1][0],
A[1][0] * B[0][1] + A[1][1] * B[1][1]])
def matrix_exp(A, e):
if not e:
return [[1,0],[0,1]]
elif e % 2:
return matrix_mul(A, matrix_exp(A, e-1))
else:
sq= matrix_exp(A, e//2)
return matrix_mul(sq, sq)
def fibo(n):
M = [[1,1],[1,0]]
return matrix_exp(M, n)[0][0]
Read this for understanding why this works