RSA Encryption
Given distinct primes p = 1009 and q = 3643, let n = pq and lambda(n) = lcm(p-1, q-1). Find the sum of all values of e with 1 < e < lambda(n) and gcd(e, lambda(n)) = 1 that minimize the number of u...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
The RSA encryption is based on the following procedure:
Generate two distinct primes $p$ and $q$.
Compute $n = pq$ and $\phi = (p - 1)(q - 1)$.
Find an integer $e$, $1 < e < \phi$, such that $\gcd(e, \phi) = 1$.
A message in this system is a number in the interval $[0, n - 1]$.
A text to be encrypted is then somehow converted to messages (numbers in the interval $[0, n - 1]$).
To encrypt the text, for each message, $m$, $c = m^e \bmod n$ is calculated.
To decrypt the text, the following procedure is needed: calculate $d$ such that $ed = 1 \bmod \phi$, then for each encrypted message, $c$, calculate $m = c^d \bmod n$.
There exist values of $e$ and $m$ such that $m^e \bmod n = m$.
We call messages $m$ for which $m^e \bmod n = m$ unconcealed messages.
An issue when choosing $e$ is that there should not be too many unconcealed messages.
For instance, let $p = 19$ and $q = 37$.
Then $n = 19 \cdot 37 = 703$ and $\phi = 18 \cdot 36 = 648$.
If we choose $e = 181$, then, although $\gcd(181,648) = 1$ it turns out that all possible messages $m$ ($0 \le m \le n - 1$) are unconcealed when calculating $m^e \bmod n$.
For any valid choice of $e$ there exist some unconcealed messages.
It's important that the number of unconcealed messages is at a minimum.
Choose $p = 1009$ and $q = 3643$.
Find the sum of all values of $e$, $1 < e < \phi(1009,3643)$ and $\gcd(e, \phi) = 1$, so that the number of unconcealed messages for this value of $e$ is at a minimum.
Problem 182: RSA Encryption
Mathematical Development
Theorem 1 (Unconcealed Message Count). Let with distinct odd primes, and let be a valid RSA exponent with . The number of unconcealed messages is
Proof. By the Chinese Remainder Theorem, if and only if and . It suffices to count solutions modulo each prime separately; the CRT bijection then gives the product.
Fix a prime . The congruence factors as , yielding two disjoint cases:
Case 1: , contributing exactly solution.
Case 2: and . Since is cyclic of order , Lagrange’s theorem asserts that has exactly solutions in the group. With , there are solutions.
The two cases are disjoint (since does not satisfy ), giving solutions modulo . By CRT, .
Theorem 2 (Minimization). The minimum of over all valid exponents is , achieved precisely when and .
Proof. Since and is even (both and are even), must be odd. Hence is even, which forces
Setting and , we seek to minimize . Since the function is increasing in each variable for , the minimum is , attained when .
It remains to verify the existence of such . We have and . The condition requires (equivalently ), , and . The condition additionally requires (already imposed) and . By the Chinese Remainder Theorem applied to the moduli (pairwise coprime), the system of congruences admits solutions, and infinitely many such exist in the range subject to .
Lemma 1 (Explicit Conditions). Let and . An exponent achieves if and only if all of the following hold:
- ,
- (so that ),
- ,
- ,
- .
Proof. Conditions (2)—(4) ensure : condition (2) gives exactly one factor of ; conditions (3) and (4) exclude the odd prime factors and of . Condition (2) already ensures , so the -adic contribution to is exactly . Conditions (3) and (5) exclude the odd prime factors and of , yielding .
Editorial
The minimum unconcealed count is 9, achieved when gcd(e-1, p-1) = 2 and gcd(e-1, q-1) = 2. We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.
Pseudocode
Set lambda_n <- lcm(p - 1, q - 1)
Set total <- 0
For e from 2 to lambda_n - 1:
If gcd(e, lambda_n) != 1 then continue
If gcd(e - 1, p - 1) != 2 then continue
If gcd(e - 1, q - 1) != 2 then continue
total += e
Return total
Complexity Analysis
- Time: . The loop iterates over all , performing two GCD computations per iteration, each in .
- Space: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
long long p = 1009, q = 3643;
long long pm1 = p - 1, qm1 = q - 1;
long long lambda_n = pm1 / __gcd(pm1, qm1) * qm1;
long long total = 0;
for (long long e = 2; e < lambda_n; e++) {
if (__gcd(e, lambda_n) != 1LL) continue;
if (__gcd(e - 1, pm1) != 2LL) continue;
if (__gcd(e - 1, qm1) != 2LL) continue;
total += e;
}
cout << total << endl;
return 0;
}
"""
Project Euler Problem 182: RSA Encryption
Find the sum of all RSA exponents e that minimize the number of unconcealed
messages for n = 1009 * 3643. The minimum unconcealed count is 9, achieved
when gcd(e-1, p-1) = 2 and gcd(e-1, q-1) = 2.
"""
from math import gcd
def solve():
p, q = 1009, 3643
pm1, qm1 = p - 1, q - 1
lambda_n = pm1 * qm1 // gcd(pm1, qm1)
total = 0
for e in range(2, lambda_n):
if gcd(e, lambda_n) != 1:
continue
if gcd(e - 1, pm1) != 2:
continue
if gcd(e - 1, qm1) != 2:
continue
total += e
return total
if __name__ == "__main__":
answer = solve()
assert answer == 11105433996
print(answer)