Sub topic 12
Trailing Zeros in Other Bases
The working model
Assume you know prime factorisation, factorials, and division with remainders.
A trailing zero in base 10 comes from a factor of 10:
\[ 10=2\times5 \]
Therefore, the number of trailing zeros equals the smaller of the number of factors 2 and factors 5.
The same idea works in every base. If the base is \(b\), factorise it into primes:
\[ b=p1^{a1}p2^{a2}\cdots pk^{ak} \]
One trailing zero in base \(b\) requires one complete group containing \(ai\) factors of each prime \(pi\). For a number \(N\), the number of trailing zeros in base \(b\) is
\[ \min\left( \left\lfloor\frac{v{p1}(N)}{a1}\right\rfloor, \left\lfloor\frac{v{p2}(N)}{a2}\right\rfloor, \ldots \right) \]
Here, \(v_p(N)\) means the exponent of prime \(p\) in the prime factorisation of \(N\).
For example, base 12 has the factorisation
\[ 12=2^2\times3 \]
Each base-12 zero requires two factors of 2 and one factor of 3.
Factorials in another base
For a factorial \(n!\), use Legendre's formula:
\[ v_p(n!)= \left\lfloor\frac n p\right\rfloor+ \left\lfloor\frac n {p^2}\right\rfloor+ \left\lfloor\frac n {p^3}\right\rfloor+\cdots \]
Stop when the power of \(p\) is greater than \(n\).
Consider the number of trailing zeros in \(100!\) when written in base 12.
First count the prime factors:
\[ v_2(100!)= \left\lfloor\frac{100}{2}\right\rfloor+ \left\lfloor\frac{100}{4}\right\rfloor+ \left\lfloor\frac{100}{8}\right\rfloor+ \left\lfloor\frac{100}{16}\right\rfloor+ \left\lfloor\frac{100}{32}\right\rfloor+ \left\lfloor\frac{100}{64}\right\rfloor \]
\[ =50+25+12+6+3+1=97 \]
Similarly,
\[ v_3(100!)= \left\lfloor\frac{100}{3}\right\rfloor+ \left\lfloor\frac{100}{9}\right\rfloor+ \left\lfloor\frac{100}{27}\right\rfloor+ \left\lfloor\frac{100}{81}\right\rfloor =33+11+3+1=48 \]
Base 12 needs two factors of 2 for every zero, so the available number of 2-pairs is
\[ \left\lfloor\frac{97}{2}\right\rfloor=48 \]
It also needs factors of 3, and there are 48 of them. Therefore,
\[ \text{zeros in base 12}=\min(48,48)=48 \]
A general algorithm
The following program factorises the base, counts each required prime factor in \(n!\), and takes the limiting quotient.
def prime_exponent_factorial(n, p):
total = 0
power = p
while power <= n:
total += n // power
power *= p
return total
def factorise(base):
factors = {}
divisor = 2
while divisor * divisor <= base:
while base % divisor == 0:
factors[divisor] = factors.get(divisor, 0) + 1
base //= divisor
divisor += 1
if base > 1:
factors[base] = factors.get(base, 0) + 1
return factors
def trailing_zeros_factorial_in_base(n, base):
factors = factorise(base)
limits = []
for prime, required_power in factors.items():
available = prime_exponent_factorial(n, prime)
limits.append(available // required_power)
return min(limits)
print(trailing_zeros_factorial_in_base(100, 12)) # 48A quick diagram
CAT decision rule
For a base \(b\):
- Factorise \(b\).
- For each prime in the base, calculate its exponent in \(n!\).
- Divide by the exponent required by the base.
- Take the minimum.
- Check that the answer is limited by a complete copy of every base factor.
Exercise
How many trailing zeros does 50! have when written in base 18?
Show answer
Since 18 = 2 × 3², calculate v₂(50!) = 47 and v₃(50!) = 22. The limits are 47 and floor(22/2) = 11, so 50! has 11 trailing zeros in base 18.