Project Euler # 25 The 1000 digit Fibonacci index
It's possible to calculate the nth Fibonacci number directly thanks to Binet's formula.

Due to floating point errors, the formula doesn't give correct values if n is too large, but it works fine in order to calculate the number of digits.

from math import log10, floor, ceil

def fibonacci_digits(n):
    if n &lt; 2:
        return 1
    ϕ = (1 + 5**0.5) / 2
    return floor(n * log10(ϕ) - log10(5) / 2) + 1


It's possible to calculate the inverse of this function. This way, you can find the first Fibonacci with k digits directly, without any loop:

def euler25(k):
    if k &lt; 2:
        return 1
    ϕ = (1 + 5**0.5) / 2
    return ceil((k + log10(5) / 2 - 1) / log10(ϕ))


It seems to return the correct answer for any k in less than 1 μs :

&gt;&gt;&gt; euler25(1000)
4782
&gt;&gt;&gt; euler25(10_000)
47847
&gt;&gt;&gt; euler25(100_000)
478495
&gt;&gt;&gt; euler25(100_000_000_000_000_000)
478497196678166592


The digits are the same as in:

&gt;&gt;&gt; 1 / log10(ϕ)
4.784971966781666