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