How to use Iterative Checking In Javascript
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" ); |
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)
JavaScript Program to Check Whether a Number is an Automorphic Number
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.
Table of Content
- Using String Manipulation
- Using Iterative Checking