Square Root Convergents
The square root of 2 can be expressed as the infinite continued fraction sqrt(2) = 1 + cfrac12 + cfrac12 + cfrac12 +... By expanding this for the first one thousand iterations, one obtains successi...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
It is possible to show that the square root of two can be expressed as an infinite continued fraction. $$\sqrt 2 =1+ \frac 1 {2+ \frac 1 {2 +\frac 1 {2+ \dots}}}$$ By expanding this for the first four iterations, we get:
$1 + \frac 1 2 = \frac 32 = 1.5$
$1 + \frac 1 {2 + \frac 1 2} = \frac 7 5 = 1.4$
$1 + \frac 1 {2 + \frac 1 {2+\frac 1 2}} = \frac {17}{12} = 1.41666 \dots$
$1 + \frac 1 {2 + \frac 1 {2+\frac 1 {2+\frac 1 2}}} = \frac {41}{29} = 1.41379 \dots$
The next three expansions are $\frac {99}{70}$, $\frac {239}{169}$, and $\frac {577}{408}$, but the eighth expansion, $\frac {1393}{985}$, is the first example where the number of digits in the numerator exceeds the number of digits in the denominator.
In the first one-thousand expansions, how many fractions contain a numerator with more digits than the denominator?
Problem 57: Square Root Convergents
Mathematical Development
Definition 1 (Continued Fraction Expansion). The continued fraction representation of is , meaning and for all . The -th expansion refers to the rational number obtained after substitution steps.
Theorem 1 (Recurrence Relation). Define the sequence of fractions by and the recurrence
Then is the -th expansion of the continued fraction for .
Proof. The -th expansion satisfies . Simplifying:
Reading off numerator and denominator gives the stated recurrence. One verifies , which is the first expansion .
Theorem 2 (Matrix Formulation). The recurrence admits the matrix representation
Proof. Direct substitution: . The result follows by induction with .
Lemma 1 (Eigenvalues of ). The matrix has eigenvalues and , with corresponding eigenvectors and .
Proof. The characteristic polynomial is , yielding by the quadratic formula. For : . The verification for is analogous.
Theorem 3 (Asymptotic Growth). As ,
In particular, as , and .
Proof. Decompose where and . Then
Since , the term decays exponentially, so (from the ratio of components of ). The closed-form expressions follow from expanding the decomposition.
Lemma 2 (Connection to Pell Numbers). The denominators satisfy the second-order recurrence for , which is the recurrence for half-companion Pell numbers.
Proof. From Theorem 1, . Substituting into at the next step: . But , so .
Theorem 4 (Digit Excess Criterion). The numerator has strictly more decimal digits than if and only if
This occurs when there exists an integer such that .
Proof. The number of digits of a positive integer is . Thus has more digits than iff , i.e., . This is equivalent to the existence of an integer with , i.e., .
Proposition 1 (Frequency Estimate). The digit-excess events occur with approximate density among the iterations, predicting roughly events per iterations. The exact count is .
Remark. The discrepancy between the heuristic and the exact count arises because the simple density argument counts the “fractional part in an interval” events, but the actual condition involves the floor function applied to of two correlated sequences, which introduces a more complex pattern. In practice, the digit-excess occurs roughly every iterations.
Editorial
We generate the first 1000 convergents of using the recurrence for successive numerators and denominators. After each update, only the decimal lengths of the current numerator and denominator need to be compared, so every expansion contributes either zero or one to the running count. The final count is the number of convergents whose numerator has more digits than the denominator.
Pseudocode
Algorithm: Counting Expansions of sqrt(2) with a Longer Numerator
Require: The first 1000 convergents of the continued fraction for sqrt(2).
Ensure: The number of convergents whose numerator has more decimal digits than the denominator.
1: Initialize the first convergent after 1 as (p, q) ← (3, 2) and set count ← 0.
2: For each expansion index in {1, 2, ..., 1000} do:
3: If the decimal length of p exceeds that of q, increment count.
4: Update (p, q) ← (p + 2q, p + q).
5: Return count.
Complexity Analysis
Theorem 5 (Time Complexity). The algorithm runs in digit-level operations, where .
Proof. By Theorem 3, and grow as , so the number of digits at step is . Each iteration performs two big-integer additions on numbers with digits, costing . The digit comparison costs (just compare lengths). The total work is . For , this is .
Space: for storing and , which at step 1000 have approximately digits.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
// Big integer as vector of digits (least significant first)
typedef vector<int> BigInt;
BigInt fromInt(int x) {
BigInt result;
if (x == 0) { result.push_back(0); return result; }
while (x > 0) {
result.push_back(x % 10);
x /= 10;
}
return result;
}
BigInt add(const BigInt& a, const BigInt& b) {
BigInt result;
int carry = 0;
int n = max(a.size(), b.size());
for (int i = 0; i < n || carry; i++) {
int sum = carry;
if (i < (int)a.size()) sum += a[i];
if (i < (int)b.size()) sum += b[i];
result.push_back(sum % 10);
carry = sum / 10;
}
return result;
}
int numDigits(const BigInt& a) {
return (int)a.size();
}
int main() {
// Recurrence: p_n = p_{n-1} + 2*q_{n-1}, q_n = p_{n-1} + q_{n-1}
BigInt p = fromInt(3);
BigInt q = fromInt(2);
int count = 0;
for (int i = 1; i <= 1000; i++) {
if (numDigits(p) > numDigits(q)) {
count++;
}
BigInt twoq = add(q, q);
BigInt p_new = add(p, twoq);
BigInt q_new = add(p, q);
p = p_new;
q = q_new;
}
cout << count << endl;
return 0;
}
"""
Project Euler Problem 57: Square Root Convergents
In the first 1000 expansions of the continued fraction for sqrt(2),
how many fractions contain a numerator with more digits than the denominator?
Recurrence: p_n = p_{n-1} + 2*q_{n-1}, q_n = p_{n-1} + q_{n-1}
Initial values: p_1 = 3, q_1 = 2
Answer: 153
"""
def solve():
p, q = 3, 2
count = 0
for _ in range(1000):
if len(str(p)) > len(str(q)):
count += 1
p, q = p + 2 * q, p + q
return count
if __name__ == "__main__":
answer = solve()
assert answer == 153
print(answer)