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...
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 , (arithmetic progression, common difference ), we have , subject to , , and a positive integer.
Proof. Identical to Problem 135, Theorem 1.
Theorem 2. Setting where and , the number of representations of equals the number of ordered factorizations with , , and .
Proof. Identical to Problem 135, Theorem 2.
Lemma 1. If is prime, then has exactly two factorizations: and . The pair always satisfies (since ). It satisfies iff , i.e., . The pair satisfies iff , i.e., . For : , so it fails. Hence a prime with has exactly one solution.
Proof. Direct verification of the conditions from Theorem 2.
Lemma 2. The values and each have exactly one solution. (These are the small exceptional cases found by direct enumeration.)
Proof. For : factorizations are . Check conditions:
- : — false.
- : — true. — true. Valid.
- : — true. — false.
So has exactly 1 solution.
For : factorizations with and : checking all divisor pairs yields exactly one valid pair .
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: by the same analysis as Problem 135.
- Space: for the counting array.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 136: Singleton Difference
Find how many values of n < 50,000,000 have exactly one solution to
x^2 - y^2 - z^2 = n where x, y, z are consecutive terms of an AP.
n = y*(4d - y) = y*u where u = 4d - y, 0 < u < 3y, (u+y) % 4 == 0.
"""
import numpy as np
def solve(N=50_000_000):
cnt = np.zeros(N, dtype=np.int32)
for y in range(1, N):
rem = (4 - y % 4) % 4
u_start = rem if rem != 0 else 4
u_max = min(3 * y - 1, (N - 1) // y)
if u_start > u_max:
continue
# Increment cnt[y*u] for u = u_start, u_start+4, ..., <= u_max
for u in range(u_start, u_max + 1, 4):
cnt[y * u] += 1
answer = int(np.sum(cnt == 1))
return answer
if __name__ == "__main__":
print(solve())