Skip to content

Instantly share code, notes, and snippets.

@nyk0r
Created April 15, 2018 20:38
Show Gist options
  • Save nyk0r/e73f144948bc480f791509b11fadf161 to your computer and use it in GitHub Desktop.
Save nyk0r/e73f144948bc480f791509b11fadf161 to your computer and use it in GitHub Desktop.
function* range(a, b, pred) {
while (a < b) {
if (typeof pred === 'undefined' || pred(a)) {
yield a;
}
++a;
}
}
function isPalindrome(val) {
val = val.toString();
const mid = val.length / 2;
for (let off = 0; off < mid; off++) {
if (val[val.length - 1 - off] !== val[off]) {
return false;
}
}
return true;
}
const primes = [...range(10000, 100000, a => {
for (let x of range(2, Math.sqrt(a) + 1)) {
if (a % x === 0) {
return false;
}
}
return true;
})].reverse();
function getExpr() {
for (let a of primes) {
for (let b of primes) {
const c = a * b;
if (isPalindrome(c)) {
return `${a} * ${b} = ${c}`;
}
}
}
}
console.time();
console.log(getExpr());
console.timeEnd();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment