Skip to content

Instantly share code, notes, and snippets.

@tobloef
Last active August 25, 2017 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobloef/ba09cb90ff4a78dc6b50985ad84671e1 to your computer and use it in GitHub Desktop.
Save tobloef/ba09cb90ff4a78dc6b50985ad84671e1 to your computer and use it in GitHub Desktop.
Finds numbers that are both palindromes and squares
'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