[Leetcode]65. Factorial Trailing zeros factorial's tail 0 numbers

Source: Internet
Author: User

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

Note:your solution should is in logarithmic time complexity.

Credits:
Special thanks to @ts for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Solution 1: If n! = = kx10m, (0! = k%10), then M is required. We decompose the n! into a mass factor, then there are N! = = 2X * 3Y * 5Z ..., and 10 is how to come, after decomposition not only by 2x5. In other words, only the min (x, z) of decomposition is required, and obviously, the number of 2 after decomposition is obviously more than the number of 5. So, M = = Z. According to the analysis, the most straightforward way to calculate Z is to calculate the 5 exponent in the factorization of I (I =1, 2, ..., N) and then sum.
classSolution { Public:    intTrailingzeroes (intN) {intCNT =0, J;  for(inti =5; I <= N; i + =5) {            intj =i;  while(J%5==0) {                ++CNT; J/=5; }        }        returnCNT; }};

This solution will time out when N is very large Time Limit exceeded. The note of the topic asks us to write a solution to the logarithmic time complexity.

Solution 2: On the basis of solution 1 to consider that the number of 5 can be contributed is a multiple of 5, while the index of 5 (5,25,125,... The number of contributors that can contribute 5 is exactly the value of a power (one-off,... ), Index of non 5 (10,15,20,30,,... ) can only contribute one 5. and using N divided by 5 can get how many multiples of 5 in [1,n], divided by 25 can get how many multiples of 25 ... So all the results add up to exactly how many 5 in [1,n]. Note that 25=5*5 can contribute two 5, and in the front divided by 5 time has been calculated once, so 1 times can be, the same 125=5*5*5, in 2 and 25 has been considered two 5, so the following is also added one on the line, the following all 5 of the index is so.

 class   solution { public  :  int  trailingzeroes ( Span style= "color: #0000ff;" >int   N) { int  cnt = 0  ;  long  long  k = 5  ;  while  (k <= N) {cnt  + = n/< Span style= "color: #000000;"            > K;        K  *= 5  ;     return   CNT; }};

Note that k must be defined as a long long type to prevent K overflow when n is large. One to avoid this pitfall is as follows:

class Solution {public:    int trailingzeroes (int  n) {          int0;          while 0 ) {            5;             5 ;        }         return cnt;    }};

[leetcode]65. Factorial Trailing zeros factorial's tail 0 number

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.