Factorial Trailing Zeroes

Source: Internet
Author: User

Factorial Trailing Zeroes

Problem:

Given an integer n, return the number of trailing zeroes in N!.

Note:your solution should is in logarithmic time complexity.

Ideas:

You know the idea, you don't really understand.

Others code:

 Public class Solution {    publicint trailingzeroes (int  n) {        if  return 0;         int k = 0;          while (n > 0)        {            + =N/5            ; /= 5;        }         return k;    }}
View Code

Why??

Others explain: After reading only then dawned, Taichetaiwu ....

Then, since can not traverse, can only change a thought, can constitute the end of 0, in fact, the determining factor is the number of 1 to n a total of how many 5 factors. So we consider this: for 5 then divisible by him is 5 10 15 25 30 ... In fact, there is a total of N/5, for 25 50 such a number includes two 5 factors, which we will calculate later, when considering 5, the result is N/5.     (This time has been considered 25 125 but only considered once) for 25 can be divisible is 25 50 75 ... So, in fact, there are a total of N/25, this time 25 of the two 5 factors, the change is counted. (On the basis of the above, in consideration of a 25 this completes 252 times the count N/5/5 = = N/25 Ah can be counted 25 how many times) for 125 can also be divisible by it is 125 625 ... (as the above method is similar to the deduction can be)in this way, the result is actually:N/5 + N/25 + n/125 ...

Factorial Trailing Zeroes

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.