⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 18m 42s17 days 22 hours 18 minutes 42 seconds.
⚡ Build the skills to land top-paying quant jobs. Offer ends in 17d 22h 18m 42s17 days 22 hours 18 minutes 42 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.