Skip to content

How Prime Exponents Determine Trailing Zeros in Factorials

  • Updated: August 26, 2026
📌

Complete guide: Prime Factorization

The number of trailing zeros in a factorial is determined by the exponents of the prime factors that can combine to form the number base. In decimal notation, each trailing zero requires one factor of 10, and each factor of 10 is formed from a pair of prime factors: 2 × 5.

Direct Answer

For a positive integer n, the number of trailing zeros in n! is:

v5(n!) = ⌊n / 5⌋ + ⌊n / 25⌋ + ⌊n / 125⌋ + ···

The sum stops when the next power of 5 is greater than n. Powers of 2 occur more often than powers of 5 in a factorial, so the exponent of 5 determines how many complete pairs of 2 and 5 are available.

What a Prime Exponent Measures

The exponent of a prime p in an integer tells how many times that integer can be divided by p before the result is no longer an integer. This exponent is commonly written as vp(m).

Prime Exponent

If m = pa × r, where r is not divisible by p, then vp(m) = a.

For example:

  • v2(40) = 3 because 40 = 23 × 5
  • v5(125) = 3 because 125 = 53
  • v3(54) = 3 because 54 = 2 × 33

A factorial contains many repeated prime factors because it multiplies every positive integer from 1 through n:

Factorial Definition

n! = 1 × 2 × 3 × ··· × n

The prime exponent in n! counts the total number of copies of that prime contributed by every factor in the product.

Why Decimal Trailing Zeros Depend on 2 and 5

A decimal integer ends in one zero when it is divisible by 10. It ends in two zeros when it is divisible by 102, and it ends in k zeros when it is divisible by 10k but not by 10k+1.

Since 10 = 2 × 5, every trailing zero needs one factor of 2 and one factor of 5. The available number of pairs is therefore:

Trailing Zeros in Base 10

Trailing zeros of n! = min(v2(n!), v5(n!))

Even numbers contribute factors of 2 frequently, while only multiples of 5 contribute factors of 5. For n ≥ 2, v2(n!) is greater than v5(n!). The smaller exponent is therefore the exponent of 5.

Factors of 2

Contributed by 2, 4, 6, 8, 10, 12, and many other terms.

Factors of 5

Contributed by 5, 10, 15, 20, 25, and other multiples of 5.

Legendre’s Formula for Prime Exponents

The exponent of any prime p in n! can be calculated with Legendre’s formula:

Legendre’s Formula

vp(n!) = ⌊n / p⌋ + ⌊n / p2⌋ + ⌊n / p3⌋ + ···

The floor symbols mean that only the whole-number part of each quotient is used. Each term counts a different layer of prime factors:

Multiples of p

⌊n / p⌋ counts numbers that contribute at least one factor of p.

Multiples of p²

⌊n / p2⌋ counts the additional factor contributed by each multiple of p2.

Higher Powers

Multiples of p3, p4, and higher powers contribute further copies of the same prime.

This layered count is needed because a number such as 25 contributes two factors of 5, while 125 contributes three.

Example: Trailing Zeros in 25!

For 25!, the exponent of 5 is:

Exponent of 5 in 25!

v5(25!) = ⌊25 / 5⌋ + ⌊25 / 25⌋

= 5 + 1 = 6

The first term counts the multiples 5, 10, 15, 20, and 25. The second term counts one additional factor of 5 from 25 because 25 = 52.

Therefore, 25! has 6 trailing zeros.

Common Counting Error

Counting only the multiples of 5 would give five zeros. That misses the second factor of 5 contained in 25.

Examples for Several Factorials

Prime-exponent calculations for decimal trailing zeros in selected factorials.
FactorialExponent CalculationTrailing Zeros
5!⌊5 / 5⌋ = 11
10!⌊10 / 5⌋ = 22
25!⌊25 / 5⌋ + ⌊25 / 25⌋ = 5 + 16
50!⌊50 / 5⌋ + ⌊50 / 25⌋ = 10 + 212
100!⌊100 / 5⌋ + ⌊100 / 25⌋ = 20 + 424
125!⌊125 / 5⌋ + ⌊125 / 25⌋ + ⌊125 / 125⌋ = 25 + 5 + 131

