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位表示尾数 浮 ...
随机推荐
- 跨源请求cors和jsonp
0.产生跨域的原因 浏览器的同源策略 什么是浏览器的同源策略? src开发 ajax禁止 解决方法 jsonp 通过src绕过浏览器的同源策略 缺点:只发送GET请求 cors 通过设置相应头 分类 ...
- 1.Java和Python的选择
我认为高级语言分为Java/c系列和其他. Java:1995年,让程序员设计一些大型分布式复杂应用. Python:1991年,面向系统管理.科研教育.等非程序员群体用的多. C系列语言:奠定了现在 ...
- 使用Razor表达式 使用条件语句 来自 精通ASP-NET-MVC-5-弗瑞曼
- Centos 7 最小化部署jenkins
前言 jenkins是devops与CI/CD的重要工具之一,下面通过jenkins与svn的结合完成自动部署功能 环境 软件 名称 版本 操作系统 Centos 7.4 开发环境 jdk 1.8 中 ...
- Ceph 存储集群7-故障排除
Ceph 仍在积极开发中,所以你可能碰到一些问题,需要评估 Ceph 配置文件.并修改日志和调试选项来纠正它. 一.日志记录和调试 般来说,你应该在运行时增加调试选项来调试问题:也可以把调试选项添加到 ...
- 使用CStatic显示图片(bmp、ico、png)
一.显示bmp及ico //h文件 CStatic m_static; //cpp文件 CBitmap bitmap; bitmap.LoadBitmapW(IDB_BITMAP); //加载位图 B ...
- FFMPEG学习----遍历所支持的封装格式
#include <stdio.h> extern "C" { #include "libavformat/avformat.h" }; int m ...
- LUA学习笔记(第18-20章)
数学库 print(math.pi)-->π print(math.huge)-->Lua中表示的最大数字 --[[ 3.1415926535898 1.#INF ]] print(mat ...
- 关于JavaScript的DOM和BOM
本文探讨JavaScript的三大部分中的两个部分,DOM和BOM. DOM介绍 DOM,全称Document Object Model,即文档对象模型.它 是W3C的一个标准,定义了一个对文档操作的 ...
- 深入理解计算机系统大作业——程序人生P2P
程序人生P2P 前言 经过一个学期的快乐学习(折磨),计算机系统终于结课了,自认为对于计算机系统算是有了粗浅的理解.为了庆祝结课,顺带总结自己的学习经历(只是为了完成大作业),便通过一个简单的程序he ...