AΒ Prime-Adam NumberΒ is defined as a positive number that fulfills two conditions simultaneously: it is a prime number and also an Adam number.
- AΒ prime numberΒ is a natural number greater than 1 that has exactly two distinct positive divisors: 1 and itself.
Examples include: 2, 3, 5, 7, etc. - AnΒ Adam number is a number whose square and the square of its digit-reversal are reverses of each other.
For example, take the number 13; its reverse is 31. The square of 13 is 169, and the square of 31 is 961. Since 961 is the reverse of 169, 13 is an Adam number.
Write a program that accepts two positive number, and (with the condition), from the user. The program should then find and print all Prime-Adam numbers within the range to inclusive. Additionally, display the count of such numbers in the specified format.
Test your code with the given examples and with different inputs:
Example 1
Input:
m equals 5
n equals 100
Output:
The Prime-Adam numbers are:
11, 13, 31
Frequency of Prime-Adam numbers is: 3
Example 2
Input:
m equals 100
n equals 200
Output:
The Prime-Adam numbers are:
101, 103, 113
Frequency of Prime-Adam numbers is: 3
Example 3
Input:
m equals 50
n equals 70
Output:
The Prime-Adam numbers are:
None
Frequency of Prime-Adam numbers is: 0
Example 4
Input:
m equals 700
n equals 450
Output:
Invalid input
Program:
import java.util.Scanner;
public class PrimeAdam {
// Method to check if a number is prime
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
// Method to reverse digits of a number
public static int reverseNumber(int num) {
int reversed = 0;
while (num > 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num = num / 10;
}
return reversed;
}
// Method to check if a number is an Adam number
public static boolean isAdamNumber(int num) {
int square = num * num;
int reverseNum = reverseNumber(num);
int reverseSquare = reverseNum * reverseNum;
return square == reverseNumber(reverseSquare);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter starting number (m): ");
int m = scanner.nextInt();
System.out.print("Enter ending number (n): ");
int n = scanner.nextInt();
if (m >= n) {
System.out.println("INVALID INPUT");
scanner.close();
return;
}
System.out.println("THE PRIME-ADAM NUMBERS ARE:");
int count = 0;
for (int i = m; i <= n; i++) {
if (isPrime(i) && isAdamNumber(i)) {
System.out.print(i + " ");
count++;
}
}
if (count == 0) {
System.out.println("NIL");
} else {
System.out.println();
}
System.out.println("FREQUENCY OF PRIME-ADAM NUMBER IS: " + count);
scanner.close();
}
}
How Does This Program Work?
Β This Java program works by checking each number in the user-provided range to see if it meets two conditions: being a prime number and being an Adam number. Here’s a step-by-step explanation of how it works:
Input and Validation
- The program first asks the user to enter two positive numbers, calledΒ mΒ andΒ n.
- It checks ifΒ mΒ is less thanΒ n. If not, it prints “INVALID INPUT” and stops.
Checking Prime Numbers
- For each number betweenΒ mΒ andΒ nΒ (inclusive), the program calls theΒ isPrimeΒ method.
- This method checks if a number is only divisible by 1 and itself by trying to divide it by all numbers from 2 up to the number.
- If any division results in zero remainder, the number is not prime.
Checking Adam Numbers
- If a number is prime, the program then checks if it’s an Adam number using theΒ isAdamNumberΒ method.
- This method first calculates the square of the number and stores it.
- It reverses the digits of the number and then squares this reversed number.
- It reverses the square of the reversed number.
- If this final reversed value is the same as the first square, the number is an Adam number.
Output
- If a number is both prime and Adam, it is printed out.
- The program keeps a count of how many such numbers it finds.
- After checking all numbers, if none are found, it prints “NIL”. Otherwise, it prints the total count of Prime-Adam numbers found.
Summary
- The program uses basic loops and conditions to filter numbers based on the two criteria.
- It uses helper methods to keep the code clean and modular for prime checking, reversing numbers, and checking the Adam number condition.
This approach ensures the program efficiently finds all Prime-Adam numbers in the given range and shows useful feedback for invalid input or no results. Let me know if you want a detailed walk-through of any specific method!