JavaScript Program to Check Whether a Number is an Automorphic Number
Last Updated :
27 Feb, 2024
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");
OutputGiven 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");
OutputGiven 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)
Similar Reads
JavaScript Program to Check Whether a Number is Harshad Number A Harshad number (also called Niven number) is a number that is divisible by the sum of its digits. In other words, if you take a number, sum up its digits, and if the original number is divisible by that sum, then it's a Harshad number. For example, 18 is a Harshad number because the sum of its dig
2 min read
JavaScript Program to Check Whether a Number is Perfect Square The number that results from squaring another integer is called a perfect square. A perfect square is a number that can be expressed as the product of an integer with itself. There are several ways available in JavaScript to check whether a number is a perfect square or not which are as follows: Tab
4 min read
JavaScript Program to Check if a Number is Float or Integer In this article, we will see how to check whether a number is a float or an integer in JavaScript. A float is a number with a decimal point, while an integer is a whole number or a natural number without having a decimal point. Table of ContentUsing the Number.isInteger() MethodUsing the Modulus Ope
2 min read
JavaScript Program to Check Prime Number By Creating a Function A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In this article, we will explore how to check if a given number is a prime number in JavaScript by creating a function. Table of Content Check Prime Number using for LoopCheck Prime Number using
2 min read
Java Program to Check if a Given Number is Perfect Number A number is said to be a perfect number if the sum of its proper divisors ( i.e. all positive divisors excluding the number itself )is equal to that number itself. Aliquot sum is the sum of divisors of a number, excluding the number itself. Hence, a number is a perfect number only if it is equal to
5 min read