Smallest Common Multiple Potential Infinite Loop for only one Test Case

Smallest Common Multiple Potential Infinite Loop for only one Test Case
0

#1

Hey everyone,
I’m having a weird issue on the Smallest Common Multiple Assignment. Here’s my code:

function smallestCommons(arr) {
  if(arr[0] > arr[1]) {
    var temp = arr[0];
    arr[0] = arr[1];
    arr[1] = temp;
  }
  for(var count = arr[1]; count < 10000000; count++) {
    var divisible = true;
    for(var count2 = arr[0]; count2 <= arr[1]; count2++) {
      if(count%count2 != 0) {
        divisible = false;
        break;
      }
    }
    if(divisible == true) {
      arr = count;
      break;
    }
  }
  return arr;
}

smallestCommons([1,5]);

The test cases are as follows:
smallestCommons([1, 5]) should return a number.
smallestCommons([1, 5]) should return 60.
smallestCommons([5, 1]) should return 60.
smallestCommons([1, 13]) should return 360360.
smallestCommons([23, 18]) should return 6056820.

Everything works except the last test case. When I plug that test case’s numbers into the program it gives me a potential infinite loop error. How is it possible that I have a potential infinite loop for only one test case?


#2

The infinite loop detection is timer based, so if an execution takes a long time it will trigger the warning.

Just so you know, it is possible to create a function that will cause an infinite loop only in certain circumstances which means that it is possible for only one test to cause it.

You can turn off infinite loop protection if you are very confident that you haven’t created an infinite loop.

FYI: all the challenges can be solved in a way that doesn’t trigger the infinite loop protection.


#3

You do not have an infinite loop, but because your algorithm is not as efficient as it could be (and takes longer than the FCC code runner expects), the FCC code runner assumes there could be an infinite loop. Read more about this issue and how you can temporarily disable the infinite loop protection from the link below.


#4

Ah, I figured it could be that the algorithm was taking too long and hence the program was treated as an infinite loop. I just didn’t know for sure. Thank you for the speedy answers! I’ll try to make the algorithm more efficient and if that takes too much time I’ll just disable the infinite loop protection!