Online Judge Solutions

Thursday, December 25, 2014

Trailing Zeros

Write an algorithm which computes the number of trailing zeros in n factorial.
Example
11! = 39916800, so the out should be 2
Challenge
O(log N) time
 
class Solution {
 public:
    // param n : description of n
    // return: description of return 
    long long trailingZeros(long long n) {
        long long out = 0;
        while(n) {
            n = n/5;
            out += n; 
        }
        
        return out;
    }
};

No comments:

Post a Comment