【Codewars python 5kyu】: Number of trailing zeros of N!

这篇博客介绍了如何编写一个程序,用于计算给定数字阶乘末尾的零的数量。它提示读者不必直接计算阶乘,而是寻找另一种方法来确定零的个数。文章提供了示例和代码实现。
摘要由CSDN通过智能技术生成

问题描述:

Write a program that will calculate the number of trailing zeros in a factorial of a given number.

N! = 1 * 2 * 3 * ... * N

Be careful 1000! has 2568 digits...

For more info, see: http://mathworld.wolfram.com/Factorial.html

Examples

zeros(6) = 1
# 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720 --> 1 trailing zero

zeros(12) = 2
# 12! = 479001600 --> 2 trailing zeros

Hint: You're not meant to calculate the factorial. Find another way to find the number of zeros.

代码实现:

#codewars第十三题
def zeros (n):
    num = 0
    while n > 4:
        n = math.floor(n/5)  
        num += n
    return num;
                             #计算结尾0的个数  主要是靠2*5产生0  但是2的话远多于5的个数 
                     
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值