codewars--js--Large Factorials--阶乘+大数阶乘
问题描述:
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 null, nil 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--阶乘+大数阶乘的更多相关文章
- nyist28大数阶乘
http://acm.nyist.net/JudgeOnline/problem.php?pid=28 大数阶乘 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 我们 ...
- 大数阶乘(c语言)
大数阶乘.代码比较简单. #include<stdio.h> #include<string.h> #define MAXN 25000 // 如果你的阶乘N比较大,建议大一点 ...
- 【大数阶乘】NYOJ-28
大数阶乘 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它? 输入 输入一个整数 ...
- 大数阶乘 nyoj
大数阶乘 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它? 输入 输入一个整数 ...
- 【ACM】大数阶乘 - Java BigInteger实现
大数阶乘 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 我们都知道如何计算一个数的阶乘,可是,如果这个数很大呢,我们该如何去计算它并输出它? 输入 输入一个整数 ...
- nyoj___大数阶乘
http://acm.nyist.net/JudgeOnline/problem.php?pid=28 大数阶乘 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 我们都知 ...
- 大数阶乘(c++实现)
#include <iostream>using namespace std;#define N 1000int BigNumFactorial(int Num[], int n);voi ...
- HDU 1133 Buy the Ticket (数学、大数阶乘)
Buy the Ticket Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- for循环计算某个数的阶乘、阶乘和及其倒数的阶乘和
//4的阶乘 int jc = 4; //定义一个变量用来代表要计算的数值 long jd =1; //定义最终输出的阶乘 for(int i = 1; i <= jc;i++) //定义循环加 ...
随机推荐
- 使用 LinkedBlockingQueue 实现简易版线程池
前一阵子在做联系人的导入功能,使用POI组件解析Excel文件后获取到联系人列表,校验之后批量导入.单从技术层面来说,导入操作通常情况下是一个比较耗时的操作,而且如果联系人达到几万.几十万级别,必须拆 ...
- 第二阶段冲刺个人任务——three
今日任务: 优化统计个人博客结果页面的显示. 昨日成果: 优化作业查询结果,按学号排列.
- List自定义排序 (例子省份排序)
//待排序集合 List<String> list=new ArrayList<String>(); list.add("辽宁"); list.add(&q ...
- 电脑开机后多了OneKey Ghost启动选项怎么解决
原文地址:http://www.xitongcheng.com/jiaocheng/dnrj_article_18745.html 大多数用户在使用OneKey Ghost安装电脑系统后,会在开机启动 ...
- java集合与数组之间转换
数组转换为集合 采用java中集合自带的asList()方法就可以完成转换了 String[] array = new String[] {"zhu", "wen&quo ...
- Firebase REST API
use firebase and firesharp to do a Library management system . look github.com/ziyasal/FireSharp
- 数据结构——栈的应用 NOI2.2 括号匹配问题
栈是一种数据结构,相当于一个容器,将一个又一个变量从顶端压进去,需要使用时,又从顶端拿出来,其具体使用方法,下面是详细讲解: #include<stack>必须使用此头文件 stack&l ...
- Windows版Redis主从配置
一.下载 从github上下载Redis的zip包,地址:https://github.com/MicrosoftArchive/redis/releases Redis本身不支持windows,这是 ...
- hadoop_2.6.5集群安装
安装hadoop2.6.5集群: 1.规划设计: JacK6:NameNode,jobtracker JacK7:secondnode,datenode,tasktracker JacK8:datan ...
- lua 中 . 和 : 的区别
lua 中 . 和 : 的区别 首先在lua中使用":"定义的函数会自动传入一个名为self的变量,这个变量是隐含的,self同c++中的this一样,表示当前对象的指针:而&qu ...