For a prime , let
be the sum of the digits of
when written in base-
form. Then the number of factors of
that divide
is
There are trailing zeroes in
. Since $100_{\text{ten}}=400_{\text{five}}$, there are
factors of
in
.
However, there are other zeros that occur earlier, making the total
:
$933262154439441526816992388562667\color{#C00000}{00}49\color{#C00000}{0}7159682643816214685929638952175999932299156\color{#C00000}{0}894146397615651828625369792\color{#C00000}{0}82722375825118521\color{#C00000}{0}
916864\color{#C00000}{000000000000000000000000}$
For a prime , let
be the sum of the digits of
when written in base-
form. Then the number of factors of
that divide
is
There are trailing zeroes in
. Since $100_{\text{ten}}=400_{\text{five}}$, there are
factors of
in
.
However, there are other zeros that occur earlier, making the total
:
$933262154439441526816992388562667\color{#C00000}{00}49\color{#C00000}{0}7159682643816214685929638952175999932299156\color{#C00000}{0}894146397615651828625369792\color{#C00000}{0}82722375825118521\color{#C00000}{0}
916864\color{#C00000}{000000000000000000000000}$
If you want to learn how many trailing zeroes are there in a factorial, it is
$\frac{N}{5} + \frac{N}{5}^2 + \frac{N}{5}^3 ..... \frac{N}{5}^{(m-1)} WHERE (\frac{N}{5}^m)<1$
You can learn here how this formula comes at
https://www.youtube.com/watch?v=wdz_KouqHx4
Videos
The total number of zeros in n! is given by sequence A027869 in the On-line Encyclopedia of Integer Sequences. There really seems to be no way to compute the total number of zeros in n! short of computing n! and counting the number of zeros. With a big int library, this is easy enough. A simple Python example:
import math
def zeros(n): return str(math.factorial(n)).count('0')
So, for example, zeros(100) evaluates to 30. For larger n you might want to skip the relatively expensive conversion to a string and get the 0-count arithmetically by repeatedly dividing by 10.
As you have noted, it is far easier to compute the number of trailing zeros. Your code, in Python, is essentially:
def trailing_zeros(n):
count = 0
p = 5
while p <= n:
count += n//p
p *= 5
return count
As a heuristic way to estimate the total number of zeros, you can first count the number of trailing zeros, subtract that from the number of digits in n!, subtract an additional 2 from this difference (since neither the first digit of n! nor the final digit before the trailing zeros are candidate positions for non-trailing zeros) and guess that 1/10 of these digits will in fact be zeros. You can use Stirling's formula to estimate the number of digits in n!:
def num_digits(n):
#uses Striling's formula to estimate the number of digits in n!
#this formula, known as, Kamenetsky's formula, gives the exact count below 5*10^7
if n == 0:
return 1
else:
return math.ceil(math.log10(2*math.pi*n)/2 + n *(math.log10(n/math.e)))
Hence:
def est_zeros(n):
#first compute the number of candidate postions for non-trailing zerpos:
internal_digits = max(0,num_digits(n) - trailing_zeros(n) - 2)
return trailing_zeros(n) + internal_digits//10
For example est_zeros(100) evaluates to 37, which isn't very good, but then there is no reason to think that this estimation is any better than asymptotic (though proving that it is asymptotically correct would be very difficult, I don't actually know if it is). For larger numbers it seems to give reasonable results. For example zeros(10000) == 5803 and est_zeros == 5814.
How about this then.
count = 0
s = str(fact)
for i in s:
if i=="0":
count +=1
print(count)
I was recently asked this pure math question in a technical interview with Apple (software engineer role). There are several techniques to solve this. What do you think?
One thing is clear. $2 \times 5 = 10$ and there is no other way to get 10 out of 2 prime numbers.
"trailing zeros" are the zeros at the end of the number.
For example: 3200 has 2 trailing zeros. The units and the tenths position.
One other thing is clear.
Multiplying a number by 10 adds a trailing zero to that number.
So in order to find the number of zeros at the tail of a number, you need to split that number into prime factors and see how many pairs (2, 5) you can form.
For example:
300 has 2 trailing zeros. Why?
because $300 = 3 \times 2 ^ 2 \times 5^2$.
So you get 2 pairs of (5, 2).
An other example:
$4000 = 2^5 \times 5 ^3 = 2 ^2 \times (2 \times 5) ^ 3$
Hence, 3 trailing zeros.
EDIT:
For your specific question. 100!.
$100! = 1 \times 2 \times 3 \times .... \times 100$.
Which is equivalent to splitting every number into prime factors like this:
$100! = 1 \times 2 \times 3 \times 2^2 \times 5 \times (3\times 2) .... \times (2 \times 5)^2$
You need to count how many 10's you can make out of these prime numbers.
As explained above, the only way to get a 10 is $2 \times 5$.
So you need to count how many 2's and how many 5s are among the prime factors above.
The 5s can appear only in numbers that are divisible by 5.
5, 10, 20, 25, 30, 35, 40, 45, 50 , 55, 60 , 65, 70, 75, 80, 85, 90, 95, 100
and the number of 5s in the numbers above are
1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2
Because for example
$15 = 3 \times 5$ so one single 5.
$50 = 2 \times 5 ^2$ so 2 fives.
Add them all up and you get 24 occurrences of 5.
Now you need to see if there are at least 24 occurrences of 2.
And they are because form 1 to 100 you have 50 even numbers (they all contain at least one 2).
This means that 100! can be written as
$2 ^ {50} \times 5 ^ {24} \times$ (some other factors that are never going to multiply to get to a multiple of 10 so we can ignore them).
We can split the line above (ignoring the factors that cannot multiply to reach a multiple of 10) to
$2 ^ {24} \times 2 ^{26} \times 5^{24} = (2 \times 5) ^{24} \times 2 ^{26}$
We can ignore again $2^{26}$ because that never ends with zeros (it ends with a 4 if I'm not mistaken)
Now you can see that 100! is basically a very large number that does not end with a 0 multiplied by $10 ^ {24}$. So you get 24 trailing zeros.
Trailing zeroes are as the name points zeroes in the end of the number. So 10 has 1 trailing zero. And because this is a question regarding base10 numbers, this is how you can represent any number with trailing zero - number0 = number x 10. And because 10 is actually 2 x 5 you need 2s and 5s. One 2 is enough to 'turn' all fives into zeroes. And you already have posted the number of 5s.