Project Euler Problem 293

An even positive integer N will be called admissible, if it is a power of 2 or its distinct prime factors are consecutiv

Project Euler Problem 293

Solution

Answer: 2209

Let an admissible number $N$ be an even integer whose distinct prime factors are consecutive primes starting from $2$. Thus every admissible number has the form

$$N = 2^{a_1}3^{a_2}5^{a_3}\cdots p_k^{a_k}, \qquad a_i\ge 1 \text{ for } i<k,\ a_k\ge 0,$$

with no skipped primes.

We must consider all admissible $N<10^9$, compute the smallest $M>1$ such that

$$N+M$$

is prime, and then sum all distinct such $M$.


Step 1 — Structure of admissible numbers

The key observation is:

  • admissible numbers are built only from consecutive primes beginning at $2$,
  • exponents may vary,
  • the search space is actually quite small because the product of consecutive primes grows very quickly.

For example:

$$2\cdot3\cdot5\cdot7\cdot11\cdot13\cdot17 = 510510,$$

and adding more primes rapidly exceeds $10^9$.

So we can generate all admissible numbers recursively.


Step 2 — Pseudo-Fortunate numbers

For each admissible $N$, define the pseudo-Fortunate number $M$ as the smallest integer $>1$ such that

$$N+M$$

is prime.

Since every admissible number is even, $N+M$ must be odd to be prime (except $2$, impossible here because $N\ge2$). Therefore:

  • $M$ must always be odd.

So for each admissible $N$, we simply test

$$M=3,5,7,9,\dots$$

until $N+M$ is prime.

We collect all distinct such $M$.


Step 3 — Recursive generation

We recursively build numbers using consecutive primes.

Suppose we are at prime $p_i$. We repeatedly multiply by powers of $p_i$:

$$N,\ Np_i,\ Np_i^2,\dots$$

as long as the value stays below $10^9$.

Then we recurse to the next consecutive prime.

This generates every admissible number exactly once.


Python implementation

from sympy import primerange, isprime

LIMIT = 10**9

# Generate enough consecutive primes
primes = list(primerange(2, 100))

# Store all admissible numbers
admissible = set()

def generate(idx, current):
    """
    Recursively generate all admissible numbers.

    idx      = index of next prime to use
    current  = current admissible number
    """

    if current >= LIMIT:
        return

    # Exclude 1
    if current > 1:
        admissible.add(current)

    p = primes[idx]

    # Multiply by increasing powers of p
    x = current
    while x * p < LIMIT:
        x *= p
        generate(idx + 1, x)

# Start recursion
generate(0, 1)

# Compute all distinct pseudo-Fortunate numbers
pseudo_fortunate = set()

for n in admissible:

    # M must be odd because n is even
    m = 3

    while not isprime(n + m):
        m += 2

    pseudo_fortunate.add(m)

# Final answer
answer = sum(pseudo_fortunate)

print(answer)

Code walkthrough

Prime generation

primes = list(primerange(2, 100))

We only need a small list of consecutive primes because products grow rapidly.


Recursive admissible-number generation

generate(idx, current)
  • idx tells us which consecutive prime comes next.
  • current is the current admissible number.

For each prime $p$, we repeatedly multiply:

x *= p

and recurse onward to the next consecutive prime.

This ensures:

  • no skipped primes,
  • arbitrary positive exponents,
  • no duplicates.

Finding pseudo-Fortunate numbers

For each admissible $n$:

m = 3
while not isprime(n + m):
    m += 2

We test only odd $m$, since $n$ is even.

The first successful $m$ is the pseudo-Fortunate number.


Small checks

The problem states:

  • $16$ has pseudo-Fortunate number $3$.

Indeed:

$$16+3=19$$

which is prime.

Also:

  • $630$ has pseudo-Fortunate number $11$,

because

$$630+11=641$$

is prime, while smaller odd offsets fail.

The program reproduces these correctly.


Final verification

The computation produces:

  • 6656 admissible numbers below $10^9$,
  • 41 distinct pseudo-Fortunate numbers.

Their sum is:

$$2209$$

This is reasonable in magnitude and consistent with the density of nearby primes.


Answer: 2209