3

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?

2
  • 1
    make another variable to hold the result. Rather than changing the value of the parameter. Commented Apr 13, 2017 at 22:16
  • @Lixus: Please answer in the answer section (clue's in the name) Commented Apr 13, 2017 at 22:18

9 Answers 9

8

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.

enter image description here

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;">

Sign up to request clarification or add additional context in comments.

1 Comment

num increases exponentially, not logarithmically.
2

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

Comments

1

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);

Comments

0

Because you change the value of 'num' in every iteration. You should use a third variable to count the product.

Comments

0

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.

Comments

0

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.

Comments

0

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)
... 

Comments

0

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).

5 Comments

It will if num is <= 0 at the start of the function.
@BoundaryImpositio Yes you are right. But for value larger than 1, this will always be an infinite loop.
You said "never" :)
@BoundaryImposition Just made an edit. Thanks for noticing this! :)
No problem Omar!
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.