C# - same as others


  • 0
    public int TrailingZeroes(int n) 
    {
        int count = 0;
        while (n > 0)
        {
            count += n/5;
            n /= 5;
        }
        
        return count;
    }

  • 0
    M

    My implementation is negligibly more efficient and includes an inadequate explanation of how the algorithm works.

            public int TrailingZeroes(int n)
            {
                int trailingZeroes = 0;
                
                // The basic underlying principle is that:
                // Every fifth number can be multiplied with a two (which are a dime-a-dozen) to create another trailing zero.
                while (n > 1)
                {
                    n = n / 5;
                    trailingZeroes += n;
                }
                return trailingZeroes;
            }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.