Automorphic Number
Last Updated :
14 Sep, 2023
Given a number N, the task is to check whether the number is an Automorphic number or not. A number is called an Automorphic number if and only if its square ends in the same digits as the number itself.
Examples :
Input : N = 76
Output : Automorphic
Explanation: As 76*76 = 5776
Input : N = 25
Output : Automorphic
As 25*25 = 625
Input : N = 7
Output : Not Automorphic
As 7*7 = 49
Approach:
- Store the square of given number.
- Loop until N becomes 0 as we have to match all digits with its square.
- Check if (n%10 == sq%10) i.e. last digit of number = last digit of square or not
- if not equal, return false.
- Otherwise, continue i.e. reduce the number and square i.e. n = n/10 and sq = sq/10;
- Return true if all digits matched.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
bool isAutomorphic( int N)
{
if (N < 0) N = -N;
int sq = N * N;
while (N > 0) {
if (N % 10 != sq % 10)
return false ;
N /= 10;
sq /= 10;
}
return true ;
}
int main()
{
int N = 5;
isAutomorphic(N) ? cout << "Automorphic"
: cout << "Not Automorphic" ;
return 0;
}
|
Java
import java.io.*;
class Test {
static boolean isAutomorphic( int N)
{
if (N < 0 ) N = -N;
int sq = N * N;
while (N > 0 ) {
if (N % 10 != sq % 10 )
return false ;
N /= 10 ;
sq /= 10 ;
}
return true ;
}
public static void main(String[] args)
{
int N = 5 ;
System.out.println(isAutomorphic(N) ? "Automorphic" : "Not Automorphic" );
}
}
|
Python3
def isAutomorphic(N):
if N < 0 :
N = - N
sq = N * N
while (N > 0 ) :
if (N % 10 ! = sq % 10 ) :
return False
N / / = 10
sq / / = 10
return True
N = 5
if isAutomorphic(N) :
print ( "Automorphic" )
else :
print ( "Not Automorphic" )
|
C#
using System;
class GFG {
static bool isAutomorphic( int N)
{
if (N < 0) N = -N;
int sq = N * N;
while (N > 0) {
if (N % 10 != sq % 10)
return false ;
N /= 10;
sq /= 10;
}
return true ;
}
public static void Main()
{
int N = 5;
Console.Write(isAutomorphic(N) ? "Automorphic" : "Not Automorphic" );
}
}
|
PHP
<?php
function isAutomorphic( $N )
{
if ( $N < 0) $N = - $N ;
$sq = $N * $N ;
while ( $N > 0)
{
if ( $N % 10 != $sq % 10)
return -1;
$N /= 10;
$sq /= 10;
}
return 1;
}
$N = 5;
$geeks = isAutomorphic( $N ) ?
"Automorphic" :
"Not Automorphic" ;
echo $geeks ;
?>
|
Javascript
<script>
function isAutomorphic(N)
{
if (N < 0) N = -N;
let sq = N * N;
while (N > 0)
{
if (N % 10 != sq % 10)
return -1;
N /= 10;
sq /= 10;
}
return 1;
}
let N = 5;
let geeks = isAutomorphic(N) ?
"Automorphic" :
"Not Automorphic" ;
document.write(geeks);
</script>
|
Time Complexity: O(log10N)
Auxiliary Space: O(1)
Another Approach to Solve the Problem
- Do first check if number is negative then make it positive.
- Store the square of number.
- Find the count of the digit of the number sothat you can find the count of digit of last number of the square of the number equal to the number i.e it doesn’t mean if the count of digit of last number of square is equal to the number will be equal to each other.
- And after counting the digit of the number perform : – squareNum%power(10, count)
- Finally check the last number of square of number is equal to number or not.
Let’s see the implementation as explained for above approach : –
C++
#include <iostream>
#include <math.h>
using namespace std;
bool checkAuto( int a){
if (a < 0) a = -a;
int squareNum = a*a;
int temp = a;
int count = 0;
int lastNum = 0;
while (temp > 0){
count++;
temp = temp/10;
}
int lastDigit = (squareNum)%( int ( pow (10, count)));
if (lastDigit == a) return true ;
else return false ;
}
int main() {
int num = -4;
if (checkAuto(num)) cout << "Automorphic" ;
else cout << "Not Automorphic" ;
cout << endl;
return 0;
}
|
Python3
def checkAuto(a):
if a < 0 : a = - a
squareNum = a * a
temp = a
count = 0
while temp ! = 0 :
count + = 1
temp = int (temp / 10 )
lastDigit = squareNum % pow ( 10 , count)
if lastDigit = = a:
return "Automorphic"
else :
return "Not Automorphic"
num = - 4
print (checkAuto(num))
|
Java
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = - 4 ;
if (a < 0 ) a = -a;
int squareNum = a*a;
int temp = a;
int count = 0 ;
while (temp > 0 ){
count++;
temp = temp/ 10 ;
}
int lastDigit = squareNum%( int )Math.pow( 10 , count);
if (lastDigit == a) System.out.print( "Automorphic" );
else System.out.print( "Not Automorphic" );
}
}
|
Javascript
function checkAuto(a){
if (a < 0) a = -a;
let squareNum = a*a;
let temp = a;
let count = 0;
while (temp > 0){
count++;
temp = Math.floor(temp/10);
}
let lastDigit = (squareNum)%(Math.pow(10, count));
if (lastDigit == a) return 1;
else return 0;
}
let num = -4;
if (checkAuto(num)) console.log( "Automorphic" );
else console.log( "Not Automorphic" );
|
C#
using System;
class Solution {
static void Main( string [] args)
{
int a = -4;
if (a < 0)
a = -a;
int squareNum = a * a;
int temp = a;
int count = 0;
while (temp > 0) {
count++;
temp = temp / 10;
}
int lastDigit
= squareNum % ( int )Math.Pow(10, count);
if (lastDigit == a)
Console.Write( "Automorphic" );
else
Console.Write( "Not Automorphic" );
}
}
|
Time Complexity: – O(log10N), where N is the given number.
Auxiliary Space:- O(1)
Share your thoughts in the comments
Please Login to comment...