Problem #64
Difficulty: 20%
Odd Period Square Roots
Solution Language: Python
Problem Statement
All square roots are periodic when written as continued fractions and can be written in the form:
√N = a₀ + 1/(a₁ + 1/(a₂ + 1/(a₃ + … )))
For example, √23 = [4;(1,3,1,8)] where (1,3,1,8) repeats. The period is 4.
It can be seen that the sequence is repeating. For conciseness, we use the notation √23 = [4;(1,3,1,8)], to indicate that the block (1,3,1,8) repeats indefinitely.
Exactly four continued fractions, for N ≤ 13, have an odd period.
How many continued fractions for N ≤ 10000 have an odd period?
Approach
The solution involves:
- Implementing the continued fraction algorithm for square roots
- Detecting when the sequence starts repeating (cycle detection)
- Computing the period length
- Counting numbers with odd period length
- Skipping perfect squares which have no period