Why the Factorial Does Not Need to Be Expanded

Factorials grow too quickly for direct decimal expansion to be an efficient way to count ending zeros. Even a moderately large factorial may contain hundreds or thousands of digits.

Prime exponents avoid that expansion. The calculation depends only on integer division by powers of 5:

  • 5
  • 25
  • 125
  • 625
  • 3,125

Once a power of 5 exceeds n, every later quotient has a floor value of zero. The sum is therefore finite.

Math Note

The number of terms grows very slowly. A factorial as large as 1,000,000! requires division only by powers of 5 up to 390,625.

Trailing Zeros in Other Number Bases

The same idea works outside decimal notation. A trailing zero in base b represents a factor of b. The base must first be written as a prime factorization.

Suppose:

Prime Factorization of the Base

b = p1a1 × p2a2 × ··· × prar

The number of trailing zeros of n! in base b is:

Trailing Zeros in Base b

min(⌊vp₁(n!) / a1⌋, ⌊vp₂(n!) / a2⌋, …, ⌊vpᵣ(n!) / ar⌋)

Each zero requires the full collection of prime factors contained in one copy of the base. The prime factor that runs out first limits the result.

Example in Base 12

Base 12 has the prime factorization 12 = 22 × 3. For 10!:

  • v2(10!) = 5 + 2 + 1 = 8
  • v3(10!) = 3 + 1 = 4
  • ⌊8 / 2⌋ = 4 complete copies of 2²
  • ⌊4 / 1⌋ = 4 complete copies of 3

The smaller value is 4, so 10! ends in four zeros when written in base 12.

Prime Exponents and the Exact Power of 10

The trailing-zero count identifies the greatest power of 10 dividing the factorial. If z is the number of trailing zeros, then:

Exact Divisibility Statement

10z divides n!, but 10z+1 does not divide n!

For 100!, the count is 24. This means 1024 divides 100!, while 1025 does not. The factorial contains more than 24 factors of 2, but it contains exactly 24 factors of 5.

Trailing Zeros Are Not the Same as Internal Zeros

The prime-exponent method counts only consecutive zeros at the right end of a factorial. It does not count zeros appearing between nonzero digits.

Trailing Zero

A zero appearing after the final nonzero digit, such as the two zeros in 3,628,800.

Internal Zero

A zero surrounded by other digits. Its position is not determined by the exponent of 10 alone.

For example, 10! = 3,628,800. It contains two trailing zeros, matching v5(10!) = 2.

FAQ

Why Are Trailing Zeros Counted Using Factors of 5?

Each decimal zero requires a factor pair of 2 and 5. Factorials contain more factors of 2 than factors of 5, so the available factors of 5 limit the number of pairs.

How Many Trailing Zeros Does 0! Have?

Since 0! = 1, it has no trailing zeros. The prime exponents of 1 are all zero.

Why Does 25 Contribute Two Factors of 5?

The number 25 equals 52. It contributes one factor of 5 as a multiple of 5 and another as a multiple of 25.

Can a Factorial Have Exactly Five Trailing Zeros?

No positive integer factorial has exactly five decimal trailing zeros. The count changes from four for 24! to six for 25! because 25 contributes two factors of 5.

Does Legendre’s Formula Work for Primes Other Than 5?

Yes. Replacing 5 with any prime p gives the exponent of p in n!. The formula is used for divisibility, binomial coefficients, factorial ratios, and calculations in other number bases.

Can the Same Method Count Zeros in Base 2?

Yes. A trailing zero in base 2 requires one factor of 2, so the number of binary trailing zeros in n! is simply v2(n!).