Sub topic 13
Remainders of Factorials and Factorial Sums
This chapter assumes you can reduce sums and products modulo n, can read congruence notation such as a ≡ b (mod n), and know Wilson's theorem from the previous chapter. It uses factorial structure to turn many apparently huge remainder questions into short calculations.
The Basic Fact: n! Contains Every Smaller Integer
The factorial n! is the product 1 × 2 × ... × n. Every integer from 1 to n is a factor of it. This gives the central rule:
- If m ≤ n, then m divides n!, so n! ≡ 0 (mod m).
Any factorial-heavy problem starts by asking where the terms become 0 for the modulus in question. After that point, those terms do not affect the remainder.
The rule is sufficient but not necessary. For composite moduli, n! can be divisible by m earlier than n = m, because m's prime factors may already be present in a smaller factorial.
| Modulus m | Smallest n with m dividing n! | Reason |
|---|---|---|
| 6 | 3 | 3! = 6 |
| 8 | 4 | 4! = 24 = 8 × 3 |
| 9 | 6 | 6! has 3² (from 3 and 6) |
| 10 | 5 | 5! has a 2 and a 5 |
| 12 | 4 | 4! = 24 = 12 × 2 |
| 100 | 10 | 10! has 2⁸ and 5² |
For a prime modulus p, the threshold is exactly n = p, because p does not appear as a factor until p itself.
Factorial Sums
A typical question asks for the remainder of 1! + 2! + 3! + ... + 100! on division by some m. Find the first factorial divisible by m and discard it and every later term.
Example: modulus 12
Since 4! = 24 is divisible by 12, every term from 4! onward is 0 modulo 12. Only the first three terms remain:
1! + 2! + 3! = 1 + 2 + 6 = 9
The remainder is 9.
Example: modulus 7
The terms from 7! onward vanish. Reduce the first six individually:
- 1! = 1
- 2! = 2
- 3! = 6
- 4! = 24 ≡ 3
- 5! = 120 ≡ 1
- 6! = 720 ≡ 6
The sum is 1 + 2 + 6 + 3 + 1 + 6 = 19 ≡ 5, so the remainder is 5.
Last digits and last two digits
Dividing by 10 or 100 asks for the last digit or the last two digits.
- 5! = 120 is divisible by 10, so from 5! onward every term ends in 0. The last digit of 1! + ... + n! is 3 for every n ≥ 4, because 1 + 2 + 6 + 24 = 33.
- 10! = 3628800 is divisible by 100, so from 10! onward every term ends in 00. You only need the sum up to 9!:
1 + 2 + 6 + 24 + 120 + 720 + 5040 + 40320 + 362880 = 409113
The last two digits are 13 for every n ≥ 9.
from math import factorial
def fact_sum_mod(n, m):
return sum(factorial(k) for k in range(1, n + 1)) % m
print(fact_sum_mod(100, 12)) # 9
print(fact_sum_mod(100, 7)) # 5
print(fact_sum_mod(100, 10)) # 3
print(fact_sum_mod(100, 100)) # 13Factorials Divided by a Prime Just Above n
Now the factorial is not divisible by the modulus. If p is prime and n < p, then p is not a factor of n!, and the remainder needs Wilson's theorem: (p − 1)! ≡ −1 (mod p).
Peel factors off (p − 1)! to reach the factorial you want, replacing each peeled factor with its negative form (p − 1 ≡ −1, p − 2 ≡ −2, and so on).
Example: 11! mod 13
12! = 12 × 11!, and 12 ≡ −1. So −11! ≡ −1, which gives 11! ≡ 1 (mod 13). In general, (p − 2)! ≡ 1 (mod p).
Example: 10! mod 13
12! = 12 × 11 × 10! ≡ (−1)(−2) × 10! = 2 × 10!. Wilson gives 2 × 10! ≡ −1 (mod 13). Multiply both sides by the inverse of 2 modulo 13, which is 7 because 2 × 7 = 14 ≡ 1. Then 10! ≡ −7 ≡ 6 (mod 13).
Example: 50! mod 53
52! ≡ (−1)(−2) × 50! = 2 × 50! ≡ −1 (mod 53). The inverse of 2 modulo 53 is 27, since 2 × 27 = 54 ≡ 1. So 50! ≡ −27 ≡ 26.
import math
print(math.factorial(12) % 13) # 12
print(math.factorial(6) % 7) # 6
print(math.factorial(11) % 13) # 1
print(math.factorial(10) % 13) # 6
print(math.factorial(50) % 53) # 26The method has three steps:
- Start from (p − 1)! ≡ −1.
- Write (p − 1)! as (p − 1)(p − 2)...(n + 1) × n!, and replace each factor p − k by −k.
- Solve for n! by dividing out the small product, using a modular inverse when it is not ±1.
Wilson Fails for Composite Moduli
Wilson's theorem is a statement about primes. For a composite n greater than 4, (n − 1)! is divisible by n, so (n − 1)! ≡ 0 rather than −1. The only exception is n = 4, where 3! = 6 ≡ 2.
import math
for n in [4, 6, 8, 9, 10, 12]:
print(n, math.factorial(n - 1) % n)
# 4 2
# 6 0
# 8 0
# 9 0
# 10 0
# 12 0How Many Times Does a Factor Divide n!?
Some questions ask for the highest power of m that divides n!, or whether n! is divisible by a given number. The tool is Legendre's formula. The exponent of a prime p in n! is
⌊n/p⌋ + ⌊n/p²⌋ + ⌊n/p³⌋ + ...
continuing until the terms reach 0.
Example: the highest power of 12 in 30!
Write 12 = 2² × 3.
- Exponent of 2 in 30!: 15 + 7 + 3 + 1 = 26, so there are 26 ÷ 2 = 13 copies of 2².
- Exponent of 3 in 30!: 10 + 3 + 1 = 14.
The limiting factor is 2², so the highest power is 12¹³.
Example: trailing zeros of 100!
Each zero needs one 2 and one 5. Fives are scarcer, so count them: 20 + 4 = 24 trailing zeros.
def legendre(n, p):
count = 0
while n:
n //= p
count += n
return count
print(legendre(30, 2), legendre(30, 3)) # 26 14
print(legendre(100, 5)) # 24Two Structural Identities
Products of consecutive integers
The product of any r consecutive integers is divisible by r!. So n(n + 1)(n + 2) is always divisible by 6, and the product of four consecutive integers is always divisible by 24. When a question gives you an expression like (n − 1)n(n + 1), this tells you the remainder modulo 6 is 0 without any computation.
Telescoping sums with k × k!
Since k × k! = (k + 1)! − k!, the sum telescopes:
1 × 1! + 2 × 2! + ... + n × n! = (n + 1)! − 1
For n = 10, the sum is 11! − 1. Now ask for remainders:
- Divided by 11: 11! ≡ 0, so the remainder is −1, which is 10.
- Divided by 13: 11! ≡ 1 by the Wilson argument above, so 11! − 1 ≡ 0.
Common Errors
- Stopping too late or too early. Check the first n where n! is divisible by m, not just n = m. For 12 the threshold is 4, not 12.
- Forgetting the early terms. The terms before the threshold still count, and you must reduce each one modulo m before adding.
- Misusing Wilson. It applies to prime moduli. For (p − 2)! or smaller, you must peel factors, not quote the theorem directly.
- Forgetting the inverse. From 2 × 10! ≡ −1 you cannot just divide by 2 in the usual sense. Multiply by the modular inverse instead.
- Counting only one prime in Legendre's formula. For composite m, compute the exponent of each prime factor and take the minimum after dividing by the required exponent.
Exercises
Exercise
Find the remainder when 1! + 2! + 3! + ... + 50! is divided by 15.
Show answer
5! = 120 is divisible by 15, so terms from 5! onward vanish. The remaining sum is 1 + 2 + 6 + 24 = 33, and 33 mod 15 = 3. The remainder is 3.
Exercise
Find the remainder when 16! is divided by 19.
Show answer
19 is prime, so 18! ≡ −1 (mod 19). Then 18! = 18 × 17 × 16! ≡ (−1)(−2) × 16! = 2 × 16!. So 2 × 16! ≡ −1. The inverse of 2 modulo 19 is 10, since 2 × 10 = 20 ≡ 1. Therefore 16! ≡ −10 ≡ 9. The remainder is 9.
Exercise
What is the highest power of 6 that divides 40!, and how many trailing zeros does 40! have?
Show answer
The exponent of 2 in 40! is 20 + 10 + 5 + 2 + 1 = 38. The exponent of 3 is 13 + 4 + 1 = 18. The limiting prime is 3, so the highest power is 6¹⁸. For trailing zeros, count the 5s: 8 + 1 = 9, so 40! has 9 trailing zeros.