Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active December 18, 2024 11:28
Show Gist options
  • Save sandrabosk/17319bb427dc4e457595d6670a455f57 to your computer and use it in GitHub Desktop.
Save sandrabosk/17319bb427dc4e457595d6670a455f57 to your computer and use it in GitHub Desktop.
// ************************************************************************************
// https://www.codewars.com/kata/5539fecef69c483c5a000015/javascript
// Find all Backwards Read Primes between two positive given numbers (both inclusive),
// the second one always being greater than or equal to the first one.
//The resulting array or the resulting string will be ordered following the natural order
// of the prime numbers.
// Example
// backwardsPrime(2, 100) => [13, 17, 31, 37, 71, 73, 79, 97]
// backwardsPrime(9900, 10000) => [9923, 9931, 9941, 9967]
// backwardsPrime(501, 599) => []
// ************************************************************************************
function backwardsPrime(start, stop) {
// your code here
let result = [];
for (let i = start; i <= stop; i++) {
// check if the number is prime
// if it is prime check if the backwards no is prime too
if (isPrime(i) && isReversePrime(i)) {
result.push(i);
}
}
return result;
}
function isPrime(num) {
for (let i = 2; i <= Math.sqrt(num); i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
function isReversePrime(num) {
let reverseNum = +num.toString().split("").reverse().join("");
if (num !== reverseNum) {
return isPrime(reverseNum);
}
return false;
}
@gqueen001
Copy link

Greate job!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment