Tim Varley Logo
Tim Varley Principal AI Engineer and Tech Leader
Problem #64 hard

Odd Period Square Roots

All square roots are periodic when written as continued fractions and can be written in the form:

N=a0+1a1+1a2+1a3+\sqrt{N}=a_0 + \dfrac 1 {a_1 + \dfrac 1 {a_2 + \dfrac 1 {a_3 + \dots}}}

For example, let us consider 23:\sqrt{23}:

23=4+234=4+11234=4+11+2337\sqrt{23} = 4 + \sqrt{23}-4=4 + \dfrac 1 {\dfrac 1 {\sqrt{23}-4}} = 4+\dfrac 1 {1 + \dfrac{\sqrt{23}-3}7}

If we continue we would get the following expansion:

23=4+11+13+11+18+\sqrt{23}=4 + \dfrac 1 {1 + \dfrac 1 {3+ \dfrac 1 {1 + \dfrac 1 {8+ \dots}}}}

The process can be summarised as follows:

\begin{align} \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 \end{align}

It can be seen that the sequence is repeating. For conciseness, we use the notation 23=[4;(1,3,1,8)]\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:

2=[1;(2)]\quad \quad \sqrt{2}=[1;(2)], period=11
3=[1;(1,2)]\quad \quad \sqrt{3}=[1;(1,2)], period=22
5=[2;(4)]\quad \quad \sqrt{5}=[2;(4)], period=11
6=[2;(2,4)]\quad \quad \sqrt{6}=[2;(2,4)], period=22
7=[2;(1,1,1,4)]\quad \quad \sqrt{7}=[2;(1,1,1,4)], period=44
8=[2;(1,4)]\quad \quad \sqrt{8}=[2;(1,4)], period=22
10=[3;(6)]\quad \quad \sqrt{10}=[3;(6)], period=11
11=[3;(3,6)]\quad \quad \sqrt{11}=[3;(3,6)], period=22
12=[3;(2,6)]\quad \quad \sqrt{12}=[3;(2,6)], period=22
13=[3;(1,1,1,1,6)]\quad \quad \sqrt{13}=[3;(1,1,1,1,6)], period=55

Exactly four continued fractions, for N13N \le 13, have an odd period.

How many continued fractions for N10000N \le 10\,000 have an odd period?

View on Project Euler

Implementations

cpp
#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <tuple>
int odd_period_square_roots() {
int count = 0;
for (long long N = 2; N <= 10000; ++N) {
long long sqrtN = (long long)std::sqrt(N);
if (sqrtN * sqrtN == N) continue;
long long m = 0;
long long d = 1;
long long a = sqrtN;
long long period = 0;
long long a0 = sqrtN;
long long an = sqrtN;
while (an != 2 * a0) {
m = d * an - m;
d = (N - m * m) / d;
an = (a0 + m) / d;
period++;
}
if (period % 2 == 1) ++count;
}
return count;
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[]) {
std::cout << odd_period_square_roots() << std::endl;
}
#endif // UNITTEST_MODE
View on GitHub
O(N) time, O(1) space