Created
April 14, 2019 14:39
-
-
Save pqlx/eece0c2e1f59510c7515e3703818c9eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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