32-bit compiler and 64-bit compiler data bytes (one byte = 8 bits)
Compiler
int type: 4 bytes 32 bits (4*8), addressing space is 2^32, data range -2^31~2^31
long long type: is a super long integer, 8 bytes 64 bits (8*8), the addressing space is 2^64 data range -2^63~2^63
Topic description:
Design an algorithm to calculate the number of trailing zeros in the factorial of n
Example : 11! =39916800, so returns 2
Analysis:
The factorial of the number n can be decomposed into the product of prime powers, that is, n!=2^x*3^y....5^z...
Obviously, 2*5=10 produces a 0, and the number of 2 is greater than The number of 5 is both x>z, and this question also finds the size of z
The multiples of 5 in 1 to n are 5, 10, 15, 20, 25, 30...50..., and n/5 is the number of numbers in n that are divisible by 5
5^2=25 provides 2 5s, the multiples of 25 in 1 to n are 25, 50, 75, 100..., n/25 is the number of numbers in n that can be divisible by 5*5, and the multiple of 25 must be Multiples of 5 and already provided a 5 on the first query, so can provide (2-1) another 5 on the second query
5^3=125 provides 3 5s, the multiples of 125 in 1 to n are 125,250, ..., n/125 is the number of numbers in n that can be divisible by 125, and the multiples of 125 must be multiples of 5 and 25 , 2 5s have been provided in the last two queries, so in the third query it is possible to provide (3-2) a 5
And so on, z=n/5+n/(5^2)+n/(5^3)...until n/(5^a)<0
code show as below:
long long trailingZeros(long long n){
long long num;
while(n)
{
num+=n/5;
n/=5;
}
return num;
}
When the condition is any non-zero value, the target statement is always executed when the condition is true.
while(condition){
statement(s);
}