172. Factorial Trailing Zeroes

Source: Internet
Author: User

    /**172. Factorial Trailing Zeroes *2016-6-4 by Mingyang * First do not forget what is factorial, is factorial. Then it is easy to think of the number of statistics * (2,5) pairs, because 2x5=10. But the condition is relaxed and you will find that just a few 5 of the number is good, * because 2 is actually more than 5.     Then the title translates into the sum of approximately 5 of the numbers from 1 to N. * Very simple to think can be used N/5 get. For example, when n is 19, 19/5 = 3.8, then there are 3 approximations containing 5, 5, 10, * 15, respectively. But some of the numbers may be divisible by 5, say 25.     Such a number can contribute several 5 to the final factorial. * The Final solution is to sum the n/5+n/25+n/125+...+ and stop when n is less than the denominator. The denominator is 5^1, 5^2, 5^2 ... * in this case, in the calculation of 5^2, can be divisible by 25 of the number of two 5, one of which has been calculated in the 5^1.     So the 5^2 is added directly to count. */     Public Static intTrailingzeroes (intN) {if(n<0)return-1; intCount = 0;  for(Longi=5; n/i>=1; I*=5) {Count+ = N/i; }                    returncount; }

172. Factorial Trailing Zeroes

Related Article

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.