LeetCode 172: Factorial Trailing Zeroes

Source: Internet
Author: User

LeetCode 172: Factorial Trailing Zeroes

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

 

// Question Description: given an integer n, return n! (Factorial of n) the number of suffixes 0. // Method 1: first obtain the factorial of n and then calculate the number of 0 at the end. This method is n when n is large! Class Solution {public: int trailingZeroes (int n) {if (n = 0) return 0; int m = 0; int cnt = 0; int k = factorial (n); while (k) {m = k % 10; k = k/10; if (m = 0) cnt ++; elsebreak ;} return cnt;} // calculate the factorial int factorial (int n1) of n {if (n1 = 0) return 1; else {return n1 * factorial (n1-1 );}}};

// Method 2: Consider n! Prime factor. The suffix 0 is always obtained by multiplying prime factor 2 and prime factor 5. If we can count the numbers of 2 and 5, the problem is solved. // Example: WHEN n = 5, 5! (2*2*2*3*5) contains one five and three two. Therefore, the number of suffixes 0 is 1. // N = 11, 11! (2 ^ 8) * (3 ^ 4) * (5 ^ 2) * 7) contains two 5 and eight 2. Therefore, the number of suffixes 0 is 2. // We can easily see that the number of 2 in the prime factor is always greater than or equal to 5, so we only need to count the number of 5. // How to calculate n! What is the number of all five in the prime factor? A simple method is to calculate floor (n/5 ). For example, 7! There is a 5, 10! There are two 5. // There is one more thing to consider. There are more than five numbers, such as 25,125. // For example, n = 25, n! = 25*24*23 *... * 15... * 10... * 5... * 1 = (5*5) * 24*23 *... * (5*3 )*... (5*2 )*... (5*1 )*... * 1, 25 of which can be regarded as 5*5, with a 5 more, it is very easy to add // to solve this problem. First, remove all single 5 for n limit 5, then 25, remove the additional 5, and so on. The formula for calculating the suffix 0 is as follows. // N! Number of suffixes 0 = n! Number of five in the prime factor = floor (n/5) + floor (n/25) + floor (n/125) + .... class Solution2 {public: int trailingZeroes (int n) {int res = 0; while (n) {res = res + n/5; n = n/5 ;} return res ;}};


 

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.