Sub topic 12
Trailing zeros in other bases
This chapter assumes you can already count trailing zeros of n! in base 10 using the floor-sum of powers of 5. It also assumes you can factorise an integer into primes and can read a number's prime factorisation. Here you extend the method to bases such as 12, 8, 36 and 30.
The base-10 method, restated
A number ends in k zeros in base 10 exactly when 10^k divides it and 10^(k+1) does not. Since 10 = 2 × 5, the divisibility 10^k | N means that 2^k | N and 5^k | N. So the zero count is min(exponent of 2, exponent of 5).
The same argument works in any base b. A number N written in base b ends in k zeros when b^k divides N and b^(k+1) does not. The only new work is that b may contain repeated primes or more than two primes, so you must handle exponents carefully.
The general rule
Factorise the base: b = p1^a1 × p2^a2 × ... × pm^am. Let ei be the exponent of the prime pi in N. Then b^k divides N exactly when, for every prime, ai × k ≤ ei. This gives:
trailing zeros in base b = min over i of floor(ei / ai)
Two points deserve emphasis:
- You divide each prime's exponent by that prime's exponent in the base, and you round down.
- You take the minimum over all primes of the base. The prime with the smallest quotient limits the count.
For a factorial, e_i comes from the formula you already know, applied to any prime p:
e = floor(n/p) + floor(n/p^2) + floor(n/p^3) + ...
This formula is usually called Legendre's formula. For p = 5 it is the zero-count you met in base 10.
Worked example: 100! in base 12
Since 12 = 2^2 × 3, you need the exponents of 2 and 3 in 100!.
- Exponent of 2: 50 + 25 + 12 + 6 + 3 + 1 = 97.
- Exponent of 3: 33 + 11 + 3 + 1 = 48.
Now divide by the exponents in the base:
- For 2: floor(97 / 2) = 48.
- For 3: floor(48 / 1) = 48.
The minimum is 48, so 100! ends in 48 zeros in base 12. Here both primes are equally limiting, which is why the quotient step matters. If you forgot to divide by 2 you would still get min(97, 48) = 48 by luck. The next examples show where that shortcut fails.
One factorial, many bases
Using the same two exponents (97 for 2, 48 for 3) and the exponent 24 for 5 in 100!, you can fill in a whole table.
| Base | Factorisation | Quotients | Trailing zeros of 100! |
|---|---|---|---|
| 6 | 2 × 3 | 97, 48 | 48 |
| 8 | 2^3 | floor(97/3) = 32 | 32 |
| 12 | 2^2 × 3 | 48, 48 | 48 |
| 15 | 3 × 5 | 48, 24 | 24 |
| 20 | 2^2 × 5 | 48, 24 | 24 |
| 24 | 2^3 × 3 | 32, 48 | 32 |
| 30 | 2 × 3 × 5 | 97, 48, 24 | 24 |
| 36 | 2^2 × 3^2 | 48, 24 | 24 |
Read the table for patterns:
- In base 8 and base 24, the prime 2 is the limiting prime, even though 2 is by far the most plentiful prime in a factorial. The factor 2^3 in the base uses up 2s three at a time.
- In base 36 the exponent of 3 is the limit, because 3^2 uses up 3s two at a time. The rule "the larger prime is always the bottleneck" fails here and in bases 8 and 24.
- A base that is a prime power, such as 8, needs only that one prime.
A small case you can check by hand
Take 10! = 3,628,800 = 2^8 × 3^4 × 5^2 × 7. In base 12 the quotients are floor(8/2) = 4 and floor(4/1) = 4, so the answer is 4 zeros. Check: 12^4 = 20,736 and 3,628,800 / 20,736 = 175. The number 175 is not divisible by 12 (it is not divisible by 4 or by 3), so the zero count is exactly 4.
When the number is not a factorial
The rule works for any positive integer N whose prime exponents you know. For a product or power, add the exponents first.
Example: how many trailing zeros does 12^5 × 18^3 have in base 6?
- 12^5 = (2^2 × 3)^5 = 2^10 × 3^5.
- 18^3 = (2 × 3^2)^3 = 2^3 × 3^6.
- The product is 2^13 × 3^11.
- Base 6 = 2 × 3, so the quotients are 13 and 11. The minimum is 11.
The answer is 11 trailing zeros in base 6.
Checking the rule in code
The function below applies the rule. It also includes a brute-force check that divides the factorial by the base until it no longer divides evenly. If the two methods agree for many small cases, the rule is implemented correctly.
from math import factorial
def factorize(b):
factors = {}
p = 2
while p * p <= b:
while b % p == 0:
factors[p] = factors.get(p, 0) + 1
b //= p
p += 1
if b > 1:
factors[b] = factors.get(b, 0) + 1
return factors
def legendre(n, p):
# Exponent of the prime p in n!
count = 0
q = p
while q <= n:
count += n // q
q *= p
return count
def zeros_factorial_base(n, b):
return min(legendre(n, p) // a for p, a in factorize(b).items())
def zeros_brute(n, b):
value = factorial(n)
count = 0
while value % b == 0:
value //= b
count += 1
return count
for n in range(1, 60):
for b in range(2, 40):
assert zeros_factorial_base(n, b) == zeros_brute(n, b), (n, b)
print(zeros_factorial_base(100, 12)) # 48
print(zeros_factorial_base(100, 8)) # 32
print(zeros_factorial_base(100, 36)) # 24
print(zeros_factorial_base(100, 30)) # 24Because the loop uses the three-line pattern count += n // q, it matches the base-10 function you already know. The only differences are that the prime is a parameter and that each result is divided by the prime's exponent in the base.
Edge cases and common errors
- A prime in the base that exceeds n. If the base has a prime factor p with p > n, then p does not divide n! at all, so e = 0 and the zero count is 0. For example, 5! in base 7 has no trailing zeros.
- Forgetting to round down. The quotient e / a must be floored. With e = 97 and a = 2 the usable count is 48, not 48.5.
- Forgetting that the base's exponents multiply. Base 36 is 2^2 × 3^2, not 2 × 3. Treating it as base 6 gives 48 instead of 24 for 100!.
- Mixing up "ends in zeros in base b" with "is divisible by b". The count is the highest power of b that divides N, not whether b divides N once.
- Using a composite quotient shortcut. Do not compute the zero count in base 6 and "square" it for base 36. Always return to the prime factorisation.
Choosing which primes to compute
For a factorial in a CAT question, the base is usually small, such as 6, 8, 12, 15, 18, 20, 24, 30 or 36. Two habits save time:
- Skip primes that are clearly not limiting. In n! the exponent of 2 is about twice the exponent of 3 and four times the exponent of 5, so in a base like 20 (2^2 × 5) the exponent of 5 is the likely limit. Confirm by computing floor(e/a) for both if the numbers are close.
- When one prime appears in the base with a large exponent, such as 2^5 = 32, the quotient for that prime shrinks fast and may become the limit. Compute it rather than guessing.
Exercises
Exercise
How many trailing zeros does 75! have in base 18?
Show answer
Since 18 = 2 × 3^2, you need the exponents of 2 and 3 in 75!. The exponent of 2 is 37 + 18 + 9 + 4 + 2 + 1 = 71. The exponent of 3 is 25 + 8 + 2 = 35. The quotients are floor(71/1) = 71 and floor(35/2) = 17. The minimum is 17, so 75! ends in 17 zeros in base 18.
Exercise
How many trailing zeros does 200! have in base 80?
Show answer
Since 80 = 2^4 × 5, you need the exponents of 2 and 5 in 200!. The exponent of 2 is 100 + 50 + 25 + 12 + 6 + 3 + 1 = 197, so the quotient is floor(197/4) = 49. The exponent of 5 is 40 + 8 + 1 = 49, so the quotient is 49. Both quotients are 49, so 200! ends in 49 zeros in base 80.
Exercise
A number equals 2^9 × 3^7 × 5^2. How many trailing zeros does it have in base 12, and how many in base 10?
Show answer
In base 12 = 2^2 × 3, the quotients are floor(9/2) = 4 and floor(7/1) = 7, so the answer is 4. In base 10 the answer is min(9, 2) = 2.