Project Euler Problem 37: Truncatable Primes

function truncatablePrimes(n) {
  var numPrimesFound = 0;
  var sum = 0;
  // generates all possible candidates; we start at 10 as it's the smallest double digit number - single digit numbers cannot be truncated. we end once we find n number of primes.
  for(var num = 10; numPrimesFound < n; num++){
      // if a candidate is truncatable left and right, then we add both to the total found and the sum.
    if(truncatableLtRAndRtL(num)){
        //console.log(num);
      numPrimesFound++;
      sum += num;
    }
  }
  return sum;
}

// returns a boolean whether a given number can be truncated left to right and right to left while remaining prime the whole way through.
function truncatableLtRAndRtL(num){
  var numText = num.toString();
  if(!isPrime(num)){
    return false;
  }
  // the first stage of deletion deletes one digit, the last stage spares one digit
  for(var del = 1; del < numText.length; del++){
    // digits are deleted from left to right
    var leftToRight = numText.substring(del);
    // digits are deleted from right to left
    var rightToLeft = numText.substring(0, numText.length - del);
    // if either isn't prime, return false early
    if(!(isPrime(leftToRight) && isPrime(rightToLeft))){
      return false;
    }
  }
  return true;
}

// given an integer, returns boolean of whether it's prime.
function isPrime(num){
  const smallestPrime = 2;
  if(num < smallestPrime){
    return false;
  }
  // we only need to divide a number up to its square root to determine if it is prime or not; if we get to the square root and do not find a whole factor, then the number is prime.
  for(var divisor = 2; divisor <= Math.sqrt(num); divisor++){
    // if the number can be cleanly divided by any whole number that isn't 0 or itself, then it's NOT prime
    if((num / divisor) % 1 == 0){
      return false;
    }
  }
  return true;
}

When running this, I keep getting an error message saying that line 5 may have an infinite loop.

The test case truncatablePrimes(11) should return 748317’ seems to fail too.

Strangely enough, when my code is run on an online compiler (like Programiz’s JS compiler), that test case comes out perfectly right.

Important to note is that all test cases seem to take roughly 3 seconds on my end on FreeCodeCamp’s compiler and roughly the same time on Programiz, so I don’t think it’s timing out (FreeCodeCamp’s compiler tells the end user if a timeout has happened).

What can I do to resolve this issue? My adblocker is disabled on the FreeCodeCamp site; I am aware adblockers and extension scripts are known to cause test case failures.

3 seconds is definitely long enough to trigger infinite loop protection.

You definitely don’t need division in your prime checker. Modulus will work fine.

Better yet, use a sieve

1 Like

Thank you! I somehow didn’t think about the modulus trick.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.