Solution

ID: past-exam-of-the-mathematical-and-computational-biology-course-of-the-university-of-cambridge/2024/paper-2/a1/a/solution

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.

New to topics? Read the docs here!