Saturday, January 17, 2015

Factorial Trailing Zeroes @LeetCode

Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.

/*
* Trailing zeros of n!
*
* take care Integer OverFlow
* Reference:
* http://www.purplemath.com/modules/factzero.htm
* http://www.cnblogs.com/EdwardLiu/p/4207498.html
*/
public class TrailingZeroes {
public static int trailingZeroes(int n) {
if (n < 0) {
n = -1* n;
}
int result = 0;
for (int i = 5; n / i >= 1; n = n / 5) {
result += n / i;
}
return result;
}
public static void main(String[] args) {
System.out.println(trailingZeroes(1808548329));
}
}

No comments:

Post a Comment