本文基于免费代码营基本算法脚本“分解数字”
在数学中,非负整数n的阶乘可能是一个棘手的算法。在本文中,我将解释这种方法,首先使用递归函数,第二种使用而循环,第三种使用以循环。
算法挑战
返回提供的整体的阶乘。
如果整体用字母n表示,则阶乘是所有小于或等于n的正整数的乘积。
阶乘经常用简写符号n!表示!
例如:5!= 1 * 2 * 3 * 4 * 5 = 120
function factorialize(num) { return num; } factorialize(5);
提供的测试用例
- factorialize(0)应该返回1
- factorialize(5)应该返回120
- factorialize(10)应该返回3628800
- factorialize(20)应该返回2432902008176640000
什么是因数分解?
当将一个因数分解时,就是称为数字乘以每个连续的数字减一个。
如果您的电话号码是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 – factorialize(5) will return 5 * factorialize(5 - 1) // factorialize(4) 2nd call – factorialize(4) will return 4 * factorialize(4 - 1) // factorialize(3) 3rd call – factorialize(3) will return 3 * factorialize(3 - 1) // factorialize(2) 4th call – factorialize(2) will return 2 * factorialize(2 - 1) // factorialize(1) 5th call – 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"htmlcode">function factorialize(num) { if (num === 0 || num === 1) return 1; for (var i = num - 1; i >= 1; i--) { num *= i; } return num; } factorialize(5);
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年11月26日
2024年11月26日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]