Maths & numbers

Binomial coefficient modulo composite number calculator

A binomial coefficient modulo a composite number cannot generally be computed by dividing factorial residues.

Your numbers

Integer 0–10^18.
Integer 0–min(n,10,000).
Integer 2–1,000,000,000, prime or composite.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

A binomial coefficient modulo a composite number cannot generally be computed by dividing factorial residues. The denominator may share a prime factor with the modulus and have no inverse. This calculator tracks those prime factors separately for each prime-power component, divides only coprime unit parts, and recombines the residues by the Chinese remainder theorem. It can therefore return a small exact residue without constructing the entire binomial coefficient.

THE FORMULAC(n,k) mod m; factor m=∏p^e, remove p factors from numerator/denominator, compute each prime-power residue, then apply CRT.

A worked example

C(10,3)=120, so its residue modulo 12 is zero. The modulus splits into 4 and 3. The coefficient contains three factors of 2 and one factor of 3, so it is divisible by both prime powers. Both component residues are zero and Chinese remaindering returns zero modulo 12. This is a legitimate residue, not a failed calculation or an indication that C(10,3) itself is zero.

Good to know

Use 0≤n≤10^18, 0≤k≤min(n,10,000) and 2≤m≤10^9. The smaller complementary count min(k,n−k) is used internally. Arithmetic is exact, including the valuation of each prime in the binomial. The output is a residue in 0…m−1, not the full count or a probability. A chosen count larger than n is rejected.

Why ordinary modular division breaks

For C(6,3) modulo 8, the exact answer is 20 modulo 8, or 4. But 3! modulo 8 is 6, and 6 has no multiplicative inverse modulo 8. Substituting a nonexistent inverse into n! divided by k!(n−k)! is therefore invalid even though the integer binomial coefficient is well defined. Cancellation must be handled before modular division.

For each prime p dividing m, the engine removes every p factor from the numerator and denominator products. Their difference gives the valuation v_p(C(n,k)); the remaining denominator is coprime to p^e and can safely be inverted. If the valuation is at least e, that component residue is zero. Otherwise the stripped quotient is multiplied by p raised to the retained valuation.

Prime valuations make both component residues zero in the worked exampleC(10,3) = 120120 mod 4 = 0120 mod 3 = 0CRT: 120 mod 12 = 0
C(10,3)=120 has 2-adic valuation 3 and 3-adic valuation 1. Thus 120 is zero modulo 4 and modulo 3; the unique combined residue modulo 12 is zero.

The Combinations calculator returns the full exact selection count; this page instead returns its residue while preserving prime-factor cancellation.

Use independent prime-power components

The component moduli p^e are pairwise coprime even when m itself is composite. Each component therefore has a unique contribution to the final residue modulo m. The table shows the prime, exponent, prime-power modulus, binomial valuation and residue, making it possible to compare the intermediate computations independently.

A residue of zero in one component does not force the final residue to be zero in every component. For C(6,3) modulo 12, the residue is zero modulo 4 but two modulo 3, giving eight modulo 12. Checking the final answer against every component is a stronger diagnostic than merely seeing a number within the expected output range.

Large n and moderate k are a deliberate input family

The calculation multiplies k consecutive numerator factors and k denominator factors, after replacing k by the smaller of k and n−k. Its cost consequently depends mainly on that smaller count and the prime-power factors of m, not on expanding all factorials through n. This makes n=10^18 practical when the chosen count is modest.

The 10,000 cap applies to the entered k, even if its complement would be smaller. This keeps the public input contract simple and predictable. If k=n is a large number, use k=0 for the symmetric coefficient rather than relying on an undocumented exception. For n=0,k=0, the empty selection has count one, and its residue is one for every supported modulus.

A few questions, answered

Can the modulus be prime?

Yes. Prime moduli are a one-component special case of the same exact procedure; there is no separate page for a different modulus type.

Why does zero not mean an impossible selection?

A positive binomial coefficient can be divisible by the modulus. Its modular residue is then zero even though many selections exist.

Find your calculator

Start typing to explore the library.