All Euler problems
Project Euler

Odd Period Square Roots

How many continued fractions for sqrt(N), N <= 10000 (where N is not a perfect square), have an odd period?

Source sync Apr 19, 2026
Problem #0064
Level Level 03
Solved By 25,339
Languages C++, Python
Answer 1322
Length 516 words
sequencemodular_arithmeticbrute_force

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 NN, the continued fraction expansion of N\sqrt{N} is the sequence [a0;a1,a2,][a_0; a_1, a_2, \ldots] defined by a0=Na_0 = \lfloor \sqrt{N} \rfloor and the recurrence on complete quotients αk=(mk+N)/dk\alpha_k = (m_k + \sqrt{N})/d_k with

m0=0,d0=1,ak=αk,m_0 = 0, \quad d_0 = 1, \quad a_k = \lfloor \alpha_k \rfloor, mk+1=dkakmk,dk+1=Nmk+12dk.m_{k+1} = d_k a_k - m_k, \quad d_{k+1} = \frac{N - m_{k+1}^2}{d_k}.

Lemma 1 (Divisibility invariant). For all k0k \ge 0, dk(Nmk2)d_k \mid (N - m_k^2), and consequently dk+1d_{k+1} is a positive integer.

Proof. By induction on kk.

Base case (k=0k=0): d0=1d_0 = 1 divides Nm02=NN - m_0^2 = N.

Inductive step: Assume dk(Nmk2)d_k \mid (N - m_k^2). Then

Nmk+12=N(dkakmk)2=(Nmk2)dk2ak2+2dkakmk.N - m_{k+1}^2 = N - (d_k a_k - m_k)^2 = (N - m_k^2) - d_k^2 a_k^2 + 2 d_k a_k m_k.

Every term on the right is divisible by dkd_k (using the inductive hypothesis for the first term), so dk(Nmk+12)d_k \mid (N - m_{k+1}^2). Hence dk+1=(Nmk+12)/dkd_{k+1} = (N - m_{k+1}^2)/d_k is an integer. That dk+1>0d_{k+1} > 0 follows from mk+1<Nm_{k+1} < \sqrt{N} (proved in Lemma 2). \square

Lemma 2 (Boundedness). For all k1k \ge 1: 0<mka00 < m_k \le a_0 and 1dk2a01 \le d_k \le 2a_0.

Proof. We show mk+1<a0+1=N+1m_{k+1} < a_0 + 1 = \lfloor\sqrt{N}\rfloor + 1 and mk+11m_{k+1} \ge 1 for k0k \ge 0 (when the result is used at k+11k+1 \ge 1).

Since ak=(a0+mk)/dka_k = \lfloor (a_0 + m_k)/d_k \rfloor, we have ak<(a0+mk)/dka_k < (a_0 + m_k)/d_k, so

mk+1=dkakmk<a0+mkmk=a0.m_{k+1} = d_k a_k - m_k < a_0 + m_k - m_k = a_0.

For the lower bound: ak1a_k \ge 1 for k1k \ge 1 (since αk>1\alpha_k > 1 for all k1k \ge 1), and combined with the structure of the recurrence, mk+11m_{k+1} \ge 1 when NN is not a perfect square.

For dkd_k: since the complete quotient αk=(mk+N)/dk\alpha_k = (m_k + \sqrt{N})/d_k satisfies αk>1\alpha_k > 1 for k1k \ge 1, we have dk<mk+Na0+N<2N+1d_k < m_k + \sqrt{N} \le a_0 + \sqrt{N} < 2\sqrt{N} + 1, giving dk2a0d_k \le 2a_0. \square

Theorem 1 (Lagrange’s theorem on periodicity). For any non-square positive integer NN, the continued fraction expansion of N\sqrt{N} is eventually periodic with purely periodic part of the form

N=[a0;a1,a2,,ar1,2a0]\sqrt{N} = [a_0; \overline{a_1, a_2, \ldots, a_{r-1}, 2a_0}]

where rr is the period length. The period terminates at the first index k1k \ge 1 with ak=2a0a_k = 2a_0.

Proof. By Lemma 2, the state (mk,dk)(m_k, d_k) takes values in a finite set of size at most a0×2a0=O(N)a_0 \times 2a_0 = O(N). 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 (mk,dk)=(a0,1)(m_k, d_k) = (a_0, 1), which is equivalent to ak=(a0+a0)/1=2a0a_k = \lfloor (a_0 + a_0)/1 \rfloor = 2a_0. Moreover, the period begins at index 1 (the expansion is purely periodic starting from α1\alpha_1). \square

Theorem 2 (Parity of period and the negative Pell equation). Let rr be the period of the continued fraction of N\sqrt{N}. The convergents pk/qkp_k/q_k satisfy

pr12Nqr12=(1)r.p_{r-1}^2 - N q_{r-1}^2 = (-1)^r.

Consequently:

  1. If rr is odd, then (pr1,qr1)(p_{r-1}, q_{r-1}) is a solution of x2Ny2=1x^2 - Ny^2 = -1.
  2. If rr is even, then (pr1,qr1)(p_{r-1}, q_{r-1}) is the fundamental solution of x2Ny2=1x^2 - Ny^2 = 1.

In particular, rr is odd if and only if the equation x2Ny2=1x^2 - Ny^2 = -1 has a solution in positive integers.

Proof. The convergent recurrence gives pkqk1pk1qk=(1)k+1p_k q_{k-1} - p_{k-1} q_k = (-1)^{k+1} for all k0k \ge 0 (the determinant identity). At the end of the first period, the relationship pr12Nqr12=(1)rp_{r-1}^2 - N q_{r-1}^2 = (-1)^r follows from the fact that dr=1d_r = 1 (since the period has ended with (mr,dr)=(a0,1)(m_r, d_r) = (a_0, 1)) and the general identity pk2Nqk2=(1)k+1dk+1p_k^2 - N q_k^2 = (-1)^{k+1} d_{k+1} applied at k=r1k = r - 1 with dr=1d_r = 1.

If rr is even, (1)r=1(-1)^r = 1, giving a solution to Pell’s equation. If rr is odd, (1)r=1(-1)^r = -1, giving a solution to the negative Pell equation. The converse (that solvability of x2Ny2=1x^2 - Ny^2 = -1 implies odd period) follows because any solution to the negative Pell equation must appear among the convergents, and the sign pattern (1)r(-1)^r at period boundaries determines the parity. \square

Editorial

We examine each non-square N10000N \le 10000 separately. For each such value, we generate the continued fraction of N\sqrt{N} by the standard recurrence on the complete quotient parameters (m,d,a)(m,d,a) and stop when the term 2a02a_0 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 NN, the period computation performs exactly rr iterations, where r2a0a0=O(N)r \le 2a_0 \cdot a_0 = O(N) by the state-space bound (Lemma 2), but in practice r=O(N)r = O(\sqrt{N}) on average. Summing over all non-square NNmaxN \le N_{\max}:

T=N=2N not squareNmaxO(N)=O ⁣(1Nmaxxdx)=O(Nmax3/2).T = \sum_{\substack{N=2 \\ N \text{ not square}}}^{N_{\max}} O(\sqrt{N}) = O\!\left(\int_1^{N_{\max}} \sqrt{x}\, dx\right) = O(N_{\max}^{3/2}).

For Nmax=10000N_{\max} = 10000: T=O(106)T = O(10^6).

Space: O(1)O(1) per value of NN. Only the variables mm, dd, aa, and the period counter are maintained.

Answer

1322\boxed{1322}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_064/solution.cpp
#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;
}