All Euler problems
Project Euler

Same Differences

Given the positive integers x, y, z are consecutive terms of an arithmetic progression, how many values of n less than one million have exactly ten solutions to x^2 - y^2 - z^2 = n?

Source sync Apr 19, 2026
Problem #0135
Level Level 05
Solved By 7,432
Languages C++, Python
Answer 4989
Length 206 words
brute_forcelinear_algebraarithmetic

Problem Statement

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

Given the positive integers, \(x\), \(y\), and \(z\), are consecutive terms of an arithmetic progression, the least value of the positive integer, \(n\), for which the equation, \(x^2 - y^2 - z^2 = n\), has exactly two solutions is \(n = 27\): \[34^2 - 27^2 - 20^2 = 12^2 - 9^2 - 6^2 = 27.\] It turns out that \(n = 1155\) is the least value which has exactly ten solutions.

How many values of \(n\) less than one million have exactly ten distinct solutions?

Problem 135: Same Differences

Mathematical Foundation

Theorem 1. If x,y,zx, y, z form an arithmetic progression (in decreasing order) with common difference d>0d > 0, so that x=y+dx = y + d, z=ydz = y - d, then x2y2z2=y(4dy)x^2 - y^2 - z^2 = y(4d - y).

Proof.

x2y2z2=(y+d)2y2(yd)2x^2 - y^2 - z^2 = (y+d)^2 - y^2 - (y-d)^2 =(y2+2yd+d2)y2(y22yd+d2)=4ydy2=y(4dy)= (y^2 + 2yd + d^2) - y^2 - (y^2 - 2yd + d^2) = 4yd - y^2 = y(4d - y)

\square

Lemma 1. The positivity constraints x,y,z>0x, y, z > 0 and n>0n > 0 are equivalent to: y1y \geq 1 and y/4dy1\lceil y/4 \rceil \leq d \leq y - 1 (with strict inequality d>y/4d > y/4).

Proof. We need z=yd>0z = y - d > 0, giving d<yd < y. We need n=y(4dy)>0n = y(4d - y) > 0, and since y>0y > 0, this requires 4dy>04d - y > 0, i.e., d>y/4d > y/4. The condition x=y+d>0x = y + d > 0 is automatic since y,d>0y, d > 0. \square

Theorem 2. Setting u=yu = y and v=4dyv = 4d - y, the equation n=uvn = uv holds subject to: u1u \geq 1, v1v \geq 1, v<3uv < 3u, and u+v0(mod4)u + v \equiv 0 \pmod{4}.

Proof. From v=4dy=4duv = 4d - y = 4d - u: d=(u+v)/4d = (u + v)/4, which must be a positive integer, so u+v0(mod4)u + v \equiv 0 \pmod{4} and u+v>0u + v > 0 (automatic). From d<yd < y: (u+v)/4<u(u + v)/4 < u, hence v<3uv < 3u. From d>y/4d > y/4: (u+v)/4>u/4(u + v)/4 > u/4, hence v>0v > 0. \square

Lemma 2. The number of solutions for a given nn equals the number of factorizations n=uvn = uv (u,v1u, v \geq 1) satisfying v<3uv < 3u and u+v0(mod4)u + v \equiv 0 \pmod{4}.

Proof. Follows directly from Theorem 2: there is a bijection between valid (y,d)(y, d) pairs and valid (u,v)(u, v) factorizations via u=yu = y, v=4dyv = 4d - y. \square

Editorial

In practice, the inner loop is bounded by min(y1,(N/y+y)/4)\min(y - 1, (N/y + y)/4), so the total work is O(NlogN)O(N \log N). We also n = y(4d - y) < N => d < (N/y + y) / 4. Finally, more precisely: 4d - y < N/y => d < (N/y + y)/4.

Pseudocode

N = 1000000
Also n = y(4d - y) < N => d < (N/y + y) / 4
more precisely: 4d - y < N/y => d < (N/y + y)/4

Complexity Analysis

  • Time: O(NlogN)O(N \log N). For each yy, the range of dd is at most min(3y/4,N/(4y))\min(3y/4, N/(4y)). Summing over yy, the total operations are y=1NN/(4y)+y=NN3y/4O(NlogN)\sum_{y=1}^{\sqrt{N}} N/(4y) + \sum_{y=\sqrt{N}}^{N} 3y/4 \approx O(N \log N).
  • Space: O(N)O(N) for the counting array.

Answer

4989\boxed{4989}

Code

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

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

int main() {
    const int N = 1000000;
    vector<int> count(N, 0);

    // n = y*(4d - y), with y/4 < d < y, n > 0, n < N
    // Iterate over y and d
    for (int y = 1; y < N; y++) {
        int d_min = y / 4 + 1; // d > y/4
        int d_max = y - 1;     // d < y

        // Also n = y*(4d - y) < N => 4d - y < N/y => d < (N/y + y) / 4
        // But be careful with integer division
        if (y > 0) {
            long long upper = ((long long)(N - 1) / y + y);
            int d_bound = (int)(upper / 4);
            if (d_bound < d_max) d_max = d_bound;
        }

        for (int d = d_min; d <= d_max; d++) {
            int n = y * (4 * d - y);
            if (n > 0 && n < N)
                count[n]++;
        }
    }

    int result = 0;
    for (int i = 1; i < N; i++)
        if (count[i] == 10)
            result++;

    cout << result << endl;
    return 0;
}