Problem One solution:
We know the number of factorial results at the end of the n is 0, which means that we do the multiplication of n from 1 when the number of 10
we can decompose this, that is, the decomposition of the Genesis from 0 to N, and then multiply these by the number of 10? Actually, we just have to figure out how many of them are 5? because in these decomposed factors, can produce 10 can only be multiplied by 5 and 2, because the number of 2 is greater than the number of 5, so
we just have to count the number 5. So the problem is that the numbers from 1 to n are decomposed into factors, and these factorsthe number of 5.
Python code
def factorialnumberofzero(v_value):
countoffive = 0
for num in range(1, v_value + 1):
value = num
while ((value % 2) == 0) and value:
value /= 2
countoffive += 1
print countoffive
return countoffive
solution to problem two:So how do we think about the problem?
In fact, it is very simple, we just calculate its result is 2 of how many power multiples of the line, if the result is 8, then 8 is 2 3 power, that is 8 of the last 1 in the bottom 4th.
So how to count n! How many powers is 2? Of course, we only have to count when the multiplication from a few 2 multiplied on it, or the first problem as the decomposition factor multiplied ah, see the total number of factors in 2. But is there an easy way?
Of course
Let's look at the 10! , then 10! How many 2 are there?
Oh, first we will 10/2=5 that is from 10 to 1 has 5 even
10, 8, 6, 4, 2
How many even numbers must be 2, and then we divide them by 2.5, 4, 3, 2, 1
These 5 numbers also have 5/2=2 an even number. 4, 2
we divide 4 and 2 by 2 to get
2, 1
There is a 2/1=1 an even number.
2
2/2=1 would have been gone.
Look at the algorithm:
def factorialpositionoflastone(v_value):
positioncount = 0
while v_value:
v_value >>=1
positioncount += v_value
print positioncount
return positioncount
From for notes (Wiz)
The number of factorial end 0 of N and the position of the last bit 1 in the binary representation