Homepage › Solution manuals › Ivan Niven › An Introduction to the Theory of Numbers › Exercise 7.7.3 (Expansion of $\sqrt{15}$ into continued fraction)
Exercise 7.7.3 (Expansion of $\sqrt{15}$ into continued fraction)
Expand into an infinite simple continued fraction.
Answers
Note: By using (7.16) and the formula from Theorem 4.1.6:
we obtain
So we can use only arithmetical operations to compute .
Proof. Starting from , we repeat (7.13), using the preceding note, until (by Theorem 7.21). We obtain
Since , there is no other computing, and , so
□
The following program in pure Python computes the continued fraction expansion of :
from math import sqrt def continued_fraction(d): """ input : d > 1 , not a perfect square output: continued fraction expansion of sqrt(d), (up to the end of the period) """ Vd = int(sqrt(d)) expansion = [Vd] m = Vd q = d - m * m while q != 1: a = (m + Vd) // q expansion.append(a) m = a * q - m; q = (d - m * m) // q expansion.append(m + Vd) return expansion if __name__ == ’__main__’: for i in range(100): if int(round(sqrt(i))) ** 2 != i: print(i, ’=>’, continued_fraction(i)) 2 => [1, 2] 3 => [1, 1, 2] 5 => [2, 4] 6 => [2, 2, 4] 7 => [2, 1, 1, 1, 4] ... 15 => [3, 1, 6] ... 98 => [9, 1, 8, 1, 18] 99 => [9, 1, 18]
This confirms