codewars--js--Number of trailing zeros of N!
问题描述:
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.
刚刚开始做的时候,很直接的思路就是先求阶乘,然后求尾部0的个数。后来发现,当数据超过一定位数时,js会以科学计数法表示,不能直接计算0的个数。
接着就是思考有2*5能出来0,然后5的个数相对于2的个数会比较少,所以就求5作为因子出现了多少次。
我的答案:
function zeros (n) {
var num=0;
for(var i=1;i<=n;i++){
var j=i;
while(j%5==0){
num=num+1;
j=j/5;
}
}
return num;
}
优秀答案:
function zeros (n) {
var zs = 0;
while(n>0){
n=Math.floor(n/5);
zs+=n
}
return zs;
}
codewars--js--Number of trailing zeros of N!的更多相关文章
- Trailing Zeros
Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...
- lintcode :Trailing Zeros 尾部的零
题目: 尾部的零 设计一个算法,计算出n阶乘中尾部零的个数 样例 11! = 39916800,因此应该返回 2 挑战 O(logN)的时间复杂度 解题: 常用方法: 也许你在编程之美中看到,通过求能 ...
- [LeetCode] Factorial Trailing Zeros
Well, to compute the number of trailing zeros, we need to first think clear about what will generate ...
- 2. Trailing Zeros【easy】
2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...
- [Algorithm] 2. Trailing Zeros
Description Write an algorithm which computes the number of trailing zeros in n factorial. Example 1 ...
- [CodeWars][JS]实现链式加法
在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...
- js Number string
Number string number Js只有一种数字类型(包括整型,浮点型) 极大或极小的可用科学计数法来表示.(7.7123e+1) 所有js数字均为64位 Js所有的数字都存储为浮点型 小数 ...
- 【转载】JS Number类型数字位数及IEEE754标准
JS的基础类型Number,遵循 IEEE 754 规范,采用双精度存储(double precision),占用 64 bit.如图 意义 1位用来表示符号位 11位用来表示指数 52位表示尾数 浮 ...
- JS Number类型数字位数及IEEE754标准
JS的基础类型Number,遵循 IEEE 754 规范,采用双精度存储(double precision),占用 64 bit.如图 意义 1位用来表示符号位 11位用来表示指数 52位表示尾数 浮 ...
随机推荐
- numpy nan和inf
一.nan和inf的简介 nan 不是一个数字 读取本地文件为flaot的时候,有缺失 inf(infinity): 无穷尽 inf: 正无穷 -inf: 负无穷 数据类型:float # 注意: 要 ...
- 创建Account控制器 安全性与收尾工作 精通ASP-NET-MVC-5-弗瑞曼
- RMQ算法使用ST表实现
RMQ RMQ (Range Minimum Query),指求区间最小值.普通的求区间最小值的方法是暴力. 对于一个数列: \[ A_1,~ A_2,~ A_3,~ \cdots,~ A_n \] ...
- 团队项目——Alpha发布1
这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/homework 这个作业要求在哪里 https ...
- 从txt文本内读取数据(逐行读取),执行循环
如下图,名称为1.txt的文本文件内有链接若干条,预期是循环读取txt文本内链接,而后访问该链接 脚本如下: for line in open('C:\\Users\\Beckham\\Desktop ...
- Centos7安装 Anaconda + jupyter notebook
一.安装Anaconda 1 下载Anaconda安装脚本 为了避免漫长的等待,镜像源选择国内的清华镜像源,镜像源地址:https://mirrors.tuna.tsinghua.edu.cn/ana ...
- QDialog
QDialog设置setWindowFlags(Qt::FramelessWindowHint);后,dialog弹不出来,界面不显示: 在继承一个QDialog类并设置窗口为无边框时,如果调用set ...
- [SDOI2011]染色(树链剖分)
[SDOI2011]染色(luogu) Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段 ...
- JVM第一弹
JVM第一弹 基本概念 JVM是可运行java代码的假想计算机,包括一套字节码指令集,一组寄存器,一个栈,一个垃圾回收.堆和一个存储方法域.JVM是运行在操作系统之上的,它与硬件没有直接的交互. 运行 ...
- ios--->ios沙盒总结
ios沙盒总结 沙盒介绍 iOS应用程序只能在该程序创建的文件系统中读取文件,不可以去其它地方访问,此区域被成为沙盒,所以所有的非代码文件都要保存在此,例如图像,图标,声音,映像,属性列表,文本文件等 ...