Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this loop terminate partway through?

I'm trying to write a program to find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.

The range will be an array of two numbers that will not necessarily be in numerical order.

For example, for 1 and 3 - find the smallest common multiple of both 1 and 3 that is evenly divisible by all numbers between 1 and 3.

Why does the loop stop at i = 510,000 (or something close to that) instead of 7,000,000, as I set it?

I also have a screenshot with the output:

function smallestCommons(arr) {
  
  var start;
  var finish;
  var something;
  
  if(arr[0] < arr[1]){start = arr[0]; finish = arr[1];}else{
      start = arr[1]; finish = arr[0];
    }
  
  for(var i = finish;i <= 7000000;i++){
    var boolea = true;
    for(var j = start;j <= finish;j++){
      if(i % j !== 0){boolea = false;break;} // 2 % 1
    }
    
    if(boolea)return i;
    
    something = i;
  }
  
  console.log("final i = " + i);
  
  return 0;
}
like image 476
Narcis Neacsu Avatar asked Jun 05 '26 11:06

Narcis Neacsu


1 Answers

Try to add this at the beginning of your loop

// noprotect

it must be that jsbin is forcing your code to exit from the loop. See source

like image 103
Gonzalo.- Avatar answered Jun 08 '26 01:06

Gonzalo.-