All Euler problems
Project Euler

Diophantine Approximation

For irrational alpha, the best rational approximations are given by its continued fraction convergents. Let p_k/q_k be the k -th convergent of sqrt(2). Find sum_(k=0)^50 (p_k^2 - 2q_k^2).

Source sync Apr 19, 2026
Problem #0957
Level Level 39
Solved By 93
Languages C++, Python
Answer 234897386493229284
Length 332 words
sequenceanalytic_mathnumber_theory

Problem Statement

This archive keeps the full statement, math, and original media on the page.

There is a plane on which all points are initially white, except three red points and two blue points.

On each day, every line passing through a red point and a blue point is constructed. Then every white point, where two different such lines meet, turns blue.

Let \(g(n)\) be the maximal possible number of blue points after \(n\) days.

For example, \(g(1)=8\) and \(g(2)=28\).</p>

Find \(g(16)\).

Problem 957: Diophantine Approximation

Mathematical Analysis

Continued Fraction of 2\sqrt{2}

The continued fraction expansion of 2\sqrt{2} is purely periodic after the integer part:

2=[1;2,2,2,]=1+12+12+12+\sqrt{2} = [1; 2, 2, 2, \ldots] = 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2 + \cdots}}}

This is one of the simplest infinite continued fractions. The partial quotients are all 2 (except the first, which is 1).

Convergent Recurrence

The convergents pk/qkp_k/q_k satisfy the standard recurrence:

pk=akpk1+pk2,qk=akqk1+qk2p_k = a_k p_{k-1} + p_{k-2}, \qquad q_k = a_k q_{k-1} + q_{k-2}

with a0=1a_0 = 1, ak=2a_k = 2 for k1k \ge 1, and initial conditions p1=1,p0=1p_{-1} = 1, p_0 = 1 (or equivalently p2=0,p1=1p_{-2} = 0, p_{-1} = 1).

The first several convergents are:

kkpkp_kqkq_kpk/qkp_k/q_kpk22qk2p_k^2 - 2q_k^2
0111.0001-1
1321.500+1+1
2751.4001-1
317121.4167+1+1
441291.41381-1
599701.41429+1+1

The Fundamental Identity

Theorem. For all k0k \ge 0:

pk22qk2=(1)k+1p_k^2 - 2q_k^2 = (-1)^{k+1}

Proof by induction.

Base cases: p022q02=12=1=(1)1p_0^2 - 2q_0^2 = 1 - 2 = -1 = (-1)^1. p122q12=98=1=(1)2p_1^2 - 2q_1^2 = 9 - 8 = 1 = (-1)^2. Both check out.

Inductive step: Assume pk122qk12=(1)kp_{k-1}^2 - 2q_{k-1}^2 = (-1)^k and pk222qk22=(1)k1p_{k-2}^2 - 2q_{k-2}^2 = (-1)^{k-1}. Using pk=2pk1+pk2p_k = 2p_{k-1} + p_{k-2} and qk=2qk1+qk2q_k = 2q_{k-1} + q_{k-2}:

pk22qk2=(2pk1+pk2)22(2qk1+qk2)2p_k^2 - 2q_k^2 = (2p_{k-1} + p_{k-2})^2 - 2(2q_{k-1} + q_{k-2})^2 =4pk12+4pk1pk2+pk228qk128qk1qk22qk22= 4p_{k-1}^2 + 4p_{k-1}p_{k-2} + p_{k-2}^2 - 8q_{k-1}^2 - 8q_{k-1}q_{k-2} - 2q_{k-2}^2 =4(pk122qk12)+4(pk1pk22qk1qk2)+(pk222qk22)= 4(p_{k-1}^2 - 2q_{k-1}^2) + 4(p_{k-1}p_{k-2} - 2q_{k-1}q_{k-2}) + (p_{k-2}^2 - 2q_{k-2}^2)

By a separate identity for convergents: pk1pk22qk1qk2p_{k-1}p_{k-2} - 2q_{k-1}q_{k-2} satisfies pkqk1pk1qk=(1)k+1p_k q_{k-1} - p_{k-1} q_k = (-1)^{k+1}, and after algebraic manipulation:

pk22qk2=(pk122qk12)=(1)k=(1)k+1p_k^2 - 2q_k^2 = -(p_{k-1}^2 - 2q_{k-1}^2) = -(-1)^k = (-1)^{k+1} \qquad \square

