Last active
August 25, 2017 20:04
-
-
Save tobloef/ba09cb90ff4a78dc6b50985ad84671e1 to your computer and use it in GitHub Desktop.
Finds numbers that are both palindromes and squares
This file contains 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
'use strict'; | |
function findPalinSquare(n) { | |
let nextRoot = Math.ceil(Math.sqrt(n + 1)); | |
while (true) { | |
const nextSquare = nextRoot * nextRoot; | |
if (isPalin(nextSquare)) { | |
return nextSquare; | |
} | |
nextRoot++; | |
} | |
} | |
function isPalin(n) { | |
const str = n.toString(); | |
const indexes = str.length - 1; | |
const half = Math.floor(str.length / 2); | |
for (let i = 0; i < half; i++) { | |
if (str[i] !== str[indexes - i]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
console.log('Crunching numbers...'); | |
let count = 0; | |
let prev = 1; | |
while (true) { | |
prev = findPalinSquare(prev); | |
count++; | |
console.log(new Date().toLocaleString(), count, prev); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment