Skip to content

Instantly share code, notes, and snippets.

@sjdonado
Created December 14, 2019 22:02
Show Gist options
  • Save sjdonado/f8434f4ab480a4c09f055416ae164761 to your computer and use it in GitHub Desktop.
Save sjdonado/f8434f4ab480a4c09f055416ae164761 to your computer and use it in GitHub Desktop.
The sum of the first 10 truncatable primes
let tPrimesCounter = 0;
let sum = 0;
let num = 7;
function isPrime(num, div = 2) {
if (num < div || num % div === 0) {
return false;
}
if (div === Math.ceil(Math.sqrt(num))) {
return true;
}
return isPrime(num, div + 1);
}
while (tPrimesCounter < 10) {
num += 1;
subNum = num;
const subNumLen = String(subNum).length;
let index = 0;
while (index < subNumLen) {
subNumR = subNum % Math.pow(10, subNumLen - index);
subNumL = Math.floor(subNum / Math.pow(10, index));
if (!isPrime(subNumL) || !isPrime(subNumR)) {
break;
}
index += 1;
}
if (index === subNumLen) {
tPrimesCounter += 1;
sum += num;
}
}
console.log('Sum: ', sum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment