Skip to content

Instantly share code, notes, and snippets.

@alexko30
Created April 30, 2018 22:44
Show Gist options
  • Save alexko30/55e86dec3a2ac83f1fa2f1eac6e8c14e to your computer and use it in GitHub Desktop.
Save alexko30/55e86dec3a2ac83f1fa2f1eac6e8c14e to your computer and use it in GitHub Desktop.
automorphic
function automorphic(number) {
const numberArr = Array.from(number.toString()).map(Number);
const square = Math.pow(number, 2);
const squareArr = Array.from(square.toString()).map(Number);
const startElement = squareArr.length - numberArr.length;
let flag = false;
for (let i = 0, j = startElement; i < numberArr.length, j < squareArr.length; i++, j++) {
flag = false;
if (numberArr[i] == squareArr[j]) {
flag = true;
} else {
console.log(`${number} is not automorphic`);
break;
}
}
if (flag) {
console.log(`${number} is automorphic`);
}
}
automorphic(76);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment