ARITHMETIC WORKSHEETS

Checking decimal trailing zeroes in a factorial

Each trailing zero in a factorial comes from a factor of 10, which needs one 2 and one 5. This guide counts the factors of five in 25 factorial, finds six trailing zeroes and extends the rule to larger numbers.

Where a trailing zero comes from

A trailing zero in a decimal number means the number is divisible by 10. Since 10 is 2 times 5, each trailing zero consumes one factor of 2 and one factor of 5 from the number's prime factorization. The total count of trailing zeroes is therefore the smaller of two counts: how many 2s the number contains and how many 5s. For a factorial n!, which multiplies every whole number from 1 to n, the 2s are far more numerous than the 5s, because every second number contributes at least one 2 and only every fifth number contributes a 5. The 5s are the bottleneck, so the trailing zero count equals the number of factors of 5 in n!. The Factorial calculator shows the exact integer so you can compare the rule with the digits.

Counting multiples of five in 25 factorial

The numbers from 1 to 25 that are multiples of 5 are 5, 10, 15, 20 and 25, which is five numbers. Each gives at least one factor of 5. But 25 equals 5 times 5, so it supplies a second factor. The total number of 5s is therefore 5 plus 1, or 6. A formula expresses this: take the integer part of 25 divided by 5, which is 5, then the integer part of 25 divided by 25, which is 1, and add them. The sum is 6. The calculator returns 25! as 15511210043330985984000000, which ends in exactly six zeroes. The final step in the worked cases adds 5 and 1 with the Basic calculator to show where the 6 comes from. A quick way to see the extra factor is to write 25 as 5 times 5, so that it appears once in the list of multiples of 5 but carries two factors of 5.

Legendre's formula

The counting pattern is a special case of a formula attributed to Adrien-Marie Legendre. The exponent of a prime p in n! equals the sum of the integer parts of n divided by p, n divided by p squared, n divided by p cubed, and so on until the terms reach zero. For p equal to 5 and n equal to 25, the terms are 5, 1 and then 0, for a total of 6. The first term counts multiples of 5, the second counts multiples of 25 which contribute an extra factor, the third would count multiples of 125 and so on. Each higher power of 5 adds one more factor for numbers that are multiples of it. Using the formula avoids listing the numbers.

Why the factors of two are not the limit

Apply the same formula to the prime 2 for n equal to 25. The terms are 12, 6, 3 and 1, from 25 divided by 2, 4, 8 and 16, and the sum is 22. So 25! contains 22 factors of 2 and only 6 factors of 5. Pairing each 5 with a 2 gives 6 tens, and 16 factors of 2 remain unused. Writing the factorial as 2 to the 22 times 5 to the 6 times other primes, the trailing zeroes come from 10 to the 6, which is the smaller of the two counts. This pattern holds for every n: the factors of 2 always outnumber the factors of 5, so counting the 5s alone is enough. It is worth checking once, since it justifies ignoring the 2s in all later problems.

Smaller factorials as a sanity check

Test the rule on factorials small enough to read. 5 factorial is 120, with one trailing zero, and the rule gives the integer part of 5 divided by 5, which is 1. 10 factorial is 3,628,800, with two trailing zeroes, and the rule gives 2. 20 factorial is 2,432,902,008,176,640,000, which ends in four zeroes, and the rule gives 4 from 20 divided by 5, since 25 is larger than 20. The count stays at 4 for 20, 21, 22, 23 and 24 and jumps to 6 at 25, because the multiple 25 adds two factors of 5 at once. The jump from 4 to 6 is a pattern worth noting. The trailing zeroes do not increase by one at every multiple of five, because some multiples contribute extra factors.

Larger examples

Apply the formula without computing the factorial. For 100, the terms are 20 from 100 divided by 5 and 4 from 100 divided by 25, and the next term, 100 divided by 125, is zero. So 100 factorial ends in 24 zeroes. For 125, the terms are 25, 5 and 1, giving 31. For 1,000, the terms are 200, 40, 8 and 1, giving 249. The factorial of 1,000 has thousands of digits, but the count of trailing zeroes comes from four small divisions. This efficiency is why the method appears in programming exercises. The calculator is helpful for numbers whose factorial it can display exactly, and the formula carries on where the displayed integer would be impractical to inspect digit by digit.

Trailing zeroes are not all the zeroes

