Skip to main content
Added negative function
Source Link
Solomon Ucko
  • 1.6k
  • 1
  • 10
  • 17

Another formula I found out about: $$f(n)=[\frac{\phi^n}{\sqrt5}]$$ (square brackets mean rounding down AKA truncation AKA conversion to intthe nearest integer).

from math import sqrt

root_five = sqrt(5)
phi = (root_five + 1) / 2

def fibonacci(n):
    return intround((phi ** n) / root_five)

Note: only supports the normal fibonacci sequence, may or may not workuse the following function for negativesnegative support.

def fibonacci(n):
    if n > 0:
        return round((phi ** n) / root_five)
    elif n % 2:
        return round((phi ** -n) / root_five)
    else:
        return -round((phi ** -n) / root_five)

Another formula I found out about: $$f(n)=[\frac{\phi^n}{\sqrt5}]$$ (square brackets mean rounding down AKA truncation AKA conversion to int).

from math import sqrt

root_five = sqrt(5)
phi = (root_five + 1) / 2

def fibonacci(n):
    return int((phi ** n) / root_five)

Note: only supports the normal fibonacci sequence, may or may not work for negatives.

Another formula I found out about: $$f(n)=[\frac{\phi^n}{\sqrt5}]$$ (square brackets mean rounding to the nearest integer).

from math import sqrt

root_five = sqrt(5)
phi = (root_five + 1) / 2

def fibonacci(n):
    return round((phi ** n) / root_five)

Note: only supports the normal fibonacci sequence, use the following function for negative support.

def fibonacci(n):
    if n > 0:
        return round((phi ** n) / root_five)
    elif n % 2:
        return round((phi ** -n) / root_five)
    else:
        return -round((phi ** -n) / root_five)
Source Link
Solomon Ucko
  • 1.6k
  • 1
  • 10
  • 17

Another formula I found out about: $$f(n)=[\frac{\phi^n}{\sqrt5}]$$ (square brackets mean rounding down AKA truncation AKA conversion to int).

from math import sqrt

root_five = sqrt(5)
phi = (root_five + 1) / 2

def fibonacci(n):
    return int((phi ** n) / root_five)

Note: only supports the normal fibonacci sequence, may or may not work for negatives.