All Euler problems
Project Euler

An Engineers' Dream Come True

Define n to be an engineer's paradise if: 1. (n-9, n-3, n+3, n+9) are four consecutive primes (a "triple-pair" of sexy primes, each consecutive pair differing by exactly 6). 2. n-8, n-4, n, n+4, n+...

Source sync Apr 19, 2026
Problem #0263
Level Level 14
Solved By 1,228
Languages C++, Python
Answer 2039506520
Length 355 words
modular_arithmeticnumber_theorylinear_algebra

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Consider the number \(6\). The divisors of \(6\) are: \(1,2,3\) and \(6\).

Every number from \(1\) up to and including \(6\) can be written as a sum of distinct divisors of \(6\):

\(1=1\), \(2=2\), \(3=1+2\), \(4=1+3\), \(5=2+3\), \(6=6\).

A number \(n\) is called a practical number if every number from \(1\) up to and including \(n\) can be expressed as a sum of distinct divisors of \(n\).

A pair of consecutive prime numbers with a difference of six is called a sexy pair (since "sex" is the Latin word for "six"). The first sexy pair is \((23, 29)\).

We may occasionally find a triple-pair, which means three consecutive sexy prime pairs, such that the second member of each pair is the first member of the next pair.

We shall call a number \(n\) such that:

  • \((n-9, n-3)\), \((n-3,n+3)\), \((n+3, n+9)\) form a triple-pair, and

  • the numbers \(n-8\), \(n-4\), \(n\), \(n+4\) and \(n+8\) are all practical,

an engineers’ paradise.

Find the sum of the first four engineers’ paradises.

Problem 263: An Engineers’ Dream Come True

Mathematical Foundation

Definition. A positive integer mm is practical if every integer 1km1 \le k \le m can be represented as a sum of distinct divisors of mm.

Theorem 1 (Stewart’s criterion). A positive integer m2m \ge 2 is practical if and only if mm is even and, writing m=p1a1p2a2prarm = p_1^{a_1} p_2^{a_2} \cdots p_r^{a_r} with p1<p2<<prp_1 < p_2 < \cdots < p_r, we have for each i2i \ge 2:

pi1+σ(p1a1pi1ai1)p_i \le 1 + \sigma(p_1^{a_1} \cdots p_{i-1}^{a_{i-1}})

where σ\sigma denotes the sum-of-divisors function.

Proof. See B. M. Stewart, “Sums of distinct divisors,” Amer. J. Math. 76 (1954), 779—785. The forward direction: if pi>1+σ(p1a1pi1ai1)p_i > 1 + \sigma(p_1^{a_1}\cdots p_{i-1}^{a_{i-1}}), then the integer 1+σ(p1a1pi1ai1)1 + \sigma(p_1^{a_1}\cdots p_{i-1}^{a_{i-1}}) cannot be represented. The reverse: if the condition holds for all ii, an inductive construction shows every integer up to σ(m)\sigma(m) (hence up to mm) is representable. \square

Lemma 1 (Modular constraints on nn). If n9,n3,n+3,n+9n-9, n-3, n+3, n+9 are all primes greater than 5, then n10n \equiv 10 or 20(mod30)20 \pmod{30}.

Proof.

  • Modulo 2: The four values n±3n \pm 3, n±9n \pm 9 must be odd, so nn is even.
  • Modulo 3: Since n9n(mod3)n - 9 \equiv n \pmod{3} and n3n(mod3)n - 3 \equiv n \pmod{3}, all four values are n(mod3)\equiv n \pmod{3}. For none to be divisible by 3, we need n≢0(mod3)n \not\equiv 0 \pmod{3}.
  • Modulo 5: The residues n9,n3,n+3,n+9n - 9, n - 3, n + 3, n + 9 modulo 5 are n+1,n+2,n+3,n+4n+1, n+2, n+3, n+4. For none to be 0(mod5)0 \pmod{5}, we need n0(mod5)n \equiv 0 \pmod{5}.

Combining: n0(mod2)n \equiv 0 \pmod{2}, n≢0(mod3)n \not\equiv 0 \pmod{3}, n0(mod5)n \equiv 0 \pmod{5}. By the Chinese Remainder Theorem, n10n \equiv 10 or 20(mod30)20 \pmod{30}. \square

Lemma 2 (Consecutivity conditions). For the four primes to be consecutive, the six intermediate odd values n7,n5,n1,n+1,n+5,n+7n - 7, n - 5, n - 1, n + 1, n + 5, n + 7 must all be composite.

Proof. Since nn is even, the only integers strictly between consecutive members of {n9,n3,n+3,n+9}\{n-9, n-3, n+3, n+9\} that could be prime are the odd ones: n7,n5n-7, n-5 (between n9n-9 and n3n-3), n1,n+1n-1, n+1 (between n3n-3 and n+3n+3), and n+5,n+7n+5, n+7 (between n+3n+3 and n+9n+9). All must be composite for consecutivity. \square