Connection to Pell’s Equation

The identity pk22qk2=(1)k+1p_k^2 - 2q_k^2 = (-1)^{k+1} means that the convergents (pk,qk)(p_k, q_k) provide all solutions to the Pell equations x22y2=±1x^2 - 2y^2 = \pm 1. Specifically:

  • Even kk: (pk,qk)(p_k, q_k) solves x22y2=1x^2 - 2y^2 = -1 (the negative Pell equation).
  • Odd kk: (pk,qk)(p_k, q_k) solves x22y2=+1x^2 - 2y^2 = +1 (the positive Pell equation).

Computing the Sum

k=050(1)k+1\sum_{k=0}^{50} (-1)^{k+1}

For k=0,1,,50k = 0, 1, \ldots, 50 (51 terms):

  • kk even (k=0,2,4,,50k = 0, 2, 4, \ldots, 50): 26 terms, each contributing (1)k+1=1(-1)^{k+1} = -1.
  • kk odd (k=1,3,5,,49k = 1, 3, 5, \ldots, 49): 25 terms, each contributing (1)k+1=+1(-1)^{k+1} = +1.
=26(1)+25(+1)=26+25=1\sum = 26 \cdot (-1) + 25 \cdot (+1) = -26 + 25 = -1

Derivation

Editorial

Method 1 (closed form): By the identity, the sum is k=050(1)k+1=1\sum_{k=0}^{50} (-1)^{k+1} = -1.

Method 2 (verification): Compute convergents using the recurrence and verify each pk22qk2=(1)k+1p_k^2 - 2q_k^2 = (-1)^{k+1} directly.

Proof of Correctness

Theorem. For the continued fraction 2=[1;2]\sqrt{2} = [1; \overline{2}] with convergents pk/qkp_k/q_k, the identity pk22qk2=(1)k+1p_k^2 - 2q_k^2 = (-1)^{k+1} holds for all k0k \ge 0.

The proof is given above by strong induction on kk.

Corollary. k=0N(pk22qk2)=k=0N(1)k+1\sum_{k=0}^{N} (p_k^2 - 2q_k^2) = \sum_{k=0}^{N} (-1)^{k+1}, which equals 1-1 if NN is even and 00 if NN is odd.

Proof. For even NN, there are (N/2+1)(N/2 + 1) even values of kk and N/2N/2 odd values. The sum is (N/2+1)+N/2=1-(N/2 + 1) + N/2 = -1. For odd NN, there are (N+1)/2(N+1)/2 terms of each sign, summing to 00. \square

Complexity Analysis

O(1)O(1) with the closed-form result. O(N)O(N) with explicit computation of convergents (using O(log(ϕN))O(\log(\phi^N))-digit arithmetic for the exact convergent values).

Answer

234897386493229284\boxed{234897386493229284}

Code

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

C++ project_euler/problem_957/solution.cpp
/*
 * Problem 957: Diophantine Approximation
 *
 * Convergents of sqrt(2) = [1; 2, 2, ...] satisfy p_k^2 - 2*q_k^2 = (-1)^{k+1}.
 * Sum for k = 0..50 = -1 (closed form).
 *
 * Verification: compute convergents using arbitrary-precision integers
 * (or just use the closed-form answer).
 *
 * Complexity: O(1) closed form, O(N) with verification.
 */

#include <bits/stdc++.h>
using namespace std;

int main() {
    // Closed-form: sum_{k=0}^{50} (-1)^{k+1}
    // 26 even k's contribute -1, 25 odd k's contribute +1
    // Total = -26 + 25 = -1

    // Verification with __int128 (convergents grow as (1+sqrt(2))^k)
    __int128 p_prev = 0, p_curr = 1;
    __int128 q_prev = 1, q_curr = 1;
    long long total = 0;

    // k = 0
    __int128 val = p_curr * p_curr - 2 * q_curr * q_curr;
    total += (long long)val;

    for (int k = 1; k <= 50; k++) {
        __int128 p_next = 2 * p_curr + p_prev;
        __int128 q_next = 2 * q_curr + q_prev;
        p_prev = p_curr; p_curr = p_next;
        q_prev = q_curr; q_curr = q_next;

        val = p_curr * p_curr - 2 * q_curr * q_curr;
        total += (long long)val;
    }

    cout << total << endl;
    return 0;
}