问题描述:

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. linux运行tomcat报错SEVERE: Unable to process Jar entry [avassist xxxx.class]

    tomcat的版本过低换成apache-tomcat-7.0.56以上的高版本的就可以了

  2. Nmap使用教程(一)

    基本扫描技术 扫描单个网络 nmap 192.168.1.1/www.baidu.com 扫描多个网络/目标 nmap 192.168.1.1 192.168.1.2 #将扫描同个网段内不同的ip地址 ...

  3. PSP第一次总结

    项目计划总结 周活动总结表 姓名:王金萱                                                                                 ...

  4. Python通过win32 com接口实现offic自动化

    最近几天通过Python做一些自动生成office报表的东东,比如解析.xml文件,导出.html/WORD/PPT等格式,html不足一提,只需要简单的html静态网页知识即可,这儿要说的是怎么生成 ...

  5. 启动Ubuntu的时候出现黑屏的情况

    在启动Ubuntu的时候出现黑屏的情况,是因为升级了内核导致显卡不兼容,启动的时候应该告诉内核不要加载显卡: 在进入系统选择时按e进入编辑 在quiet splash 后面添加 nomodeset 再 ...

  6. Beta版本

    Beta版本 博客说明 这个作业属于哪个课程 http://edu.cnblogs.com/campus/xnsy/GeographicInformationScience 这个作业的要求在哪里 ht ...

  7. 快速构建第三方api应用

    1.使用框架和扩展 详细请看composer.json "php": "^7.1.3", "laravel-admin-ext/config" ...

  8. [校内训练20_01_17]ABC

    1.平面上每次加入直角边平行于坐标轴的等腰直角三角形,每次询问某个点被覆盖了多少次. 大常数算法:O(nlog^2) #include<bits/stdc++.h> using names ...

  9. RocketMQ消息模型

    rocketmq采用的是发布-订阅的模式,不需要每个消费者维护自己的消息队列,生产者将消息发送到topic,消费者订阅此topic 读取消息. 基本概念: 消息模型:消息模型包括producer,co ...

  10. docker启动redis端口映射错误问题解决

    今天使用docker安装redis,使用的时候出现了一些问题.第一次安装好后,在虚拟机后台启动了redis,然后在连接虚拟机的redis的时候怎么也连不上.出现这种情况我第一反应是防火墙没有开启所以我 ...