Editorial

Definitions: i.e., (p, p+6), (p+6, p+12), (p+12, p+18) with all four consecutive primes. (1) n-9, n-3, n+3, n+9 are all prime AND are four consecutive primes (no primes between them: n-7, n-5, n-1, n+1, n+5, n+7 all composite) (2) n-8, n-4, n, n+4, n+8 are all practical numbers Key observations:. We sieve primes up to 1.2 * 10^9 using Sieve of Eratosthenes.

Pseudocode

Sieve primes up to 1.2 * 10^9 using Sieve of Eratosthenes
results = []
return sum(results)

Complexity Analysis

  • Time: O(NloglogN)O(N \log \log N) for the Sieve of Eratosthenes up to N1.2×109N \approx 1.2 \times 10^9. The main loop iterates over O(N/15)O(N/15) candidates. Each practical-number test factors mm in O(m)O(\sqrt{m}) and checks Stewart’s criterion in O(logm)O(\log m). Total: O(NloglogN)O(N \log \log N).
  • Space: O(N)O(N) for the prime sieve bitmap.

Answer

2039506520\boxed{2039506520}

Code

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

C++ project_euler/problem_263/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

/*
 * Problem 263: An Engineers' Dream Come True
 *
 * Find first 4 "engineers' paradises" n such that:
 * - n-9, n-3, n+3, n+9 are all prime (sexy prime quadruplet)
 * - (n-9, n-3), (n-3, n+3), (n+3, n+9) are CONSECUTIVE prime pairs
 *   (no primes between them: n-7, n-5, n-1, n+1, n+5, n+7 all composite)
 * - n-8, n-4, n, n+4, n+8 are all practical numbers
 *
 * n ≡ 10 or 20 (mod 30).
 *
 * Answer: 2039506520
 */

const int LIMIT = 1200000010;
vector<bool> is_prime_sieve;

void build_sieve() {
    is_prime_sieve.assign(LIMIT + 1, true);
    is_prime_sieve[0] = is_prime_sieve[1] = false;
    for (ll i = 2; i * i <= LIMIT; i++) {
        if (is_prime_sieve[i]) {
            for (ll j = i * i; j <= LIMIT; j += i)
                is_prime_sieve[j] = false;
        }
    }
}

bool is_practical(ll n) {
    if (n <= 0) return false;
    if (n == 1) return true;
    if (n % 2 != 0) return false;
    vector<pair<ll,int>> factors;
    ll temp = n;
    for (ll d = 2; d * d <= temp; d++) {
        if (temp % d == 0) {
            int e = 0;
            while (temp % d == 0) { temp /= d; e++; }
            factors.push_back({d, e});
        }
    }
    if (temp > 1) factors.push_back({temp, 1});
    sort(factors.begin(), factors.end());
    if (factors[0].first != 2) return false;
    ll sigma = 1;
    for (int i = 0; i < (int)factors.size(); i++) {
        ll p = factors[i].first;
        int a = factors[i].second;
        if (i == 0) {
            ll pw = 1;
            for (int j = 0; j <= a; j++) pw *= p;
            sigma = (pw - 1) / (p - 1);
        } else {
            if (p > 1 + sigma) return false;
            ll pw = 1;
            for (int j = 0; j <= a; j++) pw *= p;
            sigma *= (pw - 1) / (p - 1);
        }
    }
    return true;
}

int main() {
    fprintf(stderr, "Building sieve up to %d...\n", LIMIT);
    build_sieve();
    fprintf(stderr, "Sieve done.\n");

    vector<ll> results;
    for (ll n = 10; n <= 1200000000LL && (int)results.size() < 4; n += 10) {
        int r = n % 30;
        if (r != 10 && r != 20) continue;

        // Check four primes
        if (!is_prime_sieve[n-9] || !is_prime_sieve[n+9] ||
            !is_prime_sieve[n-3] || !is_prime_sieve[n+3])
            continue;

        // Check consecutive: no primes between the pairs
        // n-7, n-5 must be composite (between n-9 and n-3)
        // n-1, n+1 must be composite (between n-3 and n+3)
        // n+5, n+7 must be composite (between n+3 and n+9)
        if (is_prime_sieve[n-7] || is_prime_sieve[n-5] ||
            is_prime_sieve[n-1] || is_prime_sieve[n+1] ||
            is_prime_sieve[n+5] || is_prime_sieve[n+7])
            continue;

        // Check five practical numbers
        if (is_practical(n-8) && is_practical(n-4) &&
            is_practical(n) && is_practical(n+4) &&
            is_practical(n+8)) {
            results.push_back(n);
            fprintf(stderr, "Found n = %lld (%lu/4)\n", n, results.size());
        }
    }

    ll ans = 0;
    for (ll x : results) ans += x;
    cout << ans << endl;
    return 0;
}