Code007 - Number of trailing zeros of N!

https://www.codewars.com/kata/52f787eb172a8b4ae1000a34/train/javajava

package codewar;
public class Solution{
    public static int zeros(int n){
        int dividor = 5;
        int sum = 0;
        while(true){
            int i = n / dividor;
            if( i < 1){
                break;
            }
            sum += i;
            dividor *= 5;
        }
        return sum;
}

這道題若是使用BigInteger計算出階乘積,再計算尾零的數目,用時會超限。因此,必須另尋蹊徑。code

Algorithm:htm

http://www.purplemath.com/modules/factzero.htmget

相關文章
相關標籤/搜索