ExamSaathi learning library · Government exams · SSC CGL quantitative foundations; published syllabus labelled 2024 · Quantitative aptitude

Trailing zeros in n! and the highest power of a prime in n!

Count the zeros at the end of n! from its factors of 5, find the highest power of any prime in n! by repeated division, and count the zeros at the end of a product from its 2s and 5s.

Status: original pilot lesson. Teacher review is pending. It is not a complete course or official exam material.

You will learn to

Before you start

The symbol n! (read 'n factorial') stands for the product of every whole number from 1 up to n. So 6! = 1 x 2 x 3 x 4 x 5 x 6 = 720, and 0! is defined as 1. Factorials grow very fast; 20! already has 19 digits, so questions about them are solved from prime factors rather than by multiplying out. The trailing zeros of a number are the zeros at its right-hand end. Each one is a factor of 10 = 2 x 5, so a number ends in as many zeros as the pairs of one 2 and one 5 that its prime factors can make. For example, 3,600 = 2^4 x 3^2 x 5^2 can make only two such pairs, so it ends in exactly two zeros.

Trailing zeros of n!: count the 5s

Up to any n there are at least as many multiples of 2 as of 5, of 4 as of 25, of 8 as of 125, and so on, so n! never has fewer 2s than 5s. That makes 5 the scarce factor: the number of trailing zeros of n! equals the number of 5s in its prime factors. Every multiple of 5 up to n supplies one 5, every multiple of 25 supplies a second, every multiple of 125 a third, and so on. So add the whole-number parts of n / 5, n / 25, n / 125 and onwards, stopping once the divisor is larger than n. For 60!, 60 / 5 = 12 and 60 / 25 = 2 remainder 10, which counts as 2, so 60! ends in 12 + 2 = 14 zeros. The extra 2 comes from 25 and 50, which each hold two 5s.

Repeated division for the highest power of a prime

The same idea gives the highest power of any prime p that divides n!. Instead of dividing n by p, p^2, p^3 and so on, use repeated division: divide n by p and drop the remainder, divide that quotient by p again, and carry on until the quotient is 0. The sum of the quotients is the answer. For the power of 3 in 40!, the quotients are 40 / 3 = 13, 13 / 3 = 4, 4 / 3 = 1 and then 0, so 3^18 divides 40! but 3^19 does not. This works because dividing the whole-number quotient of n / p by p again gives the same whole-number quotient as dividing n by p^2. The divisor in each step must be the prime itself, since the steps count the copies of that prime among the numbers 1, 2, 3 and so on up to n.

Zeros at the end of a product, and composite divisors

For a product of listed numbers, break each number into primes, total the 2s and the 5s separately, and take the smaller total. In 16 x 75 x 90 the 2s number 4 + 0 + 1 = 5 and the 5s number 0 + 2 + 1 = 3, so the product, 108,000, ends in 3 zeros. When many of the factors are multiples of 5, the 2s can run short, so count both. A composite divisor needs the same care. Write it as prime powers, count each prime in n!, divide each count by that prime's exponent in the divisor (dropping any remainder), and take the smallest result. For 6 = 2 x 3, n! never holds more 3s than 2s, so the count of 3s is the answer. For 12 = 2^2 x 3, either prime can be the one that runs short, so work out both.

Common mistakes

Stopping after n / 5: 100! ends in 24 zeros, not 20, because 25, 50, 75 and 100 each hold a second 5. Dividing by 10 instead of 5, which misses the zeros made by a 5 from one factor and a 2 from another. Rounding a quotient up: 74 / 25 is 2 remainder 24, so it adds 2, not 3. Running repeated division with a composite number: dividing 20 by 6 gives 3 and then 0, yet 20! holds 18 twos and 8 threes, so the highest power of 6 that divides it is 6^8. Counting every 0 among the digits: 15! = 1,307,674,368,000 has a 0 near its start, but only the three zeros at the end are trailing zeros. Counting only the 5s for a product that is not a factorial, where the 2s may be fewer.

A five-step routine

1. Write the divisor as prime powers; for trailing zeros the divisor is 10 = 2 x 5. 2. For n!, run repeated division by each of these primes and add the quotients. For a product of listed numbers, total each prime across the factors instead. 3. Divide each total by the prime's exponent in the divisor, dropping any remainder. 4. The smallest result is the answer; for n! and the divisor 10 it is simply the count of 5s. 5. Test the method on a case you can multiply out, such as 10! = 3,628,800, which has two 5s and ends in two zeros.

Worked examples

  1. How many zeros are there at the end of 380!?

    1. Each trailing zero needs one 2 and one 5, and 380! has more 2s than 5s, so count the 5s.
    2. Repeated division by 5: 380 / 5 = 76, 76 / 5 = 15 (the remainder 1 is dropped), 15 / 5 = 3, and 3 / 5 = 0.
    3. Add the quotients: 76 + 15 + 3 = 94.
    4. Meaning of each term: 76 multiples of 5, 15 multiples of 25 and 3 multiples of 125 (125, 250 and 375) lie between 1 and 380.

    Answer: 380! ends in 94 zeros.

  2. Find the highest power of 7 that divides 500!.

    1. 7 is a prime, so use repeated division by 7.
    2. 500 / 7 = 71 remainder 3, then 71 / 7 = 10 remainder 1, then 10 / 7 = 1 remainder 3, and 1 / 7 = 0.
    3. Add the quotients: 71 + 10 + 1 = 82.
    4. Check the terms: up to 500 there are 71 multiples of 7, 10 multiples of 49 and 1 multiple of 343, while 7^4 = 2,401 is larger than 500.

    Answer: The highest power of 7 that divides 500! is 7^82, so the exponent is 82.

  3. How many zeros are there at the end of 25 x 30 x 35 x 48 x 125?

    1. Break each factor into primes: 25 = 5^2, 30 = 2 x 3 x 5, 35 = 5 x 7, 48 = 2^4 x 3 and 125 = 5^3.
    2. Total the 5s: 2 + 1 + 1 + 0 + 3 = 7. Total the 2s: 0 + 1 + 0 + 4 + 0 = 5.
    3. Each zero needs one 2 and one 5, so only 5 pairs can form and two 5s are left over.
    4. Check: the product is 157,500,000, which ends in 5 zeros.

    Answer: The product ends in 5 zeros.

Practice checks

  1. How many zeros are there at the end of 150!?
  2. What is the largest whole number k for which 3^k divides 50!?
  3. What is the largest whole number k for which 12^k divides 30!?
  4. How many zeros are there at the end of 5 x 10 x 15 x 20 x ... x 100, the product of the twenty multiples of 5 from 5 to 100?
  5. What is the smallest whole number n for which n! ends in exactly 12 zeros?

Sources checked