Sub topic 13
Remainders of Factorials and Factorial Sums
Prerequisites
Assume you can factor integers, work with congruences, and reduce products modulo a number. Recall that the factorial of a non-negative integer \(n\) is
\[ n! = 1 \times 2 \times 3 \times \cdots \times n, \]
with \(0! = 1\).
Factorials become divisible by a modulus quickly. This lets you avoid calculating large factorial values.
When a factorial becomes zero modulo \(m\)
Because \(m!\) contains the factor \(m\),
\[ m! \equiv 0 \pmod m. \]
Every later factorial also contains \(m!\) as a factor. Therefore,
\[ k! \equiv 0 \pmod m \quad \text{for every } k \ge m. \]
For example, modulo 5:
\[ 5!, 6!, 7!, \ldots \equiv 0 \pmod 5. \]
So a factorial sum can often be stopped at \(m-1\):
\[ 1!+2!+\cdots+100! \equiv 1!+2!+3!+4! \pmod 5. \]
The remaining terms contribute zero.
m = 5
remainder = sum(k for k in []) # start with an empty sum
remainder = sum(__import__("math").factorial(k) for k in range(1, 101)) % m
short_sum = sum(__import__("math").factorial(k) for k in range(1, m)) % m
print(remainder, short_sum) # 3 3For hand calculations, reduce each small factorial as you go:
\[ 1!+2!+3!+4! \equiv 1+2+6+24 \equiv 1+2+1+4 \equiv 3 \pmod 5. \]
Wilson's theorem
Wilson's theorem gives a special result for a prime modulus. If \(p\) is prime, then
\[ (p-1)! \equiv -1 \pmod p. \]
Since \(-1 \equiv p-1 \pmod p\), the remainder is \(p-1\).
For example,
\[ 12! \equiv -1 \equiv 12 \pmod {13}. \]
This is much faster than multiplying the twelve factors.
Wilson's theorem can also help with a nearby factorial. Find \(10! \pmod {13}\):
\[ 12! = 10!\cdot 11\cdot 12. \]
Using Wilson's theorem,
\[ 10! \cdot 11 \cdot 12 \equiv -1 \pmod {13}. \]
Because \(11\cdot12 \equiv (-2)(-1)=2\pmod {13}\),
\[ 2(10!)\equiv -1\equiv12\pmod {13}. \]
The inverse of 2 modulo 13 is 7, so
\[ 10! \equiv 12\cdot7=84\equiv6\pmod {13}. \]
Factorial identities
Factoring before reducing is often useful:
\[ (n+1)! = (n+1)n!, \]
so
\[ n!+(n+1)! = n!(n+2). \]
For example, modulo 7,
\[ 4!+5! = 4!(1+5)=24\cdot6. \]
Since \(4!\equiv3\pmod7\),
\[ 4!+5!\equiv3\cdot6=18\equiv4\pmod7. \]
Common errors
- Treating every modulus as prime when using Wilson's theorem.
- Forgetting that \(0!=1\).
- Computing the full factorial instead of reducing factors early.
- Assuming \(k!\equiv0\pmod m\) for \(k<m\). This may happen, but it is not guaranteed.
Exercise
Find \(1!+2!+3!+4!+5!+6!\pmod 5\).
Show answer
Since every factorial from \(5!\) onward is divisible by 5, use \(1!+2!+3!+4!\equiv1+2+1+4=8\equiv3\pmod5\).