How many zeros are involved in the factorial result of n?
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
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: