I'm trying to return the factorial of the provided integer. When doing it like this, e.g:
factorialize(num) {
for (var i = 1;i <= num; i++){
num*=i;
}
return num;
}
factorialize(5);
I'm getting an infinite loop. While I understand that this shouldn't give me the correct answer because my understanding of it would go something like this:
n! = 5 * 1 * 2 * 3 * 4 * 5 = 600
when really it should go:
n! = 1 * 2 * 3 * 4 * 5 = 120
But still, I don't understand why I am getting an infinite loop here?
I'm trying to return the factorial of the provided integer. When doing it like this, e.g:
factorialize(num) {
for (var i = 1;i <= num; i++){
num*=i;
}
return num;
}
factorialize(5);
I'm getting an infinite loop. While I understand that this shouldn't give me the correct answer because my understanding of it would go something like this:
n! = 5 * 1 * 2 * 3 * 4 * 5 = 600
when really it should go:
n! = 1 * 2 * 3 * 4 * 5 = 120
But still, I don't understand why I am getting an infinite loop here?
Share Improve this question edited Jan 9, 2019 at 16:31 Cœur 38.8k25 gold badges206 silver badges278 bronze badges asked Apr 13, 2017 at 22:14 KestVirKestVir 3503 silver badges16 bronze badges 2- 1 make another variable to hold the result. Rather than changing the value of the parameter. – Ousmane D. Commented Apr 13, 2017 at 22:16
- @Lixus: Please answer in the answer section (clue's in the name) – Lightness Races in Orbit Commented Apr 13, 2017 at 22:18
9 Answers
Reset to default 8Lets say that the value of num
variable is 2
.
First cycle:
for (var i = 1; i <= 2; i++) { //true num *= i; //2 = 2 * 1 => 2 }
Second cycle:
for (var i = 2; i <= 2; i++) { //true num *= i; //2 = 2 * 2 => 4 }
Third cycle:
for (var i = 3; i <= 4; i++) { //true num *= i; //4 = 4 * 3 => 12 }
Fourth cycle:
for (var i = 4; i <= 12; i++) { //true num *= i; //12 = 12 * 4 => 48 }
The num
value increases exponentially, while the i
value increases linearly.
i <= num
condition will be always fulfilled, that's why you are getting an infinite loop.
Snippet including the chart:
var num = {
x: [1, 2, 3, 4, 5],
y: [2, 4, 12, 48, 240],
type: 'scatter',
name: 'num'
};
var i = {
x: [1, 2, 3, 4, 5],
y: [1, 2, 3, 4, 5],
type: 'scatter',
name: 'i'
};
var data = [num, i];
Plotly.newPlot('myDiv', data);
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<div id="myDiv" style="width: 480px; height: 400px;">
Your loop is chasing a moving target.
It will end when i
reaches num
, but you keep making num
bigger; actually i
will never reach num
.
As you pointed out, your algorithm is wrong anyway.
function factorialize(num) {
var result = 1;
for (var i = 2; i <= num; i++) {
result *= i;
}
return result;
}
console.log(factorialize(0)); // 1
console.log(factorialize(1)); // 1
console.log(factorialize(2)); // 2
console.log(factorialize(3)); // 6
console.log(factorialize(4)); // 24
console.log(factorialize(5)); // 120
The variable that you use on the for loop it's the same variable that you use to store the multiplication.
This should work:
factorialize(num) {
var len = num;
for (var i = 1;i <= len; i++){
num*=i;
}
return num;
}
factorialize(5);
Because you change the value of 'num' in every iteration. You should use a third variable to count the product.
You are getting an infinite loop because you are always updating num. You are doing num = num * i
. So it can never reach the end of the loop. i
will never be greater then num
.
Taking your example:
Before run 1: num = 5; After run 1: num = 5; After run 2: num = 10; After run 3: num = 30;
As per your condition, loop would stop only when num is more than i, which would never be the case, as you're modifying it's value on every iteration by a huge difference.
here i
will never catch num
i = 1 num = 5 1 <= 5 //(num = 5*1)
i = 2 num = 5 2 <= 10 //(num = 5*2)
i = 3 num = 10 3 <= 30 //(num = 10*3)
...
This is because you are dynamically updating the value of num
. So, after each iteration, the number gets bigger and bigger, and that's why the value of i
will never be more than the value of num
(for values of num
larger than 1).
The end condition for i<=num
is always true
hence the infinite loop because the argument num
keeps increasing with each iteration.
A better approach is
function factorialize(num) {
var res = 1;
if(num ===0 || num === 1){
return 1;
}
for (var i = 2;i <= num; i++){
res*=i;
}
return res;
}
factorialize(5);