Skip to main content
AMC 12 only

Factorization & Valuation

Legendre's formula finds the exact power of a prime dividing a factorial, without ever computing the (enormous) factorial itself.

The pp-adic valuation vp(n)v_p(n) is the exponent of prime pp innn’s prime factorization — how many times pp divides nn. This lesson is about computing vp(n!)v_p(n!), which shows up constantly in “how many trailing zeros” and “highest power of a prime dividing this factorial” problems.

Legendre’s formula

vp(n!)=k=1npk=np+np2+np3+v_p(n!) = \sum_{k=1}^{\infty} \left\lfloor \dfrac{n}{p^k} \right\rfloor = \left\lfloor\dfrac np\right\rfloor + \left\lfloor\dfrac{n}{p^2}\right\rfloor + \left\lfloor\dfrac{n}{p^3}\right\rfloor + \cdots

The sum is finite in practice — once pk>np^k > n, that term (and every later one) is zero, so you just stop once the floor becomes 0.

Trailing zeros

A trailing zero in n!n! comes from a factor of 10, i.e. a pair of factors 2 and 5. Since factors of 2 are far more abundant than factors of 5 in any factorial, the number of trailing zeros is limited entirely by v5(n!)v_5(n!) — you only ever need to apply Legendre’s formula withp=5p=5.

Worked example

Problem: How many trailing zeros does 100!100! have?

Solution: Compute v5(100!)v_5(100!):

1005+10025+100125=20+4+0=24\left\lfloor\dfrac{100}{5}\right\rfloor + \left\lfloor\dfrac{100}{25}\right\rfloor + \left\lfloor\dfrac{100}{125}\right\rfloor = 20+4+0 = 24

So 100!100! ends in 24 zeros.

Practice problems

1.Find v2(20!)v_2(20!).

2.How many trailing zeros does 50!50! have?

3.Find the largest power of 3 dividing 30!30!.

4.Why does 100!100! end in exactly 24 zeros rather than needing to check factors of 2 as well?