Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

In this short tutorial, we'll discuss automorphic numbers and learn a couple of ways to find them along with Java programs.

2. What Is an Automorphic Number?

An automorphic number is a number whose square has the same digits in the end as the number itself.

For example, 25 is an automorphic number because the square of 25 is 625, which ends with 25. Similarly, 76 is an automorphic number because the square of 76 is 5776, which again ends with 76.

In mathematics, an automorphic number is also referred to as a circular number.

Some more examples of automorphic numbers are 0, 1, 5, 6, 25, 76, 376, 625, 9376, etc.

0 and 1 are called trivial automorphic numbers as they are automorphic numbers in every base.

3. Determine if a Number Is Automorphic

There are many algorithms available to determine whether a number is automorphic or not. Next, we'll see a couple of approaches.

3.1. Loop Over the Digits and Compare

Here's one way to determine if a number is automorphic:

  1. get the number and calculate its square
  2. get the last digit of the square and compare it with the last digit of the number
    • if the last digits are not equal, the number is not an automorphic number
    • if the last digits are equal, go to the next step
  3. remove the last digit of both number and square
  4. repeat steps 2 & 3 until all the digits of the number are compared

The above approach loops through the digits of the input number in a reverse way.

Let's write a Java program to implement this approach. The isAutomorphicUsingLoop() method takes an integer as input and checks if it's automorphic:

public boolean isAutomorphicUsingLoop(int number) {
    int square = number * number;

    while (number > 0) {
        if (number % 10 != square % 10) {
            return false;
        }
        number /= 10;
        square /= 10;
    }
    
    return true;
}

Here, we first calculate the square of number. Then, we iterate over the digits of number and compare its last digit with that of square, one by one.

At any stage, if the last digits are not equal, we return false and get out of the method. Otherwise, we get rid of the equal last digits and repeat the process for the remaining digits of number.

Let's test this out:

assertTrue(AutomorphicNumber.isAutomorphicUsingLoop(76));
assertFalse(AutomorphicNumber.isAutomorphicUsingLoop(9));

3.2. Compare Digits Directly

We can also determine whether a number is automorphic or not in a more straightforward way:

  1. get the number and count the number of digits (n)
  2. calculate the square of the number
  3. get the last digits from the square
    • if the last digits of the square make the original number, the number is automorphic
    • else it's not an automorphic number

In this case, we need not loop through the digits of the number. Instead, we can use the existing libraries of the programming framework.

We can make use of the Math class to perform numeric operations such as counting the digits in the given number and getting those many last digits from its square:

public boolean isAutomorphicUsingMath(int number) {
    int square = number * number;

    int numberOfDigits = (int) Math.floor(Math.log10(number) + 1);
    int lastDigits = (int) (square % (Math.pow(10, numberOfDigits)));

    return number == lastDigits;
}

Similar to the first approach, we start with calculating the square of number. Then, instead of comparing the last digits of number and square one by one, we get the total numberOfDigits in number at once by using Math.floor(). After that, we extract as many digits from square by using Math.pow(). In the end, we compare the input number with the extracted number lastDigits.

If the number and lastDigits are equal, the number is automorphic and we return true, otherwise, we return false.

Let's test this out:

assertTrue(AutomorphicNumber.isAutomorphicUsingMath(76));
assertFalse(AutomorphicNumber.isAutomorphicUsingMath(9));

4. Conclusion

In this article, we explored automorphic numbers. We also looked at a couple of ways to determine if a number is an automorphic number along with the corresponding Java programs.

As always, the code for these examples is available over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!