Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 35s1 days 16 hours 1 minutes 35 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 35s1 days 16 hours 1 minutes 35 seconds.

Sign in to watch this lesson
Advanced - Number Theory · 4:21
n! mod p is simply 0 once n ≥ p, yet binomials mod p need factorials, so we split n! into two parts: the number of factors p it contains, counted by Legendre's formula ν_p(n!) = Σ ⌊n/p^i⌋, and what is left modulo p after removing them. Wilson's theorem turns every full block of p − 1 numbers into −1, which gives n! mod p without the p's in O(log_p n) after an O(p) precomputation, and together the two parts give C(n, k) mod p for huge n.