Open In App

JavaScript Program to Check Whether a Number is an Automorphic Number

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Numbers, whose square ends with the same digits as the number itself are referred to as automorphic numbers, sometimes known as circular or circular-permuted numbers.

Examples:

Input: number = 5
Output: Yes
Explanation: 5 is a automorphic number, because the square of
5 is 25 which ends with 5 the digit itself.
Input: 9
Output: No
Explanation: 9 is not an automorphic number as its square ends with 1.

Using String Manipulation

Convert the number into a string, extract its last digits, square the number, and verifies if the last digits of the squared number match the original number. This approach determines automorphism by comparing the last digits of the number and its square.

Example: The below code uses the string manipulation approach to check for the automorphic number.

Javascript




function isAutomorphicString(num) {
    let strNum = num.toString();
    let square = (num * num).toString();
    return square.endsWith(strNum);
}
isAutomorphicString(25)? console.log("Given Number 25 " +
    "is a automorphic number"):console.log("Given Number 25 " +
    "is not a automorphic number");
    isAutomorphicString(33)? console.log("Given Number 33 " +
    "is a automorphic number"):console.log("Given Number 33 " +
    "is not a automorphic number");
isAutomorphicString(5)? console.log("Given Number 5 " +
    "is a automorphic number"):console.log("Given Number 5 " +
    "is not a automorphic number");
    isAutomorphicString(9)? console.log("Given Number 9 " +
    "is a automorphic number"):console.log("Given Number 9 " +
    "is not a automorphic number");


Output

Given Number 25 is a automorphic number
Given Number 33 is not a automorphic number
Given Number 5 is a automorphic number
Given Number 9 is not a automorphic number

Time Complexity: – O(log10N), where N is the given number.
Auxiliary Space:- O(1)

Using Iterative Checking

Iterate through each digit of the square of the number, compare them iteratively with the corresponding digits of the original number to determine automorphism. This method ensures accuracy by directly inspecting the digits without converting to strings, providing an efficient approach for identifying automorphic numbers.

Example: The below code will find the automorphic number by checking the input number using iterative approach.

Javascript




function isAutomorphicIterative(num) {
    let squared = num * num;
    while (num > 0) {
        if (num % 10 != squared % 10) {
            return false;
        }
        num = Math.floor(num / 10);
        squared = Math.floor(squared / 10);
    }
    return true;
}
 
isAutomorphicIterative(25)? console.log("Given Number 25 " +
    "is a automorphic number"):console.log("Given Number 25 " +
    "is not a automorphic number");
isAutomorphicIterative(33)? console.log("Given Number 33 " +
    "is a automorphic number"):console.log("Given Number 33 " +
    "is not a automorphic number");
isAutomorphicIterative(5)? console.log("Given Number 5 " +
    "is a automorphic number"):console.log("Given Number 5 " +
    "is not a automorphic number");
isAutomorphicIterative(9)? console.log("Given Number 9 " +
    "is a automorphic number"):console.log("Given Number 9 " +
    "is not a automorphic number");


Output

Given Number 25 is a automorphic number
Given Number 33 is not a automorphic number
Given Number 5 is a automorphic number
Given Number 9 is not a automorphic number

Time Complexity: – O(log10N), where N is the given number.
Auxiliary Space:- O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads