问题描述:

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. Mysql一分钟定位 Next-Key Lock,你需要几分钟

    连接与线程 查看连接信息 show processlist +----+------+------------------+------+---------+------+----------+--- ...

  2. 深入学习MySQL 01 一条查询语句的执行过程

    在学习SpringCloud的同时,也在深入学习MySq中,听着<mysql45讲>,看着<高性能MySQL>,本系列文章是本人学习过程的总结,水平有限,仅供参考,若有不对之处 ...

  3. [ZJOI2008]树的统计(树链剖分)

    [ZJOI2008]树的统计(luogu) Description 一棵树上有 n 个节点,编号分别为 1 到 n,每个节点都有一个权值 w.我们将以下面的形式来要求你对这棵树完成一些操作: I. C ...

  4. 基于Flask框架搭建视频网站的学习日志(一)

    ------------恢复内容开始------------ 基于Flask框架搭建视频网站的学习日志(一)2020/02/01 一.Flask环境搭建 创建虚拟环境 初次搭建虚拟环境 搭建完虚拟环境 ...

  5. Shell之哈希表

    前言 linux系统下会有一个hash表,当你刚开机时这个hash表为空,每当你执行过一条命令时,hash表会记录下这条命令的路径,就相当于缓存一样.第一次执行命令shell解释器默认的会从PATH路 ...

  6. Docker底层架构之命名空间

    前言 命名空间是 Linux 内核一个强大的特性.每个容器都有自己单独的命名空间,运行在其中的 应用都像是在独立的操作系统中运行一样.命名空间保证了容器之间彼此互不影响.相应的命名空间功能如下: pi ...

  7. 死磕mysql(6)

    再写数据库作业的时候,发现了一个问题,如果存在主键外键的约束,数据就删不掉 --set foreign_key_checks=0; 关掉外键约束 用好了再打开 --set foreign_key_ch ...

  8. Selenium实现微博自动化运营:关注、点赞、评论

    目录 Selenium 是什么? 一.核心代码 二.步骤分解 1.打开浏览器 2.访问微博登录页 3.输入账号密码 4.点击登录 5.通过人机验证 6.打开我们的中公题库君首页 7.加一下关注 8.定 ...

  9. virtualenv 指定 python 解释器的版本

    使用如下命令为 ubuntu 系统安装 virtualenv sudo apt-get install python-virtualenv 当我们使用 virtualenv 命令创建虚拟环境时,默认使 ...

  10. It is possible and safe to monitor a table DML history on sqlserver

    He is my test step: In a test enviroument, I make a table "test"/ demo table:create table ...