Palindrome Prime Algorithm
The Palindrome Prime Algorithm is a computational technique used to identify prime numbers that are palindromes. Palindrome primes are prime numbers that remain the same when their digits are reversed, such as 11, 101, and 131. The algorithm functions by generating prime numbers and then checking if they are palindromes. This process continues until the desired range or number of palindrome primes have been found.
In this algorithm, a prime number generator typically uses a method, such as the Sieve of Eratosthenes or trial division, to generate prime numbers within a specified range. Once a prime number is identified, the algorithm checks if the number is a palindrome by converting it to a string or an array of digits, reversing the order of digits, and then comparing the reversed number with the original number. If the reversed and original numbers match, the prime number is considered a palindrome prime. The algorithm continues this process until it reaches the upper bound of the specified range or a desired number of palindrome primes are found.
package Misc;
import java.util.Scanner;
public class PalindromePrime {
public static void main(String[] args) { // Main funtion
Scanner in = new Scanner(System.in);
System.out.println("Enter the quantity of First Palindromic Primes you want");
int n = in.nextInt(); // Input of how many first pallindromic prime we want
functioning(n); // calling function - functioning
in.close();
}
public static boolean prime(int num) { // checking if number is prime or not
for (int divisor = 3; divisor <= Math.sqrt(num); divisor += 2) {
if (num % divisor == 0) {
return false; // false if not prime
}
}
return true; // True if prime
}
public static int reverse(int n) { // Returns the reverse of the number
int reverse = 0;
while (n != 0) {
reverse *= 10;
reverse += n % 10;
n /= 10;
}
return reverse;
}
public static void functioning(int y) {
if (y == 0) return;
System.out.print(2 + "\n"); // print the first Palindromic Prime
int count = 1;
int num = 3;
while (count < y) {
if (num == reverse(num) && prime(num)) { // number is prime and it's reverse is same
count++; // counts check when to terminate while loop
System.out.print(num + "\n"); // print the Palindromic Prime
}
num += 2; // inrease iterator value by two
}
}
}