Leetcode Note: Factorial Trailing Zeroes

Source: Internet
Author: User

Leetcode Note: Factorial Trailing Zeroes

I. Description

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

Note: Your solution shocould be in logarithmic time complexity.

Ii. Question Analysis

The requirement of the question is to give an integern, Find outn!The end of the result is0. The brute force law is obtained first.n!And directly calculate the end0. (Repeated (n !) /10, until the remainder is not 0), if the inputnWhen the value is too large, it will cause overflow during factorial calculation, so this method is not easy to use.

Http://www.cnblogs.com/ganganloveu/p/4193373.html provides a very clever solution:

In fact, as longnFactorialn!, For factorization:n!=2^x*3^y*5^z*...

Apparently0The number is equalmin(x,z)And it is proved that the following information can be obtained:min(x,z)==z.

For example:

n = 5,5!Prime factor decomposition:5! = 2 * 2 * 2 * 3 * 5Contains3Items2,1Items3And1Items2. Suffix0The number is1.

n = 11,11!Prime factor decomposition:11! = 2^8 * 3^4 * 5^2 * 7Contains8Items2,4Items3And2Items5. Suffix0The number is2.

Proof:

For factorial, that is1*2*3*...*n, Set[n/k]Representative1~nNumber of divisible by k
Apparently,[n/2] > [n/5](The left side is Feng2Add1On the right side5Add1)
[n/2^2] > [n/5^2](The left side is Feng4Add1On the right side25Add1)
......
[n/2^p] > [n/5^p](The left side is Feng2^pAdd1On the right side5^pAdd1)
With powerpAppears2^pThe probability is much higher than that of appearance.5^p.
Therefore, the addition on the left must be greater than the addition on the right, that isn!Prime factor decomposition,2The power must be greater5Power.

Iii. Sample Code

#include 
  
   using namespace std;class Solution{public:    int trailingZeroes(int n) {        int result = 0;        while (n)        {            result += n / 5;            n /= 5;        }        return result;    }};
  

Iv. Summary

There are few codes for this question, but it is necessary to deduce the complex requirements from the question into simple operations. In general, it still takes some effort.

 

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.