Calculates the number of 0 at the end of the factorial n!

Source: Internet
Author: User

Problem description
Given the parameter n (n is a positive integer), calculate the factorial of n n! The number of "0" is included at the end.
For example, 5! =120, which contains the number of "0" at the end of the 1;10! = 3628800, with the number of "0" at the end of the 2;20! = 2432902008176640000, with the number of "0" at the end of which is 4.

Problem Analysis:

Obviously, for the factorial growth rate of very fast, it is easy to overflow. Of course, it is impossible to calculate the factorial, but to find a regular solution. The following with factoring thinking to consider: The end of the 0 can be decomposed into 2*5, a 5, a 2 corresponds to a 0;

The following is a recursive process:

(1) When n<5, f (n) = 0; Conclusion established

(2) When n>=5, can make n! =[5k*5 (k-1) ... 10*5]*a, where N=5k+r, R (0<=r<5), A is an integer without a factor, can be seen as an integer satisfying the condition;

For sequence 5k,5 (k-1) ... 10*5 in each 5i, there is an even number within the interval (5 (i-1), 5i), i.e. there is a 2 corresponding to it. So here's K ' 5 ' factor with n! The end of the 0-digit number is one by one corresponding.

So the recursive formula translates to:

F (n!) =g (5^k * k! *a) = k + g (k!) = K+f (k!); K! is the result of multiplying the coefficients.

F (n!) =k+f (k!);

Example: F (5!) = 1+f (1!) = 1;

F (10!) =2+f (2!) = 2;

Here are two implementations of C + + code: recursive; non-recursive:

recursive int n_jie_tail_zeors_recurise (int n) {    int zeros_num =0;    int k;    if (n < 5)        return 0;    for (k=5;k<=n;k+=5)    {        zeros_num++;    }    return zeros_num + n_jie_tail_zeors_recurise (zeros_num);}

Non-recursive int n_jie_tail_zeros_non_recurse (int n) {    int zeros_num =0;    int temp = n;    int k=0;    int index=0;    while (temp >5)    {        index =0;        for (k =5;k<=temp;k+=5)        {            zeros_num++;            index++;        }        temp = index;    }    return zeros_num;}


Calculates the number of 0 at the end of the factorial 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.