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位表示尾数 浮 ...
随机推荐
- python 栈
栈的特点:先进后出 class Stack: def __init__(self): self.data = [] def push(self, val): self.data.append(val) ...
- css 透明度
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...
- cors中间件
class MiddlewareMixin(object): def __init__(self, get_response=None): self.get_response = get_respon ...
- 个人第4次作业——alpha项目测试
这个作业属于哪个课程 http://edu.cnblogs.com/campus/xnsy/GeographicInformationScience 这个作业的要求在哪里 https://www.cn ...
- python,读取txt的方法和应用
1.读取txt内的百度盘地址,循环保存到百度云中(直接访问下方地址) https://www.cnblogs.com/becks/p/11409467.html 2.读取txt内参数,循环执行查询,读 ...
- 根据范围爬TMS规则瓦片
因为需要简单写了一个下载地图的爬虫,代码如下: #coding=utf-8 import urllib.request import os import socket import zlib impo ...
- Docker扩展内容之容器环境变量
介绍 docker容器设置环境变量除了可以在容器层面的变量文件中加载也可以在容器运行之初进行预加载环境变量,下面介绍在Dockerfile中编写环境变量的方式 ENV TZ=Asia/Shanghai ...
- Spring boot 学习中代码遇到的几个问题
1.报一大段红错 此时对象还是能创建成功的,解决方案参考链接https://blog.csdn.net/wanglin199709/article/details/99121487 2.无法创建对象 ...
- MySQL 清理缓存—flush tablesFlush tables的影响
摘自:http://blog.chinaunix.net/uid-31401119-id-5781305.html 1 Flush tables简介 官方手册中关于Flush tables的介绍, ...
- LeetCode 381. Insert Delete GetRandom O(1) - Duplicates allowed O(1) 时间插入、删除和获取随机元素 - 允许重复(C++/Java)
题目: Design a data structure that supports all following operations in averageO(1) time. Note: Duplic ...