Last active
September 22, 2024 06:02
-
-
Save menjaraz/6c4120967c0e937e8864636fd4e99d82 to your computer and use it in GitHub Desktop.
Simple Python script utilizing power series expansion of the ordinary generating function to compute the sequence terms.
This file contains hidden or 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
import sympy as sp | |
def generate_sequence(gen_func, num_terms): | |
""" | |
Generates a sequence up to num_terms terms using the given generating function. | |
Args: | |
gen_func: The generating function as a SymPy expression. | |
num_terms (int): The number of terms in the sequence. | |
Returns: | |
list: A list of integers representing the generated sequence. | |
""" | |
x = sp.symbols('x') | |
series_expansion = sp.sympify(gen_func).series(x, 0, num_terms).removeO() | |
sequences = list(series_expansion.as_coefficients_dict().values()) | |
return sequences | |
def print_sequence(oeis_id, name, length, generating_function): | |
""" | |
Prints a sequence generated from a given generating function. | |
Args: | |
generating_function (str): The generating function as a string. | |
length (int): The length of the sequence to generate. | |
name (str): The name of the sequence. | |
oeis_id (str): The OEIS identifier for the sequence. | |
Returns: | |
None | |
""" | |
sequence = generate_sequence(generating_function, length) | |
print(f"\n{name} (OEIS {oeis_id}):\n", sequence) | |
if __name__ == "__main__": | |
sequences = [ | |
{"oeis_id": "A000012", "name": "All 1's Sequence", "length": 90, "generating_function": '1/(1-x)'}, | |
{"oeis_id": "A000032", "name": "Lucas Sequence", "length": 39, "generating_function": '(2-x)/(1-x-x^2)'}, | |
{"oeis_id": "A000045", "name": "Fibonacci Sequence", "length": 41, "generating_function": 'x/(1-x-x**2)'}, | |
{"oeis_id": "A000108", "name": "Catalan Sequence", "length": 31, "generating_function": '(1-sqrt(1-4*x))/(2*x)'}, | |
{"oeis_id": "A000129", "name": "Pell Sequence", "length": 32, "generating_function": 'x/(1-2*x-x**2)'}, | |
{"oeis_id": "A168604", "name": "(2**(n-2)-1) Sequence", "length": 36, "generating_function": 'x**3/(1-2*x)/(1-x)'} | |
] | |
for sequence in sequences: | |
print_sequence(sequence["oeis_id"], sequence["name"], sequence["length"], sequence["generating_function"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment