Created
December 14, 2019 22:02
-
-
Save sjdonado/f8434f4ab480a4c09f055416ae164761 to your computer and use it in GitHub Desktop.
The sum of the first 10 truncatable primes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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