Monday, April 6, 2015

Factorial Trailing Zeroes

Problem:
Given an integer n, return the number of trailing zeroes in n!.
Note: Your solution should be in logarithmic time complexity.


Solution:
class Solution {
public:
    int trailingZeroes(int n) {
        long int result = 0;
        long int k = 5;
        while (n/k != 0) {
            result += n/k;
            k *= 5;
        }
        return result;
    }
};

No comments:

Post a Comment