<jupyter_start><jupyter_text>Kata: [Number of trailing zeros of N!](https://www.codewars.com/kata/number-of-trailing-zeros-of-n) * 在Python 中計算階層(factorial)的方法: math.factorial, numpy.math.factorial, scipy.math.factorial  解法1 (被判定Codewars沒效率):<jupyter_code>import numpy as np

def zeros(n):
    if n < 5:
        return 0
    else:
        f = np.math.factorial(n)
        i = 1
        while str(f // (10 ** i))[-1] == '0':
            i = i + 1    
        return i

print(zeros(12))
print(zeros(1000))<jupyter_output>2
249<jupyter_text>解法2 (被Codewars判定沒效率):<jupyter_code>import numpy as np

def zeros(n):
    f = np.math.factorial(n)    
    i = 1
    while np.sum(np.array([n for n in str(f)], int)[-i:]) == 0:
        i = i + 1    
    return i-1

print(zeros(12))
print(zeros(1000))<jupyter_output>2
249<jupyter_text>解法3 (被Codewars判定沒效率):<jupyter_code>import numpy as np

def zeros(n):
    f = np.math.factorial(n)
    #arry = np.array([n for n in str(f)], int)[::-1]
    arry = np.array(list(str(f)), int)[::-1]
    ind = np.nonzero(arry)[0]          
    return ind.min()

print(zeros(12))
print(zeros(1000))<jupyter_output>2
249<jupyter_text>解法4 (被Codewars判定沒效率):<jupyter_code>def zeros(n):
    res = 0
    for i in range(n):
        res +=  (n // (5 ** (i+1)))
    return res

print(zeros(12))
print(zeros(1000))<jupyter_output>2
249<jupyter_text>解法5<jupyter_code>def zeros(n):
    res = 0
    while(n):
        res += n // 5
        n = n // 5
    return res
    
print(zeros(12))
print(zeros(1000))<jupyter_output>2
249
{'fraction_non_alphanumeric': 0.16546762589928057, 'fraction_numerical': 0.04970568999345978, 'mean_word_length': 3.015748031496063, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 16, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '462396', 'license': 'MIT', 'n_tokens_mistral': 730, 'n_tokens_neox': 694, 'n_words': 125, 'path': 'number-of-trailing-zeros-of-n.ipynb', 'repo_name': 'YihaoSu/codewars', 'size': '1529'}