博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codewars第十一天–PermutationsNumber of trailing zeros of N!
阅读量:4303 次
发布时间:2019-05-27

本文共 452 字,大约阅读时间需要 1 分钟。

Codewars第十一天–PermutationsNumber of trailing zeros of N!

题目描述:

给定一个数n ,返回n 的阶乘中末尾0 的个数。比如:

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

代码如下:在这里不能够求阶乘,因此如果n 很大的会溢出,计算量很大,因此需要通过查找结果的规律来求解。

会发现阶乘末尾出现0 的情况是从1n 中有数字相乘为10 或10的倍数,也就是存在2*5=10,在数字中5的倍数出现的几率减少。
因此只需要统计出对1到n 中所有数因子分解后得到的5的个数即可。

def zeros(n):    temp = 0    while n:        temp += n // 5        n = n // 5    return temp

转载地址:http://cqmws.baihongyu.com/

你可能感兴趣的文章
PCB反推理念
查看>>
京东技术架构(一)构建亿级前端读服务
查看>>
git 提示:error: unable to rewind rpc post data - try increasing http.postBuffer
查看>>
php 解决json_encode中文UNICODE转码问题
查看>>
LNMP 安装 thinkcmf提示404not found
查看>>
PHP empty、isset、innull的区别
查看>>
apache+nginx 实现动静分离
查看>>
通过Navicat远程连接MySQL配置
查看>>
phpstorm开发工具的设置用法
查看>>
Linux 系统挂载数据盘
查看>>
Git基础(三)--常见错误及解决方案
查看>>
Git(四) - 分支管理
查看>>
PHP Curl发送数据
查看>>
HTTP协议
查看>>
HTTPS
查看>>
git add . git add -u git add -A区别
查看>>
apache下虚拟域名配置
查看>>
session和cookie区别与联系
查看>>
PHP 实现笛卡尔积
查看>>
Laravel中的$loop
查看>>