sknasmd
sknasmd

Reputation: 1

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. 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. I find the general formula for the Fibonacci sequence and wrote it in python as below. 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.

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 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?

Upvotes: 0

Views: 542

Answers (2)

yagod
yagod

Reputation: 360

The problem is your numbers are too large for python to handle. So what you want to do is

from decimal import Decimal

Then turn a and b into decimals:

a = Decimal(sqrt(5))
b = Decimal(sqrt(20))

Now we also need to ensure the index i is a Decimal. However, if we only changed that we would run into an error when trying to use math.log10. Instead, we use the log function included in the decimal package:

for i in range(1,10**4):
    if int(fib(Decimal(i)).log10())+1<1000:
        continue
    else:
        print(i)
        break

Upvotes: 0

Manu Jo Varghese
Manu Jo Varghese

Reputation: 400

To find the index of the first term in the Fibonacci sequence to contain 1000 digits using Python, you can use the following approach:

  1. Define a function fibonacci_index that takes in a number n and returns the index of the first term in the Fibonacci sequence to contain n digits.
  2. Initialize variables a and b to 1, which are the first two terms of the Fibonacci sequence.
  3. Initialize a variable index to 2, which is the index of the second term in the sequence.
  4. Implement a while loop that continues until the number of digits in a is greater than or equal to n. Inside the loop:

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. 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

Upvotes: 1

Related Questions