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?
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 form an arithmetic progression (in decreasing order) with common difference , so that , , then .
Proof.
Lemma 1. The positivity constraints and are equivalent to: and (with strict inequality ).
Proof. We need , giving . We need , and since , this requires , i.e., . The condition is automatic since .
Theorem 2. Setting and , the equation holds subject to: , , , and .
Proof. From : , which must be a positive integer, so and (automatic). From : , hence . From : , hence .
Lemma 2. The number of solutions for a given equals the number of factorizations () satisfying and .
Proof. Follows directly from Theorem 2: there is a bijection between valid pairs and valid factorizations via , .
Editorial
In practice, the inner loop is bounded by , so the total work is . 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: . For each , the range of is at most . Summing over , the total operations are .
- 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() {
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;
}
"""
Problem 135: Same Differences
How many values of n < 10^6 have exactly 10 solutions to
x^2 - y^2 - z^2 = n where x, y, z are positive integers in arithmetic progression?
With x = y+d, z = y-d: n = y(4d - y), constraints: y/4 < d < y, n > 0.
"""
def solve():
N = 1_000_000
count = [0] * N
for y in range(1, N):
d_min = y // 4 + 1
d_max = y - 1
# n = y*(4d - y) < N => d < (N/y + y)/4
upper = ((N - 1) // y + y) // 4
if upper < d_max:
d_max = upper
for d in range(d_min, d_max + 1):
n = y * (4 * d - y)
if 0 < n < N:
count[n] += 1
result = sum(1 for i in range(1, N) if count[i] == 10)
return result
answer = solve()
assert answer == 4989
print(answer)