Prime Pair Connection
For consecutive primes p_1 and p_2 where 5 <= p_1 and p_2 <= 1,000,003, find S(p_1, p_2), the smallest positive integer whose last digits equal p_1 and which is divisible by p_2. Find sum S(p_1, p_...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Consider the consecutive primes \(p_1 = 19\) and \(p_2 = 23\). It can be verified that \(1219\) is the smallest number such that the last digits are formed by \(p_1\) whilst also being divisible by \(p_2\).
In fact, with the exception of \(p_1 = 3\) and \(p_2 = 5\), for every pair of consecutive primes, \(p_2 > p_1\), there exist values of \(n\) for which the last digits are formed by \(p_1\) and \(n\) is divisible by \(p_2\). Let \(S\) be the smallest of these values of \(n\).
Find \(\displaystyle \sum S\) for every pair of consecutive primes with \(5 \le p_1 \le 1000000\).
Problem 134: Prime Pair Connection
Mathematical Foundation
Theorem 1. Let have digits, so . Then:
where , and is the modular inverse of modulo .
Proof. We seek the smallest positive integer with . Write for . The divisibility condition becomes:
Since is an odd prime greater than 5, , so the modular inverse exists. Solving: . Taking the least non-negative residue gives the unique .
We claim . If , then and . But (consecutive primes with ), so this is impossible.
Lemma 1. The modular inverse can be computed via Fermat’s little theorem as , or via the extended Euclidean algorithm.
Proof. By Fermat’s little theorem, for . Hence .
Editorial
For consecutive primes p1, p2 with 5 <= p1 <= 1000003, find S(p1,p2): smallest positive integer ending in p1 and divisible by p2. Sum all S values.
Pseudocode
primes = sieve_primes(1100000) // slightly above 1000003
total = 0
For each i in range where primes[i] >= 5 and primes[i+1] <= 1000003:
p1 = primes[i]
p2 = primes[i+1]
d = number_of_digits(p1)
pow10 = 10^d
inv = modular_inverse(pow10, p2) // via extended GCD or Fermat
k = (-p1 * inv) mod p2
S = p1 + k * pow10
total += S
Return total
Complexity Analysis
- Time: where . There are consecutive pairs, and each modular inverse costs via the extended Euclidean algorithm.
- Space: for the prime sieve.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
long long power_mod(long long base, long long exp, long long mod) {
long long result = 1;
base %= mod;
while (exp > 0) {
if (exp & 1) result = (__int128)result * base % mod;
base = (__int128)base * base % mod;
exp >>= 1;
}
return result;
}
int main() {
const int LIMIT = 1100000; // need primes a bit above 1000003
vector<bool> is_prime(LIMIT, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i < LIMIT; i++)
if (is_prime[i])
for (int j = i * i; j < LIMIT; j += i)
is_prime[j] = false;
vector<int> primes;
for (int i = 2; i < LIMIT; i++)
if (is_prime[i])
primes.push_back(i);
long long total = 0;
for (int i = 0; i + 1 < (int)primes.size(); i++) {
int p1 = primes[i];
int p2 = primes[i + 1];
if (p1 < 5) continue;
if (p1 >= 1000003) break;
// Number of digits of p1
long long pow10 = 1;
int temp = p1;
while (temp > 0) {
pow10 *= 10;
temp /= 10;
}
// k = (-p1 * modinv(pow10, p2)) mod p2
long long inv = power_mod(pow10, p2 - 2, p2);
long long k = (long long)(-(long long)p1 % p2 + p2) % p2 * inv % p2;
long long S = p1 + k * pow10;
total += S;
}
cout << total << endl;
return 0;
}
"""
Problem 134: Prime Pair Connection
For consecutive primes p1, p2 with 5 <= p1 <= 1000003, find S(p1,p2):
smallest positive integer ending in p1 and divisible by p2.
Sum all S values.
"""
def sieve(limit):
is_prime = [True] * limit
is_prime[0] = is_prime[1] = False
for i in range(2, int(limit**0.5) + 1):
if is_prime[i]:
for j in range(i*i, limit, i):
is_prime[j] = False
return [i for i in range(2, limit) if is_prime[i]]
primes = sieve(1100000)
total = 0
s_values = []
for i in range(len(primes) - 1):
p1 = primes[i]
p2 = primes[i + 1]
if p1 < 5:
continue
if p1 >= 1000003:
break
# Number of digits
pow10 = 10 ** len(str(p1))
# k = (-p1 * modinv(pow10, p2)) mod p2
inv = pow(pow10, p2 - 2, p2)
k = (-p1 * inv) % p2
S = p1 + k * pow10
total += S
if i < 100: # store first few for visualization
s_values.append((p1, p2, S))
print(total)