This article is based on Free Code Camp Basic Algorithm Scripting “Factorialize a Number”
本文基于Free Code Camp基本算法脚本“ 分解数字 ”
In mathematics, the factorial of a non-negative integer n can be a tricky algorithm. In this article, I’m going to explain three approaches, first with the recursive function, second using a while loop and third using a for loop.
在数学中 ,非负整数n的阶乘可能是一个棘手的算法。 在本文中,我将解释三种方法,第一种使用递归函数,第二种使用while循环,第三种使用for循环。
We have already seen a recursion approach on a String in the previous article, How to Reverse a String in JavaScript in 3 Different Ways ? This time we will apply the same concept on a number.
在上一篇文章如何在JavaScript中以3种不同的方式反转字符串的文章中,我们已经看到了对字符串的递归方法? 这次,我们将在数字上应用相同的概念。
算法挑战 (Algorithm Challenge)
Return the factorial of the provided integer.
返回提供的整数的阶乘。
Return the factorial of the provided integer.
返回提供的整数的阶乘。
If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.
如果整数用字母n表示,则阶乘是所有小于或等于n的正整数的乘积。
If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.
如果整数用字母n表示,则阶乘是所有小于或等于n的正整数的乘积。
Factorials are often represented with the shorthand notation n!
阶乘经常用简写符号n!表示!
Factorials are often represented with the shorthand notation n!
阶乘经常用简写符号n!表示!
For example: 5! = 1 * 2 * 3 * 4 * 5 = 120
例如: 5! = 1 * 2 * 3 * 4 * 5 = 120
function factorialize(num) {
return num;
}
factorialize(5);
提供的测试用例 (Provided test cases)
factorialize(0) should return 1
factorialize(0)应该返回1
factorialize(5) should return 120
factorialize(5)应该返回120
factorialize(10) should return 3628800
factorialize(10)应该返回3628800
factorialize(20) should return 2432902008176640000
factorialize(20)应该返回2432902008176640000
什么是因数分解? (What is factorializing a number all about?)
When you factorialize a number, you are multiplying that number by each consecutive number minus one.
当将一个因数分解时,就是将该数字乘以每个连续的数字减一个。
If your number is 5, you would have:
如果您的电话号码是5,则您将拥有:
5! = 5 * 4 * 3 * 2 * 1
The pattern would be:
该模式为:
0! = 1
1! = 1
2! = 2 * 1
3! = 3 * 2 * 1
4! = 4 * 3 * 2 * 1
5! = 5 * 4 * 3 * 2 * 1
1.递归分解一个数字 (1. Factorialize a Number With Recursion)
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);
没有评论: (Without comments:)
function factorialize(num) {
if (num < 0)
return -1;
else if (num == 0)
return 1;
else {
return (num * factorialize(num - 1));
}
}
factorialize(5);
2.使用WHILE循环分解数字 (2. Factorialize a Number with a WHILE loop)
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);
没有评论: (Without comments:)
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循环分解数字 (3. Factorialize a Number with a FOR loop)
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);
没有评论: (Without comments:)
function factorialize(num) {
if (num === 0 || num === 1)
return 1;
for (var i = num - 1; i >= 1; i--) {
num *= i;
}
return num;
}
factorialize(5);
I hope you found this helpful. This is part of my “How to Solve FCC Algorithms” series of articles on the Free Code Camp Algorithm Challenges, where I propose several solutions and explain step-by-step what happens under the hood.
希望对您有所帮助。 这是我的“如何解决FCC算法”系列文章的一部分,有关自由代码训练营算法挑战,我在其中提出了几种解决方案并逐步解释了幕后情况。
在JavaScript中重复字符串的三种方法 在本文中,我将解释如何解决freeCodeCamp的“重复字符串重复字符串”挑战。 这涉及…
在JavaScript中确认字符串结尾的两种方法 在本文中,我将解释如何解决freeCodeCamp的“确认结尾”挑战。
在JavaScript中反转字符串的三种方法 本文基于Free Code Camp基本算法脚本“反转字符串”
用JavaScript检查回文的两种方法 本文基于Free Code Camp基本算法脚本“检查回文”。
在JavaScript中查找字符串中最长单词的三种方法 本文基于Free Code Camp基本算法脚本“查找字符串中最长单词”。
用JavaScript给句子加标题的三种方法 本文基于Free Code Camp基本算法脚本“标题加句子”。
使用JavaScript 在 数组中找到最大数字的三种方法 在本文中,我将解释如何解决Free Code Camp的“在数组中返回最大数字”挑战。 这个…
If you have your own solution or any suggestions, share them below in the comments.
如果您有自己的解决方案或任何建议,请在下面的评论中分享。
Or you can follow me on Medium, Twitter, Github and LinkedIn, right after you click the green heart below ;-)
或者,您也可以在单击下面的绿色心脏之后立即在Medium , Twitter , Github和LinkedIn上关注我;-)
#StayCurious, #KeepOnHacking & #MakeItHappen!
#StayCurious,#KeepOnHacking和#MakeItHappen!
翻译自: https://www.freecodecamp.org/news/how-to-factorialize-a-number-in-javascript-9263c89a4b38/