The question concerns zeroes at the end of the decimal representation. The digits of 25! also contain zeroes elsewhere: 15511210043330985984000000 has zeroes inside it too, a pair after the digits 1551121 and a single zero after 4333, followed by the six at the end. Those interior zeroes are ordinary digits that arise from the arithmetic and have no simple formula. The count by factors of 5 applies only to the run of zeroes after the last nonzero digit. Another subtlety is the base. In base 10, a trailing zero needs 2 times 5. In another base, such as base 12 which is 2 squared times 3, the limiting prime would be different. The title and the worked cases refer to the decimal representation only.

Exact integers and the size of factorials

Factorials grow very quickly. 20 factorial is about 2.4 times 10 to the 18, and 25 factorial is about 1.55 times 10 to the 25. Ordinary double-precision floating point holds integers exactly only up to about 9 times 10 to the 15, so 25! cannot be stored exactly in that form, and a spreadsheet would show a rounded value with trailing zeroes that mean nothing. The calculator here returns the exact integer, so the six zeroes at the end are real. If you compute a large factorial in a tool that uses floating point, do not count its zeroes. Use the factor-of-five rule instead, which needs only small integers and is exact.

Common mistakes

The most frequent error is to count only the multiples of 5 and stop, which would give 5 for 25 factorial instead of 6. Another is to divide the number by 10 or by 5 in a way that ignores integer parts, such as using 25 divided by 5 equal to 5.0 and then adding fractional parts from higher powers. Take whole numbers at each step. A third error is to confuse the number of trailing zeroes with the number of digits, which is a separate question. A fourth is to apply the rule to n equal to 0 or to negative inputs. The factorial of 0 is 1, which has no trailing zeroes, and negative integers have no factorial in this sense. The rule here assumes n is a positive integer. Dropping the extra factor at 25, 50, 75 and 100 is the usual reason an answer is off by a small amount for round numbers such as 50, where the correct count is 12 and a naive count gives 10.

A running total by repeated division

The sum of integer parts can be computed by dividing repeatedly. Start with n, divide by 5 and discard any remainder, add the quotient to a running total, then divide the quotient by 5 again and continue until the quotient is zero. For 25 the successive quotients are 5, then 1, then 0, and the total is 6. For 100 they are 20, 4 and 0, for a total of 24. For 1,000 they are 200, 40, 8, 1 and 0, for 249. This loop is equivalent to Legendre's formula because dividing the quotient by 5 is the same as dividing the original by 25, then 125, and so on, with integer parts taken at each step. A short program needs only a few lines, and the calculation can be done by hand for numbers of any size.

Some zero counts never occur

The count of trailing zeroes jumps as n grows, so some values are skipped. For n from 20 to 24 the count is 4, and at n equal to 25 it becomes 6, so no factorial ends in exactly five zeroes. The same skip happens at each multiple of 25, and larger skips occur at multiples of 125. The smallest n with at least six trailing zeroes is 25, because 24 factorial has only four. If a puzzle asks for a factorial that ends in exactly five zeroes, the answer is that none exists, which follows directly from the counting rule. The reverse question, finding the smallest n for a target count, is solved by trying the multiples of five and checking the running total. The calculator can confirm any single case by displaying the exact integer.

The same counting idea for other products

The reasoning is not specific to factorials. For any product of whole numbers, count the factors of 2 and the factors of 5 across all the terms and take the smaller total. Consider the product 20 times 35 times 15 times 16. The factors of 5 are one each in 20, 35 and 15, for a total of 3. The factors of 2 are two in 20 and four in 16, for a total of 6. The smaller count is 3, so the product should end in three zeroes. Multiplying out, 20 times 35 is 700, times 15 is 10,500, and times 16 is 168,000, which does end in three zeroes. When the 5s are the scarcer prime, they decide the count, as in factorials. In a product where the 2s are scarcer, the 2s decide it, so check both counts rather than assuming.

Reproduce the checks

Exact factorial

Open the Factorial calculator and enter:

Non-negative integer
25

Expected output:

Exact factorial

15511210043330985984000000

The complete integer retains its six trailing zeroes.

Factor-of-five count

Open the Basic calculator and enter:

First number
5
Operation
add
Second number
1

Expected output:

Result

6

The extra factor from twenty-five is included.

References and implementation scope

The examples describe the behavior of the linked Computandum tools. A successful local check does not establish that another application accepts the same format or applies the same limits.

Related tasks