What Are the Last Two Nonzero Digits of 2016!?
At a glance
- 1. Separate the factors
Count the factors of 2 and 5 in 2016!, then pair them to remove all trailing zeros.
- 2. Keep the remaining factors
Strip every factor of 2 and 5 from each integer from 1 through 2016, and multiply what remains.
- 3. Restore extra twos
Multiply by the leftover power of 2 after removing the matched pairs.
- 4. Find the final digits
Reduce modulo 100; the result is 64.
The ordinary value of ends in many zeros, so reducing it modulo would only tell you that its last two digits are . To find the last two nonzero digits, first remove all trailing zeros and then calculate the remaining number modulo .
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 . In a factorial there are more factors of than factors of , so the number of trailing zeros is the number of factors of .
Write the prime-factor decomposition as
where contains all remaining factors and is divisible by neither nor . Removing the factors of leaves
To get the last two digits of this zero-stripped number, calculate it modulo .
Count the factors of 2 and 5
For a prime , the number of times divides is
Each term counts multiples contributing at least that many copies of . For :
Thus there are trailing zeros, and after removing them the remaining power of is .
Multiply the remaining factors modulo 100
For each integer from to , strip out every factor of and . Multiply the parts left over together; call that product . Then compute
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 is safe: if two numbers are congruent modulo , multiplying both by another integer preserves that congruence.
Check the result using smaller moduli
The calculation can also be checked by considering the moduli and , whose product is . Since , the factor makes the zero-stripped factorial congruent to modulo .
Modulo , powers of repeat every exponents, and . The product of the factors left after stripping all twos and fives is , as computed by the loop. Therefore
The number congruent to modulo and modulo is modulo : among the numbers modulo , only is divisible by . Written as two digits, the result is 04.
Common mistakes
- Taking first. That gives , because the factorial already contains many trailing zeros. Remove the matching factors of and before reducing modulo .
- Counting only multiples of . Multiples of , , and higher powers contribute extra factors of . 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 .
- Keeping too much precision. There is no need to construct or the zero-stripped factorial. Keeping the running product modulo gives the same last two digits.
Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.