Skip to content

Instantly share code, notes, and snippets.

@pqlx
Created April 14, 2019 14:39
Show Gist options
  • Save pqlx/eece0c2e1f59510c7515e3703818c9eb to your computer and use it in GitHub Desktop.
Save pqlx/eece0c2e1f59510c7515e3703818c9eb to your computer and use it in GitHub Desktop.
from typing import *
import numpy as np
def generate_fibonacci_function(degree: int, starting_values: List[int]):
solutions = np.roots([1, *([-1]*degree)])
def gen_coefficents(n):
return [s**n for s in solutions]
constants = np.linalg.solve([gen_coefficents(n) for n in range(degree)], starting_values)
def direct_function(n):
return round(sum([ c*s**n for c, s in zip(constants, solutions)]))
return direct_function
print(generate_fibonacci_function(2, [1, 1])(20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment