Question: How many zeros are there in the factorial of n?
A: There is only one possibility of zero production: 2*5 = 10, however, the factorial of N is essentially a product that can be split into many 2 and 5, and other multiplier products that do not contain 2 and 5, such as the factorial of 5: 1*2*3*4*5 = 1*2*3*2*5. According to this idea, each item of the factorial product of N is split to see how many 2 and 5 can be split, and then the minimum number of 2 and 5 can be taken. The program code is as follows:
# Include <stdio. h> int compute_zero (int n) {int five_count = 0; int two_count = 0; int I, temp; for (I = 1; I <= N; I ++) {temp = I; while (0 = TEMP % 2) {temp/= 2; two_count ++;} temp = I; while (0 = TEMP % 5) {temp/= 5; five_count ++ ;}} return (five_count> two_count? Two_count: five_count);} int compute_answer (int n) {int answer = 1; int I; for (I = 1; I <= N; I ++) {answer * = I; If (answer> 10000000)/* only a maximum of six 0 */{answer % = 10000000 ;}} return answer;} int main () {int N; printf ("Please input N:"); scanf ("% d", & N); printf ("answer is: % d. \ n ", compute_answer (n); printf (" answer has zero % d. \ n ", compute_zero (n); Return 0 ;}
Note: There is no problem with the tested input 25. When the input data is too large, the compute_answer function may fail to calculate the result, the results of the compute_answer function also limit the size of the results. The results are discarded when the subsequent low-level values are displayed. However, the compute_zero results analyzed above will not cause errors, here we will compare the two outputs. The test results are as follows:
How many zeros are involved in the factorial result of n?