Prime Adam Number In Java
A
positive integer without leading zeros is prime (a number which has only two
factors) and its reverse integer is also prime.
The square of a prime number and the square of its reverse is the same to each other is known as
Prime Adam Integer.
Example:
If number
= 13 and reverse of number = 31, then,
(13)2 <13*13>= 169 Square of the number
(31)2 <31*31>= 961 which is reverse of 169. Which is
the same as 13^2 = 169 so 13 is a prime adam number.
import java.util.*;
public class PrimeAdamInteger
{
/*
*If n=13 and reverse of "n" =31, then,
*(13)^2 = 169
*(31)^2 =961 which is the reverse of 169
*/
int i, rev, num, digit; // rev=reverse,i is for loop
boolean prime;
public PrimeAdamInteger()
{
rev = 0;
prime = true;
}
int
reverse(int n)
{
num = n;
rev = 0;
while(num!=0)
{
digit = num%10;
rev = rev*10 + digit;
num = num/10;
}
return rev;
}
boolean primeNumber(int n)
{
num = n;
for(i=2; i<num; i++)
{
if(num%i == 0)
{
prime = false;
break;
}
}
return prime;
}
public static void main(String args[])
{
int number, rev, sq1, sq2, revsq;
boolean isPrime1, isPrime2;
PrimeAdamInteger ob = new PrimeAdamInteger();
Scanner sc = new Scanner(System.in);
System.out.println("Enter number to Check prime Adam Integer
:");
number = sc.nextInt();
// Call the reverse function
rev = ob.reverse(number);
isPrime1 = ob.primeNumber(number);
isPrime2 = ob.primeNumber(rev);
sq1 = number * number;
sq2 = rev * rev;
revsq = ob.reverse(sq2);
System.out.println(sq1);
System.out.println(sq2);
System.out.println(revsq);
if((isPrime1==true && isPrime2==true) && sq1 == revsq)
{
System.out.println("Entered number is Prime Adam Integer");
}
else
{
System.out.println("Entered number is Not Prime Adam
Integer");
}
}
}
- In the above program, we have taken a number say number=13
- That 13 passes to the reverse(number) method and it return 31
- Both 13 and 31 passes to primeNumber(number) and primeNumber(rev)
- Next square the number 13*13 and the rev 31*31 of the number.
- Next reverse the 31*31 and the result is 169.
- Now both number is the same then that number is Prime Adam Number