All Euler problems
Project Euler

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...

Source sync Apr 19, 2026
Problem #0146
Level Level 06
Solved By 5,953
Languages C++, Python
Answer 676333270
Length 372 words
modular_arithmeticnumber_theoryalgebra

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 Foundation

Theorem 1 (Parity constraint). For n2+1n^2 + 1 to be an odd prime (greater than 2), nn must be even.

Proof. If nn is odd, n2n^2 is odd, so n2+1n^2 + 1 is even. Since n2+1>2n^2 + 1 > 2 for n>1n > 1, it would be composite. For n=1n = 1: n2+1=2n^2 + 1 = 2 is prime but n2+3=4n^2 + 3 = 4 is not. Hence nn must be even. \square

Theorem 2 (Mod 3 constraint). We must have n≢0(mod3)n \not\equiv 0 \pmod{3}.

Proof. If 3n3 \mid n, then n20(mod3)n^2 \equiv 0 \pmod{3}, so n2+30(mod3)n^2 + 3 \equiv 0 \pmod{3}. Since n2+3>3n^2 + 3 > 3 for n>0n > 0, it would be composite. \square

Theorem 3 (Mod 5 constraint). We must have n0(mod5)n \equiv 0 \pmod{5}.

Proof. The quadratic residues mod 5 are {0,1,4}\{0, 1, 4\}. We check each:

  • n20(mod5)n^2 \equiv 0 \pmod{5}: offsets give {1,3,2,4,3,2}(mod5)\{1,3,2,4,3,2\} \pmod{5} — none zero.
  • n21(mod5)n^2 \equiv 1 \pmod{5}: offsets give {2,4,3,0,4,3}(mod5)\{2,4,3,0,4,3\} \pmod{5}n2+90n^2 + 9 \equiv 0, composite.
  • n24(mod5)n^2 \equiv 4 \pmod{5}: offsets give {0,2,1,3,2,1}(mod5)\{0,2,1,3,2,1\} \pmod{5}n2+10n^2 + 1 \equiv 0, composite.

Only n0(mod5)n \equiv 0 \pmod{5} is viable. \square

Lemma 1 (Combined congruence). From Theorems 1—3: n0(mod2)n \equiv 0 \pmod{2}, n≢0(mod3)n \not\equiv 0 \pmod{3}, n0(mod5)n \equiv 0 \pmod{5}. Thus n10n \equiv 10 or 20(mod30)20 \pmod{30}.

Proof. By the Chinese Remainder Theorem, n0(mod10)n \equiv 0 \pmod{10} (from 2n2 \mid n and 5n5 \mid n). Combined with n≢0(mod3)n \not\equiv 0 \pmod{3}: nmod30{10,20}n \bmod 30 \in \{10, 20\}. \square

Theorem 4 (Mod 7 refinement). Further modular analysis with p=7p = 7 restricts nmod7n \bmod 7 to specific residues.

Proof. Quadratic residues mod 7 are {0,1,2,4}\{0, 1, 2, 4\}. For each residue r=n2mod7r = n^2 \bmod 7, check whether any offset k{1,3,7,9,13,27}k \in \{1,3,7,9,13,27\} satisfies r+k0(mod7)r + k \equiv 0 \pmod{7}:

  • r=0r = 0: 0+700 + 7 \equiv 0n2+7n^2 + 7 divisible by 7. Invalid (unless n2+7=7n^2 + 7 = 7, impossible for n>0n > 0).
  • r=1r = 1: offsets mod 7 are {2,4,1,3,0,0}\{2,4,1,3,0,0\}n2+130n^2+13 \equiv 0 and n2+270n^2+27 \equiv 0. Invalid.
  • r=2r = 2: offsets mod 7 are {3,5,2,4,1,1}\{3,5,2,4,1,1\} — none zero. Valid.
  • r=4r = 4: offsets mod 7 are {5,0,4,6,3,3}\{5,0,4,6,3,3\}n2+30n^2+3 \equiv 0. Invalid.

So n22(mod7)n^2 \equiv 2 \pmod{7}, i.e., n3n \equiv 3 or 4(mod7)4 \pmod{7}. \square

Theorem 5 (Consecutive primes condition). The “consecutive primes” requirement means that for all k{5,11,15,17,19,21,23,25}k \in \{5, 11, 15, 17, 19, 21, 23, 25\}, n2+kn^2 + k must be composite.

Proof. “Consecutive primes” means no prime exists strictly between n2+1n^2+1 and n2+27n^2+27 other than the six listed values. The integers between 1 and 27 not in {1,3,7,9,13,27}\{1,3,7,9,13,27\} are {2,4,5,6,8,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,26}\{2,4,5,6,8,10,11,12,14,15,16,17,18,19,20,21,22,23,24,25,26\}. Even offsets produce even n2+kn^2 + k (since nn is even), which are automatically composite (greater than 2). The odd offsets to check are {5,11,15,17,19,21,23,25}\{5, 11, 15, 17, 19, 21, 23, 25\}. \square

Lemma 2 (Miller—Rabin sufficiency). Deterministic Miller—Rabin with witnesses {2,3,5,7,11,13}\{2, 3, 5, 7, 11, 13\} correctly determines primality for all integers up to 3.317×10243.317 \times 10^{24}, which exceeds our maximum value of n2+27<2.25×1016n^2 + 27 < 2.25 \times 10^{16}.

Proof. This follows from the result of Jaeschke (1993) and subsequent computational verification: the witnesses {2,3,5,7,11,13}\{2, 3, 5, 7, 11, 13\} form a sufficient set for numbers below 3.317×10243.317 \times 10^{24}. \square

Editorial

We precompute valid residues mod 210 = lcm(2, 3, 5, 7). We then quick composite checks for must-not-be-prime offsets. Finally, check consecutive: no primes at intermediate offsets.

Pseudocode

INPUT: Bound B = 150,000,000
OUTPUT: Sum of all valid n
Precompute valid residues mod 210 = lcm(2, 3, 5, 7)
Quick composite checks for must-not-be-prime offsets
Check consecutive: no primes at intermediate offsets
if ok

Complexity Analysis

  • Time: The modular sieve reduces candidates to O(B/210×valid residues)O(B/50)O(B / 210 \times |\text{valid residues}|) \approx O(B / 50). Each candidate requires O(14)O(14) Miller—Rabin tests, each costing O(log2(n2))=O(log2n)O(\log^2(n^2)) = O(\log^2 n) with modular exponentiation. Total: O(Blog2B/50)O(B \log^2 B / 50).
  • Space: O(1)O(1) beyond the list of valid residues (which has O(1)O(1) elements).

Answer

676333270\boxed{676333270}

Code

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

C++ project_euler/problem_146/solution.cpp
#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;
}