0
DevMike
4y

I want to print the first number in the Fibonacci sequence to contain over 1000 digits. I got fibbonaci to work but I cannot seem to figure out how to implement the "Contain over 1000 digits".

Do I make a list to store the fibbonaci numbers in then do a statement?

My Python 3 Code:

def fibonacci(num):
if num == 2 or num ==1:
return 1
return sum([fibonacci(num - 2), fibonacci(num - 1)])

print(fibonacci(7))

Comments
  • 3
    The roughtest solution would be, for each evaluated fibonaci number, you can parse it to a string and then split them into a list. Then its just the case of looking at the len(array).
  • 0
    @theuser Thank you! Can you assist me with the actual coding of it though?
  • 2
    Write the algorithm on paper without looking at the code. Once you figure it out there, put it in code.
  • 0
    Does python have a number type that I'd not constrained by the usual bit restrictions? Meaning can they be. Infinitely big?

    Secondly with that recursive algorithm you will probably never find that number.

    Memoization will definitely help but I think that this still would take a very long to find that number
  • 0
    Don't use recursion for that. Just use a loop and store the two last numbers. Recursion doesn't like stack frames in the hundreds, let alone the thousands.

    And ensure that your number type can store numbers that big. Floats aren't good for this, you'll need a number type with _way_ more bytes. I'd suggest you look for a better number type, and double check the exact implementation. Eg. Floats can be arbitrarily large, but they will eventually lose accuracy.
  • 0
    @fuckwit Numbers in Python have infinite length by default.

    @DevMike You can use len(str(number))) to get the length, but it might not be very efficient.
  • 0
    @hilight

    import math as m

    def digits(n):
    return m.floor(m.log10(n)+1)

    # Should do the trick
Add Comment