In this post I would be discussing two methods to test for an automorphic number- a simple and not so good method and a slightly complex and efficient method! In mathematics an automorphic number (sometimes referred to as a circular number) is a number whose square “ends” in theĀ same digits as number itself.

For example:

**5**^{2}= 2**5****6**^{2}= 3**6****76**^{2}= 57**76****890625**^{2}= 793212**890625**

so 5, 6, 76 and 890625 are all automorphic numbers.

The first approach which comes in the mind is usually- find the square, count the number of digits, raise 10 to the power of number of digits, take out the end digits by finding the modulus of square divided by 10 to the power of number of digits. The logic when implemented in Java would be as follows:

class Automorphic{ /* In mathematics an automorphic number (sometimes referred to * as a circular number) is a number whose square "ends" in the * same digits as number itself. * For example, 5^2 = 25, 6^2 = 36, 76^2 = 5776, * and 890625^2 = 793212890625, so 5, 6, 76 and 890625 are all * automorphic numbers. */ public static boolean isAutomorphicNumber( int n ){ int square = n * n, digits = 0; for( int temp = n ; temp > 0 ; temp /= 10 ){ digits++; } int end = square % (int)Math.pow(10, digits); if( n == end ) return true; else return false; } public static void main( String []args ){ for( int i = 1; i <= 1000 ; i++){ if( isAutomorphicNumber( i ) ) System.out.println( i ); } } }

The above program would display all automorphic numbers from 1 to 1000. The output would be as follows:

1 5 6 25 76 376 625

The above output is correct but note that the pow() function is also abstracting a loop. That is the pow() function uses a loop internally for calculating the power.

It is possible to determine if a number is an automorphic number with only one loop and without using any Math class function. There is no need to find the number of digits, all we need is 1 followed some zeros such that the number of zeros is equal to the number of digits. The program to determine whether an integer is an automorphic number or not in Java programming language without using any function of the Math class is as follows:

class Automorphic{ /* In mathematics an automorphic number (sometimes referred to * as a circular number) is a number whose square "ends" in the * same digits as number itself. * For example, 5^2 = 25, 6^2 = 36, 76^2 = 5776, * and 890625^2 = 793212890625, so 5, 6, 76 and 890625 are all * automorphic numbers. */ public static boolean isAutomorphicNumber( int n ){ int square = n * n, factor = 1; for( int temp = n ; temp > 0 ; temp /= 10 ){ factor *= 10; } return ( square % factor ) == n; } public static void main( String []args ){ for( int i = 1; i <= 1000 ; i++){ if( isAutomorphicNumber( i ) ) System.out.println( i ); } } }

The main() function in the above program would display all the automorphic numbers between 1 and 1000. For detail of digit extraction process please refer to my earlier post on integer palindrome.

As usual, the main function has purposely been kept to minimum and its expected that students will write appropriate input/output statements as per the requirement.

### Vinay Singh

#### Latest posts by Vinay Singh (see all)

- Sorting non boundary elements - February 19, 2016
- Circular Prime – Solution of ISC Computer Practical 2016 - February 18, 2016
- Array Pattern - February 15, 2016