[英]Huge Fibonacci Number
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.
我的示例输入是5949。应该在不到5秒的时间内计算出输出。
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
但是我在数组上收到错误:
IndexError: list index out of range
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
在添加新元素时也请使用append
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))
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]
. 您需要初始化数组的前两个元素:当
i=2
,行F[i]=F[i-1]+F[i-2]
实际上是F[2]=F[1]+F[0]
。 But F[1]
and F[0]
don't exist: the array is empty! 但是
F[1]
和F[0]
不存在:数组为空!
As others have mentioned your error is due to attempting to access elements in the list that don't exist. 正如其他人提到的那样,您的错误是由于尝试访问列表中不存在的元素引起的。 A fresh Python list created using
[]
has no elements, you have to add elements to it before you can safely index into it. 使用
[]
创建的新Python列表没有元素,您必须先向其中添加元素,然后才能安全地对其进行索引。
As I mentioned in my comment you don't need to create a list here, unless you want to keep a table of Fibonacci numbers that can be accessed randomly. 正如我在评论中提到的那样,您无需在此处创建列表,除非您希望保留一个可随机访问的斐波纳契数表。
To calculate single Fibonacci numbers Ibrahim's matrix multiplication algorithm is very fast, but it's not really necessary for calculating F(5949). 要计算单个斐波那契数,易卜拉欣的矩阵乘法算法非常快,但实际上并不需要计算F(5949)。 A simple
for
loop can do that in less than 0.06 seconds on my old 2GHz machine. 在我的旧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))
output 产量
0 0
1 1
2 1
3 2
4 3
5 5
If you are doing this on Python 2 replace range
in fib
by xrange
to save memory. 如果在Python 2上执行此操作,请用
xrange
替换fib
的range
以节省内存。
You're getting an IndexError
because you create an empty array, and in the next step you try to access its last two elements. 因为创建一个空数组,所以得到一个
IndexError
,然后在下一步中尝试访问它的最后两个元素。 You must initialize it with (at least) two elements. 您必须使用(至少)两个元素对其进行初始化。
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.
它使用矩阵乘法并在O(log N)时间内完成。 Using this approach, you can find Fibonacci of 5949 in milliseconds.
使用这种方法,您可以找到5949的斐波纳契数(以毫秒为单位)。
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.