All Euler problems
Project Euler

Singleton Difference

The positive integers x, y, and z are consecutive terms of an arithmetic progression. Given that n is a positive integer, the equation x^2 - y^2 - z^2 = n has exactly one solution for how many valu...

Source sync Apr 19, 2026
Problem #0136
Level Level 05
Solved By 6,643
Languages C++, Python
Answer 2544559
Length 229 words
modular_arithmeticnumber_theorybrute_force

Problem Statement

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

The positive integers, $x$, $y$, and $z$, are consecutive terms of an arithmetic progression. Given that $n$ is a positive integer, the equation, $x^2 - y^2 - z^2 = n$, has exactly one solution when $n = 20$: $$13^2 - 10^2 - 7^2 = 20.$$ In fact there are twenty-five values of $n$ below one hundred for which the equation has a unique solution.

How many values of $n$ less than fifty million have exactly one solution?

Problem 136: Singleton Difference

Mathematical Foundation

Theorem 1. With x=y+dx = y + d, z=ydz = y - d (arithmetic progression, common difference dd), we have x2y2z2=y(4dy)x^2 - y^2 - z^2 = y(4d - y), subject to y1y \geq 1, y/4<d<yy/4 < d < y, and dd a positive integer.

Proof. Identical to Problem 135, Theorem 1. \square

Theorem 2. Setting n=uvn = uv where u=yu = y and v=4dyv = 4d - y, the number of representations of nn equals the number of ordered factorizations n=uvn = uv with u,v1u, v \geq 1, v<3uv < 3u, and u+v0(mod4)u + v \equiv 0 \pmod{4}.

Proof. Identical to Problem 135, Theorem 2. \square

Lemma 1. If nn is prime, then n=uvn = uv has exactly two factorizations: (u,v)=(1,n)(u, v) = (1, n) and (u,v)=(n,1)(u, v) = (n, 1). The pair (n,1)(n, 1) always satisfies v<3uv < 3u (since 1<3n1 < 3n). It satisfies u+v0(mod4)u + v \equiv 0 \pmod{4} iff n+10(mod4)n + 1 \equiv 0 \pmod{4}, i.e., n3(mod4)n \equiv 3 \pmod{4}. The pair (1,n)(1, n) satisfies v<3uv < 3u iff n<3n < 3, i.e., n=2n = 2. For n=2n = 2: u+v=3≢0(mod4)u + v = 3 \not\equiv 0 \pmod{4}, so it fails. Hence a prime n>3n > 3 with n3(mod4)n \equiv 3 \pmod 4 has exactly one solution.

Proof. Direct verification of the conditions from Theorem 2. \square

Lemma 2. The values n=4n = 4 and n=16n = 16 each have exactly one solution. (These are the small exceptional cases found by direct enumeration.)

Proof. For n=4n = 4: factorizations are (1,4),(2,2),(4,1)(1,4), (2,2), (4,1). Check conditions:

  • (1,4)(1, 4): v<3u    4<3v < 3u \iff 4 < 3 — false.
  • (2,2)(2, 2): v<3u    2<6v < 3u \iff 2 < 6 — true. u+v=40(mod4)u + v = 4 \equiv 0 \pmod{4} — true. Valid.
  • (4,1)(4, 1): v<3u    1<12v < 3u \iff 1 < 12 — true. u+v=5≢0(mod4)u + v = 5 \not\equiv 0 \pmod{4} — false.

So n=4n = 4 has exactly 1 solution.

For n=16n = 16: factorizations (u,v)(u,v) with v<3uv < 3u and u+v0(mod4)u + v \equiv 0 \pmod 4: checking all divisor pairs yields exactly one valid pair (4,4)(4, 4). \square

Editorial

n = y*(4d - y) = y*u where u = 4d - y, 0 < u < 3y, (u+y) % 4 == 0.

Pseudocode

    N = 50000000
    solutions = array of size N, initialized to 0

    For y from 1 to N-1:
        d_min = floor(y / 4) + 1
        d_max = min(y - 1, floor((N - 1 + y^2) / (4 * y)))
        For d from d_min to d_max:
            n = y * (4 * d - y)
            If 0 < n < N then
                solutions[n] += 1

    count = 0
    For n from 1 to N-1:
        If solutions[n] == 1 then
            count += 1
    Return count

Complexity Analysis

  • Time: O(NlogN)O(N \log N) by the same analysis as Problem 135.
  • Space: O(N)O(N) for the counting array.

Answer

2544559\boxed{2544559}

Code

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

C++ project_euler/problem_136/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    // n = y*(4d - y) where d in (y/4, y)
    // Let u = 4d - y, then n = y*u, u in (0, 3y), u+y = 0 mod 4
    // Count n < N with exactly one (y,u) factorization satisfying constraints.

    const int N = 50000000;
    vector<int> cnt(N, 0);

    for (int y = 1; y < N; y++) {
        // u must satisfy: u > 0, u < 3y, y*u < N, (u+y) % 4 == 0
        // u starts at first positive value with (u+y) % 4 == 0
        int u_start = (4 - (y % 4)) % 4;
        if (u_start == 0) u_start = 4;
        // Actually u > 0 and (u+y) % 4 == 0
        // So u % 4 == (-y) % 4 == (4 - y%4) % 4
        int rem = (4 - y % 4) % 4;
        u_start = rem;
        if (u_start == 0) u_start = 4;

        long long u_max = min((long long)3 * y - 1, (long long)(N - 1) / y);

        for (long long u = u_start; u <= u_max; u += 4) {
            cnt[y * u]++;
        }
    }

    int answer = 0;
    for (int i = 1; i < N; i++) {
        if (cnt[i] == 1) answer++;
    }

    cout << answer << endl;
    return 0;
}