Prime Adam Number ISC Computer Science 2020 Practical
A Prime-Adam integer is a positive integer (without leading zeroes) which is a prime as well as an Adam number.
Prime number: A number which has only two factors, i.e. 1 and the number itself.
Example: 2, 3, 5, 7 ... etc.
Adam number: The square of a number and the square of its reverse are reverse to each other.
Example: If n = 13 and reverse of 'n' = 31, then,
(13)2 = 169
(31)2 = 961 which is reverse of 169.
Thus, 13 is an Adam number.
Accept two positive integers m and n, where m is less than n as user input. Display all Prime-Adam integers that are in the range between m and n (both inclusive) and output them along with the frequency, in the format given below:
Test your program with the following data and some random data:
Example 1:
INPUT:
m = 5
n = 100
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
11 13 31
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 2:
INPUT:
m = 100
n = 200
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
101 103 113
FREQUENCY OF PRIME-ADAM INTEGERS IS: 3
Example 3:
INPUT:
m = 50
n = 70
OUTPUT:
THE PRIME-ADAM INTEGERS ARE:
NIL
FREQUENCY OF PRIME-ADAM INTEGERS IS: 0
Example 4:
INPUT:
m = 700
n = 450
OUTPUT:
INVALID INPUT.
class PrimeAdam{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("m = ");
int m = Integer.parseInt(in.nextLine());
System.out.print("n = ");
int n = Integer.parseInt(in.nextLine());
int count = 0;
if(m >= n){
System.out.println("INVALID INPUT.");
return;
}
System.out.println("THE PRIME-ADAM INTEGERS ARE:");
for(int i = m; i <= n; i++){
if(isPrime(i) && isAdam(i)){
System.out.print(i + " ");
count++;
}
}
if(count == 0)
System.out.println("NIL");
System.out.println("\nFREQUENCY OF PRIME-ADAM INTEGERS IS: " + count);
}
public static boolean isPrime(int n){
for(int i = 2; i <= n / 2; i++){
if(n % i == 0)
return false;
}
return true;
}
public static boolean isAdam(int n){
int rev1 = reverse(n);
int sq1 = n * n;
int sq2 = rev1 * rev1;
int rev2 = reverse(sq2);
if(sq1 == rev2)
return true;
return false;
}
public static int reverse(int n){
int rev = 0;
while(n != 0){
rev = rev * 10 + n % 10;
n /= 10;
}
return rev;
}
}
Comments
Post a Comment