<filename>_cont_frac_tools.py
"""
This module contains functions related to continued fractions.
"""

from math import floor, sqrt


def CF(n, details=False):
    """
    Returns the continued fraction representation of sqrt(n).
    The length of the continued fraction representation is one greater than
    its period.
    """
    # Require non-negative
    if n < 0:
        return [0]
    # Each list in frac are values from the sequence of fractions x_i
    # [floor(x_i) or the CF value, denom of x_i, num of x_{i+1}]
    frac = [[floor(sqrt(n)), 1, floor(sqrt(n))]]
    # Check for perfect squres
    if frac[0][0] == sqrt(n):
        return [sqrt(n)]
    # Repeat until periodicity
    while True:
        next = [0, 0, 0]
        # MATH145 flashbacks
        next[1] = int((n - pow(frac[-1][2], 2)) / frac[-1][1])
        next[0] = floor((frac[0][0] + frac[-1][2]) / next[1])
        next[2] = next[0] * next[1] - frac[-1][2]
        if len(frac) > 1 and next == frac[1]:
            if details:
                return frac
            else:
                return [f[0] for f in frac]
        frac.append(next)

def eCF(i):
    """
    Returns the first i terms of the (aperiodic) continued fraction 
    representation of e.
    """
    return [2] + [int((a+1) / 3 * 2) if a % 3 == 2 else 1 for a in range(1, i)]


def cf_expansion(cf, i):
    """
    Produces the i-th term continued fraction expansion [num, denom] of the
    continued fraction representation given by cf.
    """
    p = len(cf) - 1 # period of the CF
    frac = [[1, 0], [cf[0], 1]]
    for j in range(2, i+1):
        a = cf[((j-2) % p) + 1]
        frac.append([
            a * frac[-1][0] + frac[-2][0],
            a * frac[-1][1] + frac[-2][1]
        ])
    return frac[i]

<filename>_cont_frac_tools.py
"""
This module contains functions related to continued fractions.
"""

from math import floor, sqrt


def CF(n, details=False):
    """
    Returns the continued fraction representation of sqrt(n).
    The length of the continued fraction representation is one greater than
    its period.
    """
    # Require non-negative
    if n < 0:
        return [0]
    # Each list in frac are values from the sequence of fractions x_i
    # [floor(x_i) or the CF value, denom of x_i, num of x_{i+1}]
    frac = [[floor(sqrt(n)), 1, floor(sqrt(n))]]
    # Check for perfect squres
    if frac[0][0] == sqrt(n):
        return [sqrt(n)]
    # Repeat until periodicity
    while True:
        next = [0, 0, 0]
        # MATH145 flashbacks
        next[1] = int((n - pow(frac[-1][2], 2)) / frac[-1][1])
        next[0] = floor((frac[0][0] + frac[-1][2]) / next[1])
        next[2] = next[0] * next[1] - frac[-1][2]
        if len(frac) > 1 and next == frac[1]:
            if details:
                return frac
            else:
                return [f[0] for f in frac]
        frac.append(next)

def eCF(i):
    """
    Returns the first i terms of the (aperiodic) continued fraction 
    representation of e.
    """
    return [2] + [int((a+1) / 3 * 2) if a % 3 == 2 else 1 for a in range(1, i)]


def cf_expansion(cf, i):
    """
    Produces the i-th term continued fraction expansion [num, denom] of the
    continued fraction representation given by cf.
    """
    p = len(cf) - 1 # period of the CF
    frac = [[1, 0], [cf[0], 1]]
    for j in range(2, i+1):
        a = cf[((j-2) % p) + 1]
        frac.append([
            a * frac[-1][0] + frac[-2][0],
            a * frac[-1][1] + frac[-2][1]
        ])
    return frac[i]

This module contains functions related to continued fractions. Returns the continued fraction representation of sqrt(n).     The length of the continued fraction representation is one greater than     its period. # Require non-negative # Each list in frac are values from the sequence of fractions x_i # [floor(x_i) or the CF value, denom of x_i, num of x_{i+1}] # Check for perfect squres # Repeat until periodicity # MATH145 flashbacks Returns the first i terms of the (aperiodic) continued fraction      representation of e. Produces the i-th term continued fraction expansion [num, denom] of the     continued fraction representation given by cf. # period of the CF