[英]How to handle OverflowError: (34, 'Result too large') as a beginner?
I am a relative beginner in python and I saw some similar content I understood something but not completely.我是python的一个相对初学者,看到一些类似的内容,我明白了一些,但不完全明白。 I am a math student and I try to solve problems on project Euler.我是一名数学专业的学生,我尝试解决欧拉项目中的问题。 The question was finding the index of the first term in the Fibonacci sequence to contain 1000 digits.问题是找到包含 1000 位数字的斐波那契数列中第一项的索引。 I find the general formula for the Fibonacci sequence and wrote it in python as below.我找到了斐波那契数列的一般公式,并将其写在 python 中,如下所示。 It raises an error which is它引发了一个错误,即
Traceback (most recent call last):
File "C:\Users\baldi\Desktop\soru.py", line 6, in <module>
if int(log10(fib(i)))+1<1000:
File "C:\Users\baldi\Desktop\soru.py", line 4, in fib
return (((sqrt(5)+1)/sqrt(20))*((sqrt(5)+ 1)/2)**(n-1)+((sqrt(5)-1)/sqrt(20))*((-sqrt(5)+ 1)/2)**(n-1))
OverflowError: (34, 'Result too large')
and then I wanted to find after which I value my function doesn't respond.然后我想找到我认为我的 function 没有响应的值。
from math import sqrt, log10
a = sqrt(5)
b= sqrt(20)
def fib(n):
return (((a+1)/b)*((a+ 1)/2)**(n-1)+((a-1)/b)*((-a+1)/2)**(n-1))
for i in range(1,10**4):
if int(log10(fib(i)))+1<1000:
continue
else:
print(i)
break
it said the computer cannot compute after i=1476.它说计算机在 i=1476 之后无法计算。 I read similar articles, they said you could use the decimal library, etc. But as beginner, I either don't know how to use them or I cannot understand the notation to learn them.我看过类似的文章,他们说你可以使用十进制库等。但是作为初学者,我要么不知道如何使用它们,要么看不懂符号来学习它们。 Can you please help me and please can it be not too complex?你能帮我吗,请不要太复杂吗?
To find the index of the first term in the Fibonacci sequence to contain 1000 digits using Python, you can use the following approach:要使用 Python 找到斐波那契数列中第一项包含 1000 位数字的索引,您可以使用以下方法:
Calculate the next term in the Fibonacci sequence by adding a and b and storing the result in a new variable c. Set a to b and b to c. Increment index by 1. Return index after the while loop has been completed.通过将 a 和 b 相加并将结果存储在新变量 c 中计算斐波那契数列中的下一项。将 a 设置为 b,将 b 设置为 c。将索引递增 1。在 while 循环完成后返回索引。 Here is the code that implements this approach:下面是实现这种方法的代码:
def fibonacci_index(n):
a, b = 1, 1
index = 2
while len(str(a)) < n:
c = a + b
a, b = b, c
index += 1
return index
print(fibonacci_index(1000)) # prints 4782
The problem is your numbers are too large for python to handle.问题是您的数字太大,python 无法处理。 So what you want to do is所以你想要做的是
from decimal import Decimal
Then turn a
and b
into decimals:然后将a
和b
化为小数:
a = Decimal(sqrt(5))
b = Decimal(sqrt(20))
Now we also need to ensure the index i
is a Decimal.现在我们还需要确保索引i
是 Decimal。 However, if we only changed that we would run into an error when trying to use math.log10.但是,如果我们只更改它,我们将在尝试使用 math.log10 时遇到错误。 Instead, we use the log function included in the decimal package:相反,我们使用包含在十进制 package 中的日志 function:
for i in range(1,10**4):
if int(fib(Decimal(i)).log10())+1<1000:
continue
else:
print(i)
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.