Created
April 15, 2018 20:38
-
-
Save nyk0r/e73f144948bc480f791509b11fadf161 to your computer and use it in GitHub Desktop.
This file contains hidden or 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* 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