Skip to content

Instantly share code, notes, and snippets.

@P1xt
Created August 5, 2014 08:58
Show Gist options
  • Save P1xt/ee24ca70ee7b177d0511 to your computer and use it in GitHub Desktop.
Save P1xt/ee24ca70ee7b177d0511 to your computer and use it in GitHub Desktop.
Prime Palindrome
var highEnd = 1000;
var primes = sieve(highEnd);
highEnd--;
var found = false;
while (found == false ) {
if (isPalindrome(highEnd.toString()) == true) {
if (primes[highEnd] == true) {
found = true;
}
}
highEnd--;
}
console.log(++highEnd);
function isPalindrome(input) {
var i;
var length = input.length -1;
for (i=0; i< length; i++) {
if (input[i] != input[length-i]) {
return false;
}
}
return true;
}
function sieve (highEnd) {
var sieve = newFilledArray(highEnd, true);
var maxCheck = Math.sqrt(highEnd);
var i, j;
var results = [];
for (i = 2; i <= maxCheck; i++) {
if (sieve[i] == true) {
for (j = i * i; j < highEnd; j += i) {
sieve[j] = false
}
}
}
for (i=2; i<highEnd; i++) {
if (sieve[i]==true) {
results.push(i);
}
}
return(sieve);
}
function newFilledArray(length, val) {
var array = [];
for (var i = 0; i < length; i++) {
array[i] = val;
}
return array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment