Ma · Mathematics

What Are the Last Two Nonzero Digits of 2016!?

Published By the 9to5Science editorial team

At a glance

  1. 1. Separate the factors

    Count the factors of 2 and 5 in 2016!, then pair them to remove all trailing zeros.

  2. 2. Keep the remaining factors

    Strip every factor of 2 and 5 from each integer from 1 through 2016, and multiply what remains.

  3. 3. Restore extra twos

    Multiply by the leftover power of 2 after removing the matched pairs.

  4. 4. Find the final digits

    Reduce modulo 100; the result is 64.

The ordinary value of 2016!2016! ends in many zeros, so reducing it modulo 100100 would only tell you that its last two digits are 0000. To find the last two nonzero digits, first remove all trailing zeros and then calculate the remaining number modulo 100100.

The answer is 04. The method below shows how to remove the zeros without ever needing to write out the enormous factorial.

Why trailing zeros come from pairs

A trailing zero comes from a factor of 10=2⋅510=2\cdot5. In a factorial there are more factors of 22 than factors of 55, so the number of trailing zeros is the number of factors of 55.

Write the prime-factor decomposition as

2016!=2a5bU,2016! = 2^{a}5^{b}U,

where UU contains all remaining factors and is divisible by neither 22 nor 55. Removing the bb factors of 1010 leaves

2016!10b=2a−bU.\frac{2016!}{10^b}=2^{a-b}U.

To get the last two digits of this zero-stripped number, calculate it modulo 100100.

Count the factors of 2 and 5

For a prime pp, the number of times pp divides n!n! is

⌊np⌋+⌊np2⌋+⌊np3⌋+⋯ .\left\lfloor\frac{n}{p}\right\rfloor+ \left\lfloor\frac{n}{p^2}\right\rfloor+ \left\lfloor\frac{n}{p^3}\right\rfloor+\cdots.

Each term counts multiples contributing at least that many copies of pp. For 2016!2016!:

a=1008+504+252+126+63+31+15+7+3+1=2010,b=403+80+16+3=502.\begin{aligned} a &= 1008+504+252+126+63+31+15+7+3+1=2010,\\ b &= 403+80+16+3=502. \end{aligned}

Thus there are 502502 trailing zeros, and after removing them the remaining power of 22 is 22010−502=215082^{2010-502}=2^{1508}.

Multiply the remaining factors modulo 100

For each integer from 11 to 20162016, strip out every factor of 22 and 55. Multiply the parts left over together; call that product UU. Then compute

21508U(mod100).2^{1508}U \pmod{100}.

A short Python program performs this calculation using only small integers:

n = 2016
twos = 0
fives = 0
unit = 1

for i in range(1, n + 1):
    x = i
    while x % 2 == 0:
        twos += 1
        x //= 2
    while x % 5 == 0:
        fives += 1
        x //= 5
    unit = (unit * x) % 100

answer = (unit * pow(2, twos - fives, 100)) % 100
print(twos, fives, answer)

The program prints 2010 502 4, so the last two digits are 04. Reducing the intermediate product modulo 100100 is safe: if two numbers are congruent modulo 100100, multiplying both by another integer preserves that congruence.

Check the result using smaller moduli

The calculation can also be checked by considering the moduli 44 and 2525, whose product is 100100. Since 1508≥21508\ge 2, the factor 215082^{1508} makes the zero-stripped factorial congruent to 00 modulo 44.

Modulo 2525, powers of 22 repeat every 2020 exponents, and 1508≡8(mod20)1508\equiv8\pmod{20}. The product of the factors left after stripping all twos and fives is U≡9(mod25)U\equiv9\pmod{25}, as computed by the loop. Therefore

21508U≡28⋅9≡6⋅9≡4(mod25).2^{1508}U\equiv 2^8\cdot9\equiv6\cdot9\equiv4\pmod{25}.

The number congruent to 00 modulo 44 and 44 modulo 2525 is 44 modulo 100100: among the numbers 4,29,54,794,29,54,79 modulo 100100, only 44 is divisible by 44. Written as two digits, the result is 04.

Common mistakes

  • Taking 2016! mod 1002016!\bmod 100 first. That gives 00, because the factorial already contains many trailing zeros. Remove the matching factors of 22 and 55 before reducing modulo 100100.
  • Counting only multiples of 55. Multiples of 2525, 125125, and higher powers contribute extra factors of 55. The floor-sum counts all of them.
  • Removing equal numbers of twos and fives from each individual integer. The factors need to be paired across the whole factorial. Strip all the twos and fives, multiply the remaining parts, and then restore the excess power of 22.
  • Keeping too much precision. There is no need to construct 2016!2016! or the zero-stripped factorial. Keeping the running product modulo 100100 gives the same last two digits.

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.