FOR FREE MATERIALS

Automorphic Number

Back to Programming

Description

A number is said to be an automorphic number if the number is present in the last digit(s) of the square of the original number.

 

If the number is 25, the square of 25 is 625 in which the last two digits contains the original number i.e. 25. Therefore, this number is an automorphic number.

If the number is 20, the square of 20 is 400 which does not contain 20 in its last digit(s). Therefore, 20 is not an automorphic number.

 

For this program, a number is taken as input, and then the square of the number is calculated. After that it is checked that the square contains the original number in its last digit(s) or not. If it contains then it is an automorphic number otherwise not.

Algorithm

INPUT: A number
OUTPUT: Whether the number is an automorphic number or not
PROCESS:
Step 1: [Taking the input]
	Read n [The number to be checked]
Step 2: [Checking for automorphic number]
	[Calculating the square of the number]
	Set s<-n2
	[Checking for automorphic number]
	While n>0 repeat
		[Comparing the digits of the number with its square]
		If (s mod 10)≠(n mod 10) then
			Break
		[End of ‘if’]
		Set s<-s/10
		Set n<-n/10
	[End of ‘while’ loop]
	[Printing the output according to the proper condition]
	If n=0 then
		Print "The given number is an automorphic number"
	Else
		Print "The given number is not an automorphic number"
	[End of ‘if-else’]	
Step 3: Stop.

Code

Time Complexity:

//checking for the automorphic number

                while(n>0) -----------------------------------------O(n) 

[‘n’ is the number of digits of the original number]

                {                //comparing the digits of the

                                //number with its square

                                if((s%10)!=(n%10))

                                                break;

                                s=s/10;

                                n=n/10;    }

                //printing the output according to the proper condition

                if(n==0)------------------------------------------------------O(1)

                                printf("The given number is an automorphic number");

                else-----------------------------------------------------------O(1)

                                printf("The given number is not an automorphic number");         

 

The time complexity of this program is O(n) where ‘n’ is the number of digits of the given number.

 

Space Complexity:

The space complexity of this program is O(1) as it requires a constant number of spaces to execute for any given input.