问题描述:

In mathematics, the factorial of integer n is written as n!. It is equal to the product of n and every integer preceding it. For example: 5! = 1 x 2 x 3 x 4 x 5 = 120

Your mission is simple: write a function that takes an integer n and returns the value of n!.

You are guaranteed an integer argument. For any values outside the non-negative range, return nullnil or None (return an empty string "" in C and C++). For non-negative numbers a full length number is expected for example, return 25! = "15511210043330985984000000" as a string.

For more on factorials, see http://en.wikipedia.org/wiki/Factorial

解题思路:

刚开始就是按照寻常情况,直接就用for循环或是递归求阶乘。然后发现js的Number有位数限制(n数相对较大时,会以科学计数法呈现结果;n数很大时,越界,Infinity)。总之就不能像是题目要求的显示所有的数字。

参考博客:https://www.cnblogs.com/h5course/p/7566812.html

得出大数相乘,可以用数组来存储每一位数字,基本求解方法可以类比于小学数学乘法计算。(当24*5时,先用4*5得20,则个位数为0,进位为2;再用2*5+2得12,则十位为2,进位为1,。最后为[0,2,1]。数组倒置后即为乘法结果。)

我的答案:

function factorial(n){
// Add some code
if(n<0){return null;}
if(n==0 ||n==1){return "1";}
let result=[1]; //result数组存储当前阶乘结果
for(let num=2;num<=n;num++){
for(let i=0,plus=0 ; i<result.length || plus!=0 ; i++){
let count=(i<result.length)?(num*result[i]+plus):plus; //若当前i小于result所存数字的位数,则分别*num+plus;若等于,则直接进位。
result[i]=count%10; //个位、十位、百位……上的数字,存放在数组result中
plus=(count-result[i])/10;
}
}
return result.reverse().join(""); //将数组result倒序后,即为最后的阶乘结果
}

优秀答案:

function factorial(n) {
var res = [1];
for (var i = 2; i <= n; ++i) {
var c = 0; //c代表进位
for (var j = 0; j < res.length || c !== 0; ++j) {
c += (res[j] || 0) * i;
res[j] = c % 10; //分别求出个位、十位、百位……的数
c = Math.floor(c / 10);
}
}
return res.reverse().join("");
}

另外发现直接用python做,就没有这个问题出现。(用递归方法)

def fun(n):
if n<0:
return null
elif n==0 or n==1:
return ""
else:
return n*fun(n-1)

用for循环

def fun(n):
sum=1
if n<0:
return null
elif n==0 or n==1:
return ""
else:
for i in range(1,n+1):
sum*=i
return sum

哈哈哈!

codewars--js--Large Factorials--阶乘+大数阶乘的更多相关文章

  1. nyist28大数阶乘

    http://acm.nyist.net/JudgeOnline/problem.php?pid=28 大数阶乘 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 我们 ...

  2. 大数阶乘(c语言)

    大数阶乘.代码比较简单. #include<stdio.h> #include<string.h> #define MAXN 25000 // 如果你的阶乘N比较大,建议大一点 ...

  3. 【大数阶乘】NYOJ-28

    大数阶乘 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?   输入 输入一个整数 ...

  4. 大数阶乘 nyoj

    大数阶乘 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?   输入 输入一个整数 ...

  5. 【ACM】大数阶乘 - Java BigInteger实现

    大数阶乘 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它?   输入 输入一个整数 ...

  6. nyoj___大数阶乘

    http://acm.nyist.net/JudgeOnline/problem.php?pid=28 大数阶乘 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 我们都知 ...

  7. 大数阶乘(c++实现)

    #include <iostream>using namespace std;#define N 1000int BigNumFactorial(int Num[], int n);voi ...

  8. HDU 1133 Buy the Ticket (数学、大数阶乘)

    Buy the Ticket Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  9. for循环计算某个数的阶乘、阶乘和及其倒数的阶乘和

    //4的阶乘 int jc = 4; //定义一个变量用来代表要计算的数值 long jd =1; //定义最终输出的阶乘 for(int i = 1; i <= jc;i++) //定义循环加 ...

随机推荐

  1. 修改现有消息类让.net core项目支持Protobuf - 【无需使用 [ProtoBuf.ProtoContract] 的方法】

    前言 第二次发博客,希望大家多多鼓励!!! 又接无上老板的一个需求,需要让.net core消息发送端跟消息接收端通信的消息是protobuf格式的(基于protobuf比json小一倍数据量,独特的 ...

  2. 为BlueLake主题增加自定义icon图标

    一.前言 hexo 的 Bluelake 主题是我一直在用的,简单大方,很喜欢.但最近有了添加自定义 icon 图标的需求,比如,添加 "地址"."扫一扫".& ...

  3. Elasticsearch如何修改Mapping结构并实现业务零停机

    Elasticsearch 版本:6.4.0 一.疑问 在项目中后期,如果想调整索引的 Mapping 结构,比如将 ik_smart 修改为 ik_max_word 或者 增加分片数量 等,但 El ...

  4. 团队项目—Beta版本冲刺3

    博客介绍 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience 这个作业要求在哪里 https://w ...

  5. Linux网络文件共享服务之smaba

    一.SAMBA服务简介 samba是1991年由Andrew Tridgel开发实现,主要用于Windows和unix文件共享.samba实现了共享文件和打印,实现在线编辑,登录SAMBA用户的身份认 ...

  6. OpenCV各种绘制调用:线,矩形,圆,椭圆,文字

    OpenCV提供了各种绘制接口,可以往图片里画各种东西,这种功能可以为以后在图像上标记一些信息方便调试 // drawcall.cpp: 定义控制台应用程序的入口点. // #include &quo ...

  7. 下载 安装MYsql 服务器

    摘自 https://blog.csdn.net/youxianzide/article/details/85319106 https://www.2cto.com/database/201805/7 ...

  8. JVM性能分析 | 一次生产系统Full GC问题分析与排查总结

    一次生产系统Full GC问题分析与排查总结 背景 最近某线上业务系统生产环境频频CPU使用率过低,频繁告警,通过重启可以缓解,但是过了一段时间又会继续预警,线上两个服务节点相继出现CPU资源紧张,导 ...

  9. springcloud 依赖版本问题

    SpringCloud 版本: 版本名称 版本 Finchley snapshot版 Edgware snapshot版 Dalston SR1 当前最新稳定版本 Camden SR7 稳定版本 Br ...

  10. vue中导入bootstrap.css

    1.利用cnpm下载然后导入,用npm也是一样的: cnmp install bootstrap -S 在main.js文件下导入: import "bootstrap/dist/css/b ...