"Leetcode" 172. Factorial Trailing Zeroes

Source: Internet
Author: User

Title:

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

Note: Your solution should is in logarithmic time complexity.

Tips:

This problem requires the number of end zeros of n factorial. Because when and only a pair of 2 and 5 appear in factorial, a 0 is present at the end, and the number of 2 is much less than 5, so the number of occurrences of 5 can be calculated. One method is to increment 5 each time and calculate the number of factor 5. There is also a faster way to borrow the base 5 logarithm. You can see the code specifically.

Code:
classSolution { Public:    intTrailingzeroes (intN) {intStep = log (n)/log (5); intCount =0;  for(inti =1; I <= step; ++i) {count+ = (N/pow (5, i)); }        returncount; }};

"Leetcode" 172. 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.