Investigating a Prime Pattern
The smallest positive integer n for which the numbers n^2+1, n^2+3, n^2+7, n^2+9, n^2+13, and n^2+27 are consecutive primes is 10. Find the sum of all integers n, 0 < n < 150,000,000, such that n^2...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
The smallest positive integer \(n\) for which the numbers \(n^2 + 1\), \(n^2 + 3\), \(n^2 + 7\), \(n^2 + 9\), \(n^2 + 13\), and \(n^2 + 27\) are consecutive primes is \(10\). The sum of all such integers \(n\) below one-million is \(1242490\).
What is the sum of all such integers \(n\) below \(150\) million?
Problem 146: Investigating a Prime Pattern
Mathematical Development
Theorem 1 (Parity constraint). For to be an odd prime (greater than 2), must be even.
Proof. If is odd, is odd, so is even. Since for , it would be composite. For : is prime but is not. Hence must be even.
Theorem 2 (Mod 3 constraint). We must have .
Proof. If , then , so . Since for , it would be composite.
Theorem 3 (Mod 5 constraint). We must have .
Proof. The quadratic residues mod 5 are . We check each:
- : offsets give — none zero.
- : offsets give — , composite.
- : offsets give — , composite.
Only is viable.
Lemma 1 (Combined congruence). From Theorems 1—3: , , . Thus or .
Proof. By the Chinese Remainder Theorem, (from and ). Combined with : .
Theorem 4 (Mod 7 refinement). Further modular analysis with restricts to specific residues.
Proof. Quadratic residues mod 7 are . For each residue , check whether any offset satisfies :
- : — divisible by 7. Invalid (unless , impossible for ).
- : offsets mod 7 are — and . Invalid.
- : offsets mod 7 are — none zero. Valid.
- : offsets mod 7 are — . Invalid.
So , i.e., or .
Theorem 5 (Consecutive primes condition). The “consecutive primes” requirement means that for all , must be composite.
Proof. “Consecutive primes” means no prime exists strictly between and other than the six listed values. The integers between 1 and 27 not in are . Even offsets produce even (since is even), which are automatically composite (greater than 2). The odd offsets to check are .
Lemma 2 (Miller—Rabin sufficiency). Deterministic Miller—Rabin with witnesses correctly determines primality for all integers up to , which exceeds our maximum value of .
Proof. This follows from the result of Jaeschke (1993) and subsequent computational verification: the witnesses form a sufficient set for numbers below .
Editorial
The key to making the search feasible is to eliminate almost every integer before any expensive primality testing happens. The congruence arguments force into a tiny set of residue classes modulo , so the program first precomputes exactly which residues survive those modular filters.
The main loop then scans candidates of the form using only those residues. For each candidate, it first checks that the six required values
are prime. Only after that does it verify the “consecutive primes” condition by testing the odd offsets in between that are supposed to be composite. Because the candidate set is already heavily reduced, deterministic Miller-Rabin primality tests are fast enough for the full bound.
Pseudocode
Set the search limit to 150000000.
Precompute all residues modulo 210 that satisfy the parity, mod 3, mod 5, and mod 7 filters.
Prepare the two offset lists:
one list for values that must be prime,
one list for odd intermediate values that must be composite.
Initialize the answer to zero.
For each multiple of 210 below the limit:
For each admissible residue:
Form the candidate $n$ and skip it if it falls outside the open interval $(0,150000000)$.
Compute $n^2$ once.
Test every required offset for primality.
If any required value is composite, reject this candidate immediately.
Then test every forbidden odd offset.
If any forbidden value is prime, reject this candidate.
Otherwise add $n$ to the running total.
Return the total.
Complexity Analysis
- Time: The modular sieve reduces candidates to . Each candidate requires Miller—Rabin tests, each costing with modular exponentiation. Total: .
- Space: beyond the list of valid residues (which has elements).
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef __int128 lll;
ll mod_mul(ll a, ll b, ll m) {
return (lll)a * b % m;
}
ll mod_pow(ll base, ll exp, ll mod) {
ll result = 1;
base %= mod;
while (exp > 0) {
if (exp & 1) result = mod_mul(result, base, mod);
base = mod_mul(base, base, mod);
exp >>= 1;
}
return result;
}
bool miller_rabin(ll n, ll a) {
if (n % a == 0) return n == a;
ll d = n - 1;
int r = 0;
while (d % 2 == 0) { d /= 2; r++; }
ll x = mod_pow(a, d, n);
if (x == 1 || x == n - 1) return true;
for (int i = 0; i < r - 1; i++) {
x = mod_mul(x, x, n);
if (x == n - 1) return true;
}
return false;
}
bool is_prime(ll n) {
if (n < 2) return false;
if (n < 4) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (ll a : {2, 3, 5, 7, 11, 13, 17, 19, 23}) {
if (!miller_rabin(n, a)) return false;
}
return true;
}
int main() {
// Precompute valid residues mod 210
vector<int> valid;
for (int r = 0; r < 210; r++) {
if (r % 2 != 0) continue;
if (r % 3 == 0) continue;
if (r % 5 != 0) continue;
// Check mod 7
ll r2 = (ll)r * r;
bool ok = true;
for (int k : {1, 3, 7, 9, 13, 27}) {
if ((r2 + k) % 7 == 0) { ok = false; break; }
}
if (ok) valid.push_back(r);
}
ll total = 0;
int must_prime[] = {1, 3, 7, 9, 13, 27};
int must_not_prime[] = {5, 11, 15, 17, 19, 21, 23, 25};
for (int base = 0; base < 150000000; base += 210) {
for (int r : valid) {
ll n = base + r;
if (n <= 0 || n >= 150000000) continue;
ll n2 = n * n;
bool ok = true;
for (int k : must_prime) {
if (!is_prime(n2 + k)) { ok = false; break; }
}
if (!ok) continue;
for (int k : must_not_prime) {
if (is_prime(n2 + k)) { ok = false; break; }
}
if (!ok) continue;
total += n;
}
}
cout << total << endl;
return 0;
}
"""
Problem 146: Investigating a Prime Pattern
Find the sum of all n, 0 < n < 150,000,000, such that
n^2+1, n^2+3, n^2+7, n^2+9, n^2+13, n^2+27 are consecutive primes.
"""
def is_prime_miller_rabin(n):
"""Deterministic Miller-Rabin for n < 3.317e14 (sufficient witnesses)."""
if n < 2:
return False
if n < 4:
return True
if n % 2 == 0 or n % 3 == 0:
return False
# For n < 3.317 * 10^14, witnesses {2,3,5,7,11,13,17} suffice.
# Our max is ~(1.5e8)^2 + 27 ~ 2.25e16, so use more witnesses.
d = n - 1
r = 0
while d % 2 == 0:
d //= 2
r += 1
for a in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]:
if a >= n:
continue
x = pow(a, d, n)
if x == 1 or x == n - 1:
continue
for _ in range(r - 1):
x = pow(x, x, n) if False else (x * x) % n
if x == n - 1:
break
else:
return False
return True
def solve():
LIMIT = 150_000_000
# Precompute valid residues mod 210
valid_residues = []
for r in range(210):
if r % 2 != 0:
continue
if r % 3 == 0:
continue
if r % 5 != 0:
continue
# Check mod 7
r2 = r * r
ok = True
for k in [1, 3, 7, 9, 13, 27]:
if (r2 + k) % 7 == 0:
ok = False
break
if ok:
valid_residues.append(r)
must_be_prime = [1, 3, 7, 9, 13, 27]
must_not_be_prime = [5, 11, 15, 17, 19, 21, 23, 25]
total = 0
for base in range(0, LIMIT, 210):
for r in valid_residues:
n = base + r
if n <= 0 or n >= LIMIT:
continue
n2 = n * n
ok = True
for k in must_be_prime:
if not is_prime_miller_rabin(n2 + k):
ok = False
break
if not ok:
continue
for k in must_not_be_prime:
if is_prime_miller_rabin(n2 + k):
ok = False
break
if not ok:
continue
total += n
print(total)
if __name__ == "__main__":
solve()