Skip to content

Instantly share code, notes, and snippets.

@susansilver
Last active December 19, 2024 13:14
Show Gist options
  • Save susansilver/90f52f45187af10f11610a0ddae1f65d to your computer and use it in GitHub Desktop.
Save susansilver/90f52f45187af10f11610a0ddae1f65d to your computer and use it in GitHub Desktop.
A program for the Recurse Center application
/*
If a prime is still prime when you reverse the digits
and it is a different prime, then it is an emirp.
https://en.wikipedia.org/wiki/Emirp
The sequence begins with 13 and 2,3,5,7,11
should not return as emirp because they are palindromes.
*/
function isNotNum(num) {
return /[^0-9]/.test(String(num));
}
function isPrime(num) {
/* Starts at 2 because all numbers are divisible by 1.
It runs until i < num because all primes are
divisible by themselves. */
for (let i = 2; i < num; i++) {
/*if the number is divisible by any number
other than itself and one it is not prime */
if (num % i === 0) {
return false;
}
}
/* 0 and 1 are not prime */
return num > 1;
}
function isEmirp(num) {
const reversePrime = parseInt(num.toString().split("").reverse().join(""));
if (!isPrime(num) || isNotNum(num)) {
console.log(`${num} is not a prime`);
} else if (isPrime(reversePrime) && reversePrime !== num) {
console.log(`${num} is an emirp (reverse is ${reversePrime})`);
} else {
console.log(`${num} is prime but not an emirp`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment