Largest Integer Divisible by Two Primes
For two distinct primes p and q, let M(p, q, N) be the largest positive integer <= N whose only prime factors are p and q (both must appear), or 0 if no such integer exists. Find sum M(p, q, 10^7)...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
The largest integer \(\le 100\) that is only divisible by both the primes \(2\) and \(3\) is \(96\), as \(96=32\times 3=2^5 \times 3\). For two
E.g. \(M(2,3,100)=96\).
\(M(3,5,100)=75\) and not \(90\) because \(90\) is divisible by \(2\), \(3\) and \(5\).
Also \(M(2,73,100)=0\) because there does not exist a positive integer \(\le 100\) that is divisible by both \(2\) and \(73\).
Let \(S(N)\) be the sum of all distinct \(M(p,q,N)\). \(S(100)=2262\).
Find \(S(10\,000\,000)\).
Problem 347: Largest Integer Divisible by Two Primes
Mathematical Foundation
Theorem 1 (Structure of --smooth numbers). The set of positive integers whose only prime factors are and is exactly . The subset with both primes present is .
Proof. By the fundamental theorem of arithmetic, the prime factorization of any integer in this set involves only and . Requiring both to appear forces and .
Lemma 1 (Finite Enumeration). For fixed with , the number of integers with is at most . For , this is .
Proof. The exponent ranges from 1 to and for each , ranges from 1 to . Both bounds are at most .
Theorem 2 (Correctness of Exhaustive Search). , and this maximum is attained since the set is finite and non-empty (as ).
Proof. The set is non-empty because by hypothesis. It is finite because grows exponentially. A finite non-empty subset of has a maximum.
Lemma 2 (Prime Pair Bound). The number of prime pairs with and is .
Proof. For each prime , the number of primes is by the prime number theorem. Summing over primes gives .
Editorial
We but p * next_prime > N means no valid q. Finally, find max p^a * q^b <= N with a >= 1, b >= 1.
Pseudocode
but p * next_prime > N means no valid q
Find max p^a * q^b <= N with a >= 1, b >= 1
For this power_p = p^a, find max b
Complexity Analysis
- Time: . There are prime pairs, each requiring work to enumerate powers.
- 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;
int main() {
const long long N = 10000000;
// Sieve of Eratosthenes
vector<bool> is_prime(N + 1, true);
is_prime[0] = is_prime[1] = false;
for (long long i = 2; i * i <= N; i++) {
if (is_prime[i]) {
for (long long j = i * i; j <= N; j += i)
is_prime[j] = false;
}
}
vector<long long> primes;
for (long long i = 2; i <= N; i++) {
if (is_prime[i]) primes.push_back(i);
}
long long total = 0;
for (int i = 0; i < (int)primes.size(); i++) {
long long p = primes[i];
if (p * p > N) break; // p < q, so p*q > p*p
for (int j = i + 1; j < (int)primes.size(); j++) {
long long q = primes[j];
if (p * q > N) break;
long long best = 0;
// Enumerate p^a * q^b with a>=1, b>=1
long long pa = p;
while (pa * q <= N) {
long long val = pa * q;
// Multiply by q as much as possible
while (val * q <= N) val *= q;
best = max(best, val);
pa *= p;
}
total += best;
}
}
cout << total << endl;
return 0;
}
def solve():
N = 10**7
# Sieve of Eratosthenes
is_prime = [False, False] + [True] * (N - 1)
for i in range(2, int(N**0.5) + 1):
if is_prime[i]:
for j in range(i * i, N + 1, i):
is_prime[j] = False
primes = [i for i in range(2, N + 1) if is_prime[i]]
total = 0
for i, p in enumerate(primes):
if p * p > N:
break
for j in range(i + 1, len(primes)):
q = primes[j]
if p * q > N:
break
best = 0
pa = p
while pa * q <= N:
val = pa * q
while val * q <= N:
val *= q
best = max(best, val)
pa *= p
total += best
print(total)
if __name__ == "__main__":
solve()