Division that is not exact
transcript
Count n choose k modulo a prime. Store the factorials, already reduced. Then divide them as plain integers. It looks fine on small tests. Does it hold?
Twelve factorial is four hundred seventy-nine million. That is below the modulus, so nothing is reduced. So the division is exact. Small tests pass.
Thirteen factorial is above the modulus. Its stored value is reduced. Dividing gives zero, not thirteen.
A reduced number is only a remainder. It need not divide evenly any more. Integer division then rounds to nonsense. Division needs an inverse.
Find the inverse with a fast power. Multiply by it. Never divide. Reduce after every product. Now every count is right.
Small tests pass and mislead you. Always try thirteen and more. Compare with exact big integers.
Reduction removes the factors. Division stops being exact. Multiply by the inverse instead. That is the safe rule. Full breakdown in the lecture.