Nick Huemmer

13 April 2022

JavaScript Snippet - Test for Automorphic Numbers!

Here’s a fun JavaScript challenge that I was working on a few months back - test if a number is automorphic or not.

An automorphic number is a number when squared the last digits are the same as the original number.

I’m not sure if this is the best way to do it, but it works.

// Test to see if a number is automorphic

const firstNumber = 1376;
const secondNumber = 625

// square the number to check the last digit
const test = (n) => n**2

// function to test if the number is automorphic
const isAutomorphic = (n) => 
n === +String(n**2).split('').slice(-String(n).length).join('');

// Try 1376 to see if it's automorphic  
test(firstNumber) ///1893376
isAutomorphic(firstNumber) // false

// Now try 625!
test(secondNumber) //390625
isAutomorphic(secondNumber) // true