本文基于免费代码营基本算法脚本“分解数字”

在数学中,非负整数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);
标签:
js分解数字,js分解数字的方法

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
狼山资源网 Copyright www.pvsay.com

评论“详解JavaScript中分解数字的三种方法”

暂无“详解JavaScript中分解数字的三种方法”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。