Odd Period Square Roots
How many continued fractions for sqrt(N), N <= 10000 (where N is not a perfect square), have an odd period?
Problem Statement
This archive keeps the full statement, math, and original media on the page.
All square roots are periodic when written as continued fractions and can be written in the form: $$\displaystyle \quad \quad \sqrt{N}=a_0+\frac 1 {a_1+\frac 1 {a_2+ \frac 1 {a3+ \dots}}}$$ For example, let us consider $\sqrt{23}:$ $$\quad \quad \sqrt{23}=4+\sqrt{23}-4=4+\frac 1 {\frac 1 {\sqrt{23}-4}}=4+\frac 1 {1+\frac{\sqrt{23}-3}7}$$ If we continue we would get the following expansion: $$\displaystyle \quad \quad \sqrt{23}=4+\frac 1 {1+\frac 1 {3+ \frac 1 {1+\frac 1 {8+ \dots}}}}$$ The process can be summarised as follows: $\quad \quad a_0=4, \frac 1 {\sqrt{23}-4}=\frac {\sqrt{23}+4} 7=1+\frac {\sqrt{23}-3} 7$
$\quad \quad a_1=1, \frac 7 {\sqrt{23}-3}=\frac {7(\sqrt{23}+3)} {14}=3+\frac {\sqrt{23}-3} 2$
$\quad \quad a_2=3, \frac 2 {\sqrt{23}-3}=\frac {2(\sqrt{23}+3)} {14}=1+\frac {\sqrt{23}-4} 7$
$\quad \quad a_3=1, \frac 7 {\sqrt{23}-4}=\frac {7(\sqrt{23}+4)} 7=8+\sqrt{23}-4$
$\quad \quad a_4=8, \frac 1 {\sqrt{23}-4}=\frac {\sqrt{23}+4} 7=1+\frac {\sqrt{23}-3} 7$
$\quad \quad a_5=1, \frac 7 {\sqrt{23}-3}=\frac {7 (\sqrt{23}+3)} {14}=3+\frac {\sqrt{23}-3} 2$
$\quad \quad a_6=3, \frac 2 {\sqrt{23}-3}=\frac {2(\sqrt{23}+3)} {14}=1+\frac {\sqrt{23}-4} 7$
$\quad \quad a_7=1, \frac 7 {\sqrt{23}-4}=\frac {7(\sqrt{23}+4)} {7}=8+\sqrt{23}-4$
It can be seen that the sequence is repeating. For conciseness, we use the notation $\sqrt{23}=[4;(1,3,1,8)]$, to indicate that the block (1,3,1,8) repeats indefinitely.
The first ten continued fraction representations of (irrational) square roots are:
$\quad \quad \sqrt{2}=[1;(2)]$, period=$1$
$\quad \quad \sqrt{3}=[1;(1,2)]$, period=$2$
$\quad \quad \sqrt{5}=[2;(4)]$, period=$1$
$\quad \quad \sqrt{6}=[2;(2,4)]$, period=$2$
$\quad \quad \sqrt{7}=[2;(1,1,1,4)]$, period=$4$
$\quad \quad \sqrt{8}=[2;(1,4)]$, period=$2$
$\quad \quad \sqrt{10}=[3;(6)]$, period=$1$
$\quad \quad \sqrt{11}=[3;(3,6)]$, period=$2$
$\quad \quad \sqrt{12}=[3;(2,6)]$, period=$2$
$\quad \quad \sqrt{13}=[3;(1,1,1,1,6)]$, period=$5$
Exactly four continued fractions, for $N \le 13$, have an odd period.
How many continued fractions for $N \le 10\,000$ have an odd period?
Problem 64: Odd Period Square Roots
Mathematical Foundation
Definition 1. For a non-square positive integer , the continued fraction expansion of is the sequence defined by and the recurrence on complete quotients with
Lemma 1 (Divisibility invariant). For all , , and consequently is a positive integer.
Proof. By induction on .
Base case (): divides .
Inductive step: Assume . Then
Every term on the right is divisible by (using the inductive hypothesis for the first term), so . Hence is an integer. That follows from (proved in Lemma 2).
Lemma 2 (Boundedness). For all : and .
Proof. We show and for (when the result is used at ).
Since , we have , so
For the lower bound: for (since for all ), and combined with the structure of the recurrence, when is not a perfect square.
For : since the complete quotient satisfies for , we have , giving .
Theorem 1 (Lagrange’s theorem on periodicity). For any non-square positive integer , the continued fraction expansion of is eventually periodic with purely periodic part of the form
where is the period length. The period terminates at the first index with .
Proof. By Lemma 2, the state takes values in a finite set of size at most . By the pigeonhole principle, the sequence of states must eventually repeat. It is a standard result (see Hardy and Wright, An Introduction to the Theory of Numbers, Theorem 176) that the first repetition occurs precisely when , which is equivalent to . Moreover, the period begins at index 1 (the expansion is purely periodic starting from ).
Theorem 2 (Parity of period and the negative Pell equation). Let be the period of the continued fraction of . The convergents satisfy
Consequently:
- If is odd, then is a solution of .
- If is even, then is the fundamental solution of .
In particular, is odd if and only if the equation has a solution in positive integers.
Proof. The convergent recurrence gives for all (the determinant identity). At the end of the first period, the relationship follows from the fact that (since the period has ended with ) and the general identity applied at with .
If is even, , giving a solution to Pell’s equation. If is odd, , giving a solution to the negative Pell equation. The converse (that solvability of implies odd period) follows because any solution to the negative Pell equation must appear among the convergents, and the sign pattern at period boundaries determines the parity.
Editorial
We examine each non-square separately. For each such value, we generate the continued fraction of by the standard recurrence on the complete quotient parameters and stop when the term appears, which marks the end of one full period. If that period length is odd, we increase the count. Perfect squares are skipped immediately because they do not contribute nontrivial periodic expansions.
Pseudocode
Set the count of odd periods to zero.
For each integer N from 2 up to the limit:
compute a0 = floor(sqrt(N))
if N is a perfect square, skip it
begin the continued-fraction recurrence from m = 0, d = 1, and a = a0
repeat the standard update of m, d, and a until the term 2a0 appears
record how many updates were required
if that period length is odd, increase the count
Return the final count.
Complexity Analysis
Time: For each non-square , the period computation performs exactly iterations, where by the state-space bound (Lemma 2), but in practice on average. Summing over all non-square :
For : .
Space: per value of . Only the variables , , , and the period counter are maintained.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
int count = 0;
for (int N = 2; N <= 10000; N++) {
int a0 = (int)sqrt((double)N);
if (a0 * a0 == N) continue;
int m = 0, d = 1, a = a0;
int period = 0;
do {
m = d * a - m;
d = (N - m * m) / d;
a = (a0 + m) / d;
period++;
} while (a != 2 * a0);
if (period % 2 == 1) count++;
}
cout << count << endl;
return 0;
}
"""
Problem 64: Odd Period Square Roots
How many continued fractions for sqrt(N), N <= 10000, have an odd period?
"""
import math
def cf_period(N):
"""Return the period of the continued fraction of sqrt(N).
Returns 0 if N is a perfect square.
"""
a0 = int(math.isqrt(N))
if a0 * a0 == N:
return 0
m, d, a = 0, 1, a0
period = 0
while True:
m = d * a - m
d = (N - m * m) // d
a = (a0 + m) // d
period += 1
if a == 2 * a0:
return period
def solve():
count = 0
for N in range(2, 10001):
p = cf_period(N)
if p > 0 and p % 2 == 1:
count += 1
return count
answer = solve()
assert answer == 1322
print(answer)