js怎样实现分解数字?教你三个办法
Admin 2021-05-28 群英技术资讯 2399 次浏览
对于新手来说,非负整数n的阶乘是比较难理解的一个算法,对此,这篇文章给大家分享的是有关js如何实现分解数字的内容、下面会介绍使用递归分解一个数字、使用WHILE循环分解一个数字和使用FOR循环分解数字。
算法挑战
返回提供的整体的阶乘。如果整体用字母n表示,则阶乘是所有小于或等于n的正整数的乘积。阶乘经常用简写符号n!表示!
例如:5!= 1 * 2 * 3 * 4 * 5 = 120
function factorialize(num) {
return num;
}
factorialize(5);
提供的测试用例
什么是因数分解?
当将一个因数分解时,就是称为数字乘以每个连续的数字减一个。
如果您的电话号码是5,则您将:
5! = 5 * 4 * 3 * 2 * 1
该模式为:
0! = 1
1! = 1
2! = 2 * 1
3! = 3 * 2 * 1
4! = 4 * 3 * 2 * 1
5! = 5 * 4 * 3 * 2 * 1
1.递归分解一个数字
function factorialize(num) {
// If the number is less than 0, reject it.
if (num < 0)
return -1;
// If the number is 0, its factorial is 1.
else if (num == 0)
return 1;
// Otherwise, call the recursive procedure again
else {
return (num * factorialize(num - 1));
/*
First Part of the recursion method
You need to remember that you won't have just one call, you'll have several nested calls
Each call: num === "?" num * factorialize(num - 1)
1st call -C factorialize(5) will return 5 * factorialize(5 - 1) // factorialize(4)
2nd call -C factorialize(4) will return 4 * factorialize(4 - 1) // factorialize(3)
3rd call -C factorialize(3) will return 3 * factorialize(3 - 1) // factorialize(2)
4th call -C factorialize(2) will return 2 * factorialize(2 - 1) // factorialize(1)
5th call -C factorialize(1) will return 1 * factorialize(1 - 1) // factorialize(0)
Second part of the recursion method
The method hits the if condition, it returns 1 which num will multiply itself with
The function will exit with the total value
5th call will return (5 * (5 - 1)) // num = 5 * 4
4th call will return (20 * (4 - 1)) // num = 20 * 3
3rd call will return (60 * (3 - 1)) // num = 60 * 2
2nd call will return (120 * (2 - 1)) // num = 120 * 1
1st call will return (120) // num = 120
If we sum up all the calls in one line, we have
(5 * (5 - 1) * (4 - 1) * (3 - 1) * (2 - 1)) = 5 * 4 * 3 * 2 * 1 = 120
*/
}
}
factorialize(5);
没有注释:
function factorialize(num) {
if (num < 0)
return -1;
else if (num == 0)
return 1;
else {
return (num * factorialize(num - 1));
}
}
factorialize(5);
2.用WHILE循环分解一个数字
function factorialize(num) {
// Step 1. Create a variable result to store num
var result = num;
// If num = 0 OR num = 1, the factorial will return 1
if (num === 0 || num === 1)
return 1;
// Step 2. Create the WHILE loop
while (num > 1) {
num--; // decrementation by 1 at each iteration
result = result * num; // or result *= num;
/*
num num-- var result result *= num
1st iteration: 5 4 5 20 = 5 * 4
2nd iteration: 4 3 20 60 = 20 * 3
3rd iteration: 3 2 60 120 = 60 * 2
4th iteration: 2 1 120 120 = 120 * 1
5th iteration: 1 0 120
End of the WHILE loop
*/
}
// Step 3. Return the factorial of the provided integer
return result; // 120
}
factorialize(5);
没有注释:
function factorialize(num) {
var result = num;
if (num === 0 || num === 1)
return 1;
while (num > 1) {
num--;
result *= num;
}
return result;
}
factorialize(5);
3.使用FOR循环分解数字
function factorialize(num) {
// If num = 0 OR num = 1, the factorial will return 1
if (num === 0 || num === 1)
return 1;
// We start the FOR loop with i = 4
// We decrement i after each iteration
for (var i = num - 1; i >= 1; i--) {
// We store the value of num at each iteration
num = num * i; // or num *= i;
/*
num var i = num - 1 num *= i i-- i >= 1?
1st iteration: 5 4 = 5 - 1 20 = 5 * 4 3 yes
2nd iteration: 20 3 = 4 - 1 60 = 20 * 3 2 yes
3rd iteration: 60 2 = 3 - 1 120 = 60 * 2 1 yes
4th iteration: 120 1 = 2 - 1 120 = 120 * 1 0 no
5th iteration: 120 0 120
End of the FOR loop
*/
}
return num; //120
}
factorialize(5);
没有注释:
function factorialize(num) {
if (num === 0 || num === 1)
return 1;
for (var i = num - 1; i >= 1; i--) {
num *= i;
}
return num;
}
factorialize(5);
以上就是关于js分解数字的方法分享,上述介绍了三种办法及示例,感兴趣的朋友可以参考,希望大家阅读完这篇文章能有所收获。
文本转载自脚本之家
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
如果有使用过jQuery,都知道不同版本的jQuery存在冲突问题。因此,不同版本会有$冲突,那么我们要如何处理这个冲突呢?下面我们一起来看一看。
本文主要介绍了TypeScript 接口继承的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章主要介绍了Vue2的diff算法的相关资料,帮助大家更好的理解和使用vue框架,感兴趣的朋友可以了解下
vue的防抖和节流是什么意思?一些朋友可能对防抖和节流不是很了解,对此这篇文章就给大家具体的介绍一下什么是防抖和节流,感兴趣的朋友接下来跟随小编一起学习一下吧。
随着 Vite2 的发布并日趋稳定,现在越来越多的项目开始尝试使用它。本文我们就介绍了Vite创建项目的实现步骤,感兴趣的可以了解一下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008