The expression is the Binet formula for the Fibonacci numbers. Compute the two roots and round the result to the nearest integer:
from math import sqrt


def binet(n):
    phi = (1 + sqrt(5)) / 2
    psi = (1 - sqrt(5)) / 2
    return round((phi**n - psi**n) / sqrt(5))
The returned object is an integer. This floating-point arithmetic implementation correctly rounds the requested range through ; it is not an arbitrary-precision method for indefinitely large . An integer recurrence is a useful independent numerical check.

Articles by others on the same topic (0)

There are currently no matching articles.