[LeetCode] #172 Factorial Trailing Zeroes

#172 Factorial Trailing Zeroes

Question

Given an integer n, return the number of trailing zeroes in n!.

Note: Your solution should be in logarithmic time complexity.

Thinking

題目是給一個整數n,要我們算出n!共出現幾個0,並且要求時間複雜度要在O(logn)。因為5乘以任何一個偶數都將會出現0,所以關鍵在於找出有多少個5存在

My C# Solution

public class Solution {
    public int TrailingZeroes(int n) {
        var result = 0;
        while(n >= 5)
        {
            n /= 5;
            result += n;
        }
        return result;
    }
}