The Prime Factorisation of Binomial Coefficients
The binomial coefficient C(10, 3) = 120 has prime factorisation 2^3 x 3 x 5, so the sum of the terms in its prime factorisation is 2^3 + 3 + 5 = 16. Find the sum of the terms in the prime factorisa...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
The binomial coefficient \(\displaystyle \binom {10} 3 = 120\).
\(120 = 2^3 \times 3 \times 5 = 2 \times 2 \times 2 \times 3 \times 5\), and \(2 + 2 + 2 + 3 + 5 = 14\).
So the sum of the terms in the prime factorisation of \(\displaystyle \binom {10} 3\) is \(14\).
Find the sum of the terms in the prime factorisation of \(\displaystyle \binom {20\,000\,000} {15\,000\,000}\).
Problem 231: The Prime Factorisation of Binomial Coefficients
Mathematical Foundation
Theorem (Legendre’s Formula). For any prime and positive integer , the -adic valuation of is
where denotes the sum of the digits of in base .
Proof. Among , exactly are divisible by , exactly are divisible by , and so on. Each multiple of contributes at least factors of to . Counting each factor of once yields . Writing in base as , a telescoping computation gives .
Lemma (Kummer’s Theorem). For primes and integers , the -adic valuation of equals the number of carries when adding and in base .
Proof. We have . Applying Legendre’s formula:
The quantity equals times the number of carries in the base- addition , since each carry reduces the digit sum by exactly .
Corollary. The exponent of prime in is
The desired answer is .
Editorial
We iterate over p in primes. We first generate the primes required by the search, then enumerate the admissible combinations and retain only the values that satisfy the final test.
Pseudocode
primes = sieve_of_eratosthenes(n)
S = 0
for p in primes:
e = 0
power = p
While power <= n:
e += floor(n / power) - floor(k / power) - floor((n - k) / power)
power *= p
S += e * p
Return S
Complexity Analysis
- Time: for the Sieve of Eratosthenes, plus for computing exponents across all primes. Total: .
- Space: for the sieve array.
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 int N = 20000000;
const int K = 15000000;
const int M = N - K; // 5000000
// Sieve of Eratosthenes
vector<bool> is_prime(N + 1, true);
is_prime[0] = is_prime[1] = false;
for (int i = 2; (long long)i * i <= N; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= N; j += i)
is_prime[j] = false;
}
}
// For each prime, compute exponent in C(N, K) using Legendre's formula
long long answer = 0;
for (int p = 2; p <= N; p++) {
if (!is_prime[p]) continue;
long long exp_val = 0;
long long pk = p;
while (pk <= N) {
exp_val += N / pk - K / pk - M / pk;
if (pk > N / p) break; // prevent overflow
pk *= p;
}
answer += exp_val * p;
}
cout << answer << endl;
return 0;
}
"""
Problem 231: The Prime Factorisation of Binomial Coefficients
Find the sum of terms in the prime factorisation of C(20000000, 15000000).
"""
def sieve(n):
"""Sieve of Eratosthenes returning list of primes up to n."""
is_prime = bytearray(b'\x01') * (n + 1)
is_prime[0] = is_prime[1] = 0
for i in range(2, int(n**0.5) + 1):
if is_prime[i]:
is_prime[i*i::i] = bytearray(len(is_prime[i*i::i]))
return [i for i in range(2, n + 1) if is_prime[i]]
def exponent_in_factorial(n, p):
"""Compute the exponent of prime p in n! using Legendre's formula."""
exp = 0
pk = p
while pk <= n:
exp += n // pk
pk *= p
return exp
def solve():
N = 20000000
K = 15000000
M = N - K # 5000000
primes = sieve(N)
answer = 0
for p in primes:
e = exponent_in_factorial(N, p) - exponent_in_factorial(K, p) - exponent_in_factorial(M, p)
if e > 0:
answer += e * p
print(answer)
if __name__ == "__main__":
solve()