Last active
June 1, 2020 06:57
-
-
Save thunder775/bd82afd650564d335d016ecd5c8229ea to your computer and use it in GitHub Desktop.
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
function interprime(num = 0) { | |
if (num === 0 || isPrime(num)) return []; | |
let prevPrime = num - 1; | |
while (!isPrime(prevPrime) && prevPrime > 0) { | |
prevPrime--; | |
} | |
let nextPrime = num + 1; | |
while (!isPrime(nextPrime)) { | |
nextPrime++; | |
} | |
return num - prevPrime === nextPrime - num ? [prevPrime, nextPrime] : []; | |
} | |
function isPrime(num = 1) { | |
if (num === 0 || num === 1) return false; | |
for (let i = 2; i < Math.ceil(num / 2); i++) { | |
if (num % i === 0) { | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment