Prime Triples and Geometric Sequences
Let S(n) be the sum of a + b + c over all triples (a, b, c) of prime numbers with a < b < c < n such that a + 1, b + 1, c + 1 form a geometric sequence. Given S(100) = 1035, find S(10^8).
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let $S(n) = \displaystyle \sum a + b + c$ over all triples $(a, b, c)$ such that:
$a$, $b$ and $c$ are prime numbers.
$a < b < c < n$.
$a+1$, $b+1$, and $c+1$ form a geometric sequence.
For example, $S(100) = 1035$ with the following triples: $(2, 5, 11)$, $(2, 11, 47)$, $(5, 11, 23)$, $(5, 17, 53)$, $(7, 11, 17)$, $(7, 23, 71)$, $(11, 23, 47)$, $(17, 23, 31)$, $(17, 41, 97)$, $(31, 47, 71)$, $(71, 83, 97)$.
Find $S(10^8)$.
Problem 518: Prime Triples and Geometric Sequences
Mathematical Foundation
Theorem 1 (Geometric Sequence Characterization). Three positive integers form a geometric sequence if and only if .
Proof. If the common ratio is , then and , so . Conversely, if with , set ; then .
Theorem 2 (Parametrization). Let be primes with and in geometric progression. Then there exist positive integers with and such that
Proof. From , write the common ratio as in lowest terms (, ). Then must be divisible by : set . We get and . Both are positive integers since .
Lemma 1 (Integrality Constraint). For and to be integers, it suffices that (which is given by the reduced-fraction representation).
Proof. Since are positive integers and , the expressions and are manifestly integers.
Lemma 2 (Bound on Parameters). For (i.e., ), we need . Thus and .
Proof. From , we get . Since , . For fixed , .
Editorial
We sieve primes up to n. Finally, enumerate triples. We first generate the primes required by the search, then enumerate the admissible combinations and retain only the values that satisfy the final test.
Pseudocode
Sieve primes up to n
Enumerate triples
Complexity Analysis
- Time: The sieve takes . The main loop iterates over up to , for each over up to , and for each over up to . The total number of triples is , but the filter and primality checks reduce the constant. In practice, runtime is dominated by the sieve at .
- Space: for the prime sieve (can use a bitarray for ).
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
const int LIMIT = 100000000; // 10^8
int main() {
// Sieve of Eratosthenes
vector<bool> is_prime(LIMIT + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; (long long)i * i <= LIMIT; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= LIMIT; j += i)
is_prime[j] = false;
}
}
long long ans = 0;
// Enumerate x, k, y
for (long long x = 2; x * x <= LIMIT; x++) {
for (long long k = 1; k * x * x <= LIMIT; k++) {
long long c1 = k * x * x; // c + 1
if (c1 - 1 < 2) continue;
if (!is_prime[c1 - 1]) continue;
for (long long y = 1; y < x; y++) {
// Skip if both even
if (x % 2 == 0 && y % 2 == 0) continue;
long long a1 = k * y * y; // a + 1
long long b1 = k * x * y; // b + 1
if (a1 < 2 || b1 < 2) continue;
if (a1 - 1 < 2) continue;
if (!is_prime[a1 - 1]) continue;
if (!is_prime[b1 - 1]) continue;
// Check gcd(x, y) == 1
if (__gcd(x, y) != 1) continue;
ans += (a1 - 1) + (b1 - 1) + (c1 - 1);
}
}
}
cout << ans << endl;
return 0;
}
"""
Project Euler Problem 518: Prime Triples and Geometric Sequences
Find S(10^8) = sum of a+b+c over all prime triples (a,b,c)
with a<b<c<10^8 and a+1,b+1,c+1 forming a geometric sequence.
"""
import math
def solve():
LIMIT = 10**8
# Sieve of Eratosthenes
is_prime = bytearray([1]) * (LIMIT + 1)
is_prime[0] = is_prime[1] = 0
for i in range(2, int(LIMIT**0.5) + 1):
if is_prime[i]:
is_prime[i*i::i] = bytearray(len(is_prime[i*i::i]))
ans = 0
# Enumerate x, k, y
x = 2
while x * x <= LIMIT:
k = 1
while k * x * x <= LIMIT:
c1 = k * x * x
if c1 - 1 >= 2 and is_prime[c1 - 1]:
for y in range(1, x):
if x % 2 == 0 and y % 2 == 0:
continue
a1 = k * y * y
b1 = k * x * y
if a1 < 2 or b1 < 2 or a1 - 1 < 2:
continue
if not is_prime[a1 - 1]:
continue
if not is_prime[b1 - 1]:
continue
if math.gcd(x, y) != 1:
continue
ans += (a1 - 1) + (b1 - 1) + (c1 - 1)
k += 1
x += 1
print(ans)
if __name__ == "__main__":
solve()