Skip to content

Instantly share code, notes, and snippets.

@alastairparagas
Last active November 8, 2016 11:23
Show Gist options
  • Select an option

  • Save alastairparagas/71389810e2036e9221671f6bdf6a9480 to your computer and use it in GitHub Desktop.

Select an option

Save alastairparagas/71389810e2036e9221671f6bdf6a9480 to your computer and use it in GitHub Desktop.
Recurrence Relations
import math
"""
Fibonacci recurrence relation of the form
fsubn - fsub(n-1) - fsub(n-2) = 0
could be solved by figuring out a value
q^n - q^(n-1) - q^(n-2) = 0
Then q^(n-2) * (q^2 - q - 1) = 0
We thus obtain roots
q1=(1+sqrt(5))/2
q2=(1-sqrt(5))/2
"""
def fn(n):
return ((1 + math.sqrt(5))/2) ** n + ((1 - math.sqrt(5))/2) ** n
def fn_with_constants(n, c1, c2):
return c1 * (((1 + math.sqrt(5))/2) ** n) + c2 * (((1 - math.sqrt(5))/2) ** n)
"""
fn(100) == fn(99) + fn(98)
fn_with_constants(50, 3, 4) == fn(49, 3, 4) + fn(48, 3, 4)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment