问题描述:

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!的更多相关文章

  1. Trailing Zeros

    Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...

  2. lintcode :Trailing Zeros 尾部的零

    题目: 尾部的零 设计一个算法,计算出n阶乘中尾部零的个数 样例 11! = 39916800,因此应该返回 2 挑战 O(logN)的时间复杂度 解题: 常用方法: 也许你在编程之美中看到,通过求能 ...

  3. [LeetCode] Factorial Trailing Zeros

    Well, to compute the number of trailing zeros, we need to first think clear about what will generate ...

  4. 2. Trailing Zeros【easy】

    2. Trailing Zeros[easy] Write an algorithm which computes the number of trailing zeros in n factoria ...

  5. [Algorithm] 2. Trailing Zeros

    Description Write an algorithm which computes the number of trailing zeros in n factorial. Example 1 ...

  6. [CodeWars][JS]实现链式加法

    在知乎上看到这样一个问题:http://www.zhihu.com/question/31805304; 简单地说就是实现这样一个add函数: add(x1)(x2)(x3)...(xn) == x1 ...

  7. js Number string

    Number string number Js只有一种数字类型(包括整型,浮点型) 极大或极小的可用科学计数法来表示.(7.7123e+1) 所有js数字均为64位 Js所有的数字都存储为浮点型 小数 ...

  8. 【转载】JS Number类型数字位数及IEEE754标准

    JS的基础类型Number,遵循 IEEE 754 规范,采用双精度存储(double precision),占用 64 bit.如图 意义 1位用来表示符号位 11位用来表示指数 52位表示尾数 浮 ...

  9. JS Number类型数字位数及IEEE754标准

    JS的基础类型Number,遵循 IEEE 754 规范,采用双精度存储(double precision),占用 64 bit.如图 意义 1位用来表示符号位 11位用来表示指数 52位表示尾数 浮 ...

随机推荐

  1. 五个常见 AI 开发库

    上一篇文章已经写过了,人工智能的发展不可谓不曲折,三起两落,不同的历史阶段,主流的研究方法不一样,开始时的 if-else 结构,简单的逻辑判断字符串匹配,到后期的穷举计算等,慢慢发展到现在的大数据与 ...

  2. Element中(Notification)通知组件字体修改(Vue项目中Element的Notification修改字体)

    这个问题纠结很久,一样的写的为啥有的页面就可以,有的就不行: 后来才发现: 先说一下怎么设置: 先定义customClass一个属性,用来写class属性值: 之后还需要修改一下组件里style标签的 ...

  3. 关于Xen

    尝试了各种方法搭建xen,尝试过从xenserver入手,但似乎最近时间端不允许访问,感谢我的老师,叫我从kvm入手,暂时记下xen中种种的坑,以后有缘再战.欢迎交流

  4. 在python开发工具PyCharm中搭建QtPy环境(详细)

    在python开发工具PyCharm中搭建QtPy环境(详细) 在Python的开发工具PyCharm中安装QtPy5(版本5):打开“File”——“Settings”——“Project Inte ...

  5. zookeeper3.4.6安装

    1.关闭防火墙 service iptables stop chkconfig iptables off 2.编辑hosts文件: vi /etc/hosts 192.168.99.6 JacK6 1 ...

  6. UML--->用例图梳理

    用例图梳理 概述 用例图主要用来描述"用户.需求.系统功能单元"之间的关系.它展示了一个外部用户能够观察到的系统功能模型图. 用例图的主要目的是帮助开发团队以一种可视化的方式理解系 ...

  7. 连接redis出现错误:Cannot get Jedis connection

    错误信息: 错误描述:由于我的redis数据库没有设置密码,配置连接的时候我配置了密码为空,导致连接失败

  8. mysql 记录一次内存清理

    摘自:https://blog.csdn.net/wyzxg/article/details/7279986/ 摘要:Linux对内存的管理与Windows不同,free小并不是说内存不够用了,应该看 ...

  9. 安装jrebel并激活

    1.File-Settings-Plugins搜索jrebel,找到JRebel for IntelliJ ,点击install(我这里已经安装过了,所以显示的是更新) 2.重启idea 3.激活步骤 ...

  10. 017.Python函数匿名函数

    匿名函数 lambda表达式 lambda表达式 : 用一句话来表达只具有返回值的函数,简单,方便,直截了当 # 语法: lambda 参数 : 返回值 无参数的lambda 表达式 def func ...