Reputation: 350
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?
Upvotes: 3
Views: 327
Reputation: 136
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);
Upvotes: 1
Reputation: 45
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);
Upvotes: 0
Reputation: 41893
Lets 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;">
Upvotes: 8
Reputation: 174
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).
Upvotes: 0
Reputation: 441
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)
...
Upvotes: 0
Reputation: 867
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.
Upvotes: 0
Reputation: 385154
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
Upvotes: 2
Reputation: 29
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
.
Upvotes: 0
Reputation: 678
Because you change the value of 'num' in every iteration. You should use a third variable to count the product.
Upvotes: 0