コード例 #1
0
ファイル: rational.py プロジェクト: nickspoon/part-ii
def continued_fraction_expansion(target, terms):
    """
    Return continued fraction expansion of a real number.

    >>> continued_fraction_expansion(1.4142, 2)
    [1, 2, 2]

    The first component is the integer part, and rest is fractional
    part, whose number of terms is specified by the second argument.
    """
    # integer part
    ipart = math.floor(target)
    target -= ipart
    result = [int(ipart)]

    # expansion
    for i in range(terms):
        reverse = 1 / target
        term = math.floor(reverse)
        target = reverse - term
        result.append(int(term))

    return result
コード例 #2
0
def continued_fraction_expansion(target, terms):
    """
    Return continued fraction expansion of a real number.

    >>> continued_fraction_expansion(1.4142, 2)
    [1, 2, 2]

    The first component is the integer part, and rest is fractional
    part, whose number of terms is specified by the second argument.
    """
    # integer part
    ipart = math.floor(target)
    target -= ipart
    result = [int(ipart)]

    # expansion
    for i in range(terms):
        reverse = 1 / target
        term = math.floor(reverse)
        target = reverse - term
        result.append(int(term))

    return result