How many zeros are involved in the factorial result of n?

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.