Fast way to calculate n mod m where m is prime?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Despite the title, this question is usually about computing n! mod m efficiently when m is prime. The prime assumption matters because it gives you strong number-theory tools, and it also creates one very important shortcut: if n is at least m, then n! already contains m as a factor, so the result is immediately zero.
The First Shortcut: n >= m
If m is prime and n >= m, then:
- '
n!includes the factorm' - any multiple of
mis congruent to zero modulom
So this case is trivial:
Output:
For many inputs, this early exit is the whole optimization.
The Straightforward Method for n < p
When n < p, the simplest correct method is to multiply from 1 through n and reduce modulo p after each step.
This runs in O(n) time and is usually the right answer unless n is extremely large and still smaller than p.
The per-step modulo keeps intermediate values small, so you never need to build the full factorial.
A Faster Trick When n Is Close to p
If n is just a little smaller than p, Wilson's theorem can be useful. For a prime p:
That lets you rewrite n! in terms of the tail from n + 1 to p - 1. Instead of multiplying all the way from 1 to n, you multiply the shorter missing tail and divide it out modulo p.
Because modular division under a prime modulus is multiplication by an inverse, you can compute those inverses with fast exponentiation.
For n = 9 and p = 11, this is convenient because you only deal with the missing factor 10 instead of multiplying 1 through 9.
How to Choose Between the Two Methods
If n is small, the ordinary iterative product is simple and fast. If n is very close to p, Wilson's theorem can reduce the work from roughly n steps to roughly p - n steps.
So the practical rule is:
- if
n >= p, return0 - if
nis much smaller thanp, multiply from1ton - if
nis very close top, use Wilson's theorem and modular inverses
That gives you an efficient algorithm without unnecessary complexity.
Common Pitfalls
The biggest mistake is forgetting the n >= p shortcut. Once p appears as a factor of n!, the answer is zero, and no further work is needed.
Another pitfall is trying to compute n! directly before taking the modulus. Factorials grow extremely quickly, so the whole point is to reduce after each multiplication.
Developers also misuse Fermat's little theorem by applying modular inverses under a modulus that is not prime. The inverse formula using a^(p-2) only works cleanly in this context because the modulus is prime and the value being inverted is not divisible by it.
Finally, do not overcomplicate the problem. For one-off computations with moderate n, the simple linear loop is often the best real-world solution.
Summary
- If the goal is
n! mod pandn >= p, the result is immediately0. - For
n < p, the standard iterative product with per-step modulo runs inO(n). - When
nis close top, Wilson's theorem can reduce the amount of multiplication. - Modular inverses under a prime modulus can be computed with fast exponentiation.
- The simplest method is usually best unless
nis specifically near the prime modulus.

