Java-factorial Trailing Zeroes

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.

See for a long while did not read the title meaning to ask N end 0 of the number of a half-day is required n! That is, the factorial of n results in the end of how many 0 can be known when the factorial factor in a pair of 5 and 2 to produce 0 and for the factorial of 5 is less than 2 of the number of 5 that is 2 with its collocation to get 0 so as long as the calculation of the number of multiples of 5 can be decomposed into 25 125 5*5 In order to get the results also to be carried out in addition to five in order to get a number like 5^n, more than 5 has been circulating to the N=0 code as follows:

public class Solution {public    int trailingzeroes (int n) {        int count=0;        while (n>0) {            count+=n/5;            N=N/5;        }        return count;    }}


Java-factorial Trailing Zeroes

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.