LeetCode 172 Factorial Trailing Zeroes (zero after Factorial )(*)

Source: Internet
Author: User

LeetCode 172 Factorial Trailing Zeroes (zero after Factorial )(*)
Translation

Given an integer n, return n! The number of zeros. Note: Your solution should be within the log time complexity.
Original
Given an integer n, return the number of trailing zeroes in n !. Note: Your solution shocould be in logarithmic time complexity.
Analysis

At first, I did not pay too much attention when reading the question. I thought it was just a fraction of the number n. Although I thought it would not be that simple ...... I wrote a piece of code and submitted it. The result WA prompts me that 1 should be returned if 5 is returned. This is why I am wondering, the number of hairs after 5 is 0 ...... Read the questions ...... Oh, it turns out to be a factorial. I have met this question.

If there is zero at the end, there must be five in the factorial process. If it is 10, there will be two numbers that can be divisible by five, 10 and 5, and so on ......

Just now I went to look for the blog I wrote. You can check it out. It's very interesting. Using Functional Language LISP, we can find the 20000 factorial. ^_^

Is it true that 100 of the class cannot be counted?

Code
Class Solution {public: int trailingZeroes (int n) {int count = 0; while (n> 1) count + = (n/= 5); return count ;}};

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.