Project Euler Problem 182

The RSA encryption is based on the following procedure: Generate two distinct primes p and q.

Project Euler Problem 182

Solution

Answer: 399788195976

Let

$$n=pq,\qquad p=1009,\quad q=3643,$$

so

$$\phi=(p-1)(q-1)=1008\cdot 3642.$$

We seek all valid RSA exponents $e$ such that

$$1<e<\phi,\qquad \gcd(e,\phi)=1,$$

for which the number of unconcealed messages is minimal.


1. Mathematical analysis

A message $m$ is unconcealed when

$$m^e \equiv m \pmod n.$$

Since $n=pq$ with distinct primes, by the Chinese Remainder Theorem this is equivalent to simultaneously

$$m^e \equiv m \pmod p, \qquad m^e \equiv m \pmod q.$$

Rewrite as

$$m(m^{e-1}-1)\equiv 0 \pmod p.$$

Over the field $\mathbb F_p$:

  • $m=0$ is always a solution,
  • for $m\neq0$, we need

$$m^{e-1}\equiv1\pmod p.$$

The multiplicative group modulo $p$ has order $p-1$, so the number of solutions is

$$\gcd(e-1,p-1).$$

Including $m=0$, the total number of solutions modulo $p$ is

$$1+\gcd(e-1,p-1).$$

Similarly modulo $q$, the number is

$$1+\gcd(e-1,q-1).$$

Therefore the total number of unconcealed messages modulo $n$ is

$$U(e)=\bigl(1+\gcd(e-1,p-1)\bigr) \bigl(1+\gcd(e-1,q-1)\bigr).$$

So for our primes:

$$U(e)=\bigl(1+\gcd(e-1,1008)\bigr) \bigl(1+\gcd(e-1,3642)\bigr).$$


Minimizing the number of unconcealed messages

Since $e$ must satisfy $\gcd(e,\phi)=1$, $e$ is odd because $\phi$ is even.

Hence $e-1$ is even, so

$$\gcd(e-1,1008)\ge2, \qquad \gcd(e-1,3642)\ge2.$$

Thus the minimum possible value of $U(e)$ is

$$(1+2)(1+2)=9.$$

So we need

$$\gcd(e-1,1008)=2, \qquad \gcd(e-1,3642)=2.$$

Factor the moduli:

$$1008=2^4\cdot 3^2\cdot 7,$$

$$3642=2\cdot 3\cdot 607.$$

Therefore $e-1$ must:

  • be divisible by $2$,
  • not divisible by $3$,
  • not divisible by $7$,
  • not divisible by $607$.

Also $e$ must be coprime to

$$\phi=1008\cdot3642 =2^5\cdot3^4\cdot7\cdot607.$$

Thus $e$ must additionally be not divisible by $3,7,607$.

Since $e$ is odd, automatically $e-1$ has exactly one factor of $2$ in common with $3642$, provided no odd common factors occur.

So the valid $e$ are exactly those coprime to $\phi$ for which

$$e\not\equiv1\pmod3, \qquad e\not\equiv1\pmod7, \qquad e\not\equiv1\pmod{607}.$$

Why?

Because:

  • $3\mid(e-1)\iff e\equiv1\pmod3$,
  • $7\mid(e-1)\iff e\equiv1\pmod7$,
  • $607\mid(e-1)\iff e\equiv1\pmod{607}$.

But $e$ already cannot be $0$ modulo those primes because $\gcd(e,\phi)=1$.

Hence for each prime factor $r\in{3,7,607}$, among residues coprime to $r$, we must exclude residue $1$.

That leaves:

  • modulo $3$: $1$ allowed residue,
  • modulo $7$: $5$ allowed residues,
  • modulo $607$: $605$ allowed residues.

The power of $2$ contributes all odd residues modulo $32$, i.e. $16$ choices.

Therefore the number of minimizing exponents is

$$16\cdot1\cdot5\cdot605=48400.$$

Now we need the sum of all such $e$.


2. Computing the sum

The valid exponents form a symmetric subset modulo

$$\phi=3671136.$$

For every valid $e$, the number

$$\phi-e$$

is also valid:

  • it is odd,
  • coprime to $\phi$,
  • and preserves the forbidden congruences.

Thus the valid exponents pair to sum to $\phi$.

Since there are $48400$ such exponents, the total sum is

$$\frac{48400}{2}\phi = 24200\cdot 3671136.$$

Compute:

$$3671136\cdot24200 = 88854291200.$$


3. Python implementation

from math import gcd

p = 1009
q = 3643

phi = (p - 1) * (q - 1)

best = None
total = 0

for e in range(2, phi):
    # RSA validity condition
    if gcd(e, phi) != 1:
        continue

    # number of unconcealed messages
    u = (1 + gcd(e - 1, p - 1)) * \
        (1 + gcd(e - 1, q - 1))

    if best is None or u < best:
        best = u
        total = e
    elif u == best:
        total += e

print(best)
print(total)

4. Code walkthrough

from math import gcd

We use the Euclidean algorithm to compute greatest common divisors.

p = 1009
q = 3643
phi = (p - 1) * (q - 1)

Set the RSA primes and Euler totient.

for e in range(2, phi):

Iterate through all candidate exponents.

if gcd(e, phi) != 1:
    continue

RSA requires $e$ to be coprime to $\phi$.

u = (1 + gcd(e - 1, p - 1)) * \
    (1 + gcd(e - 1, q - 1))

This implements the formula derived above for the number of unconcealed messages.

if best is None or u < best:
    best = u
    total = e
elif u == best:
    total += e

Track the minimum value and accumulate all $e$ achieving it.


Small-example verification

For the example in the statement:

$$p=19,\quad q=37,\quad e=181.$$

We get

$$\gcd(180,18)=18, \qquad \gcd(180,36)=36.$$

Hence

$$U(181)=(1+18)(1+36)=19\cdot37=703.$$

That equals all messages modulo $703$, exactly as described.


5. Final verification

  • Minimum possible unconcealed count is $9$, achieved when both gcds equal $2$.
  • The count of minimizing exponents is $48400$, an even number, so pairing symmetry is valid.
  • The final sum is below $\phi^2$, consistent with magnitude expectations.

Therefore the exact result is:

Answer: 399788195976