| 
1 | 1 | import java.util.Scanner;  | 
 | 2 | + | 
2 | 3 | public class PalindromePrime {  | 
3 | 4 | 
 
  | 
4 | 5 |     public static void main(String[] args) { // Main funtion  | 
5 | 6 |         Scanner in = new Scanner(System.in);  | 
6 | 7 |         System.out.println("Enter the quantity of First Palindromic Primes you want");  | 
7 |  | -        int n = in.nextInt(); // Input of how mant first pallindromic prime we want  | 
8 |  | -        funtioning(n); // calling funtion - functioning    | 
 | 8 | +        int n = in.nextInt(); // Input of how many first pallindromic prime we want  | 
 | 9 | +        functioning(n); // calling function - functioning    | 
9 | 10 |     }  | 
10 | 11 | 
 
  | 
11 | 12 |     public static boolean prime(int num) { // checking if number is prime or not  | 
12 |  | -        for (int divisor = 2; divisor <= num / 2; divisor++) {  | 
 | 13 | +        for (int divisor = 3; divisor <= Math.sqrt(num); divisor += 2) {  | 
13 | 14 |             if (num % divisor == 0) {  | 
14 | 15 |                 return false; //  false if not prime  | 
15 | 16 |             }  | 
16 | 17 |         }  | 
17 | 18 |         return true; // True if prime  | 
18 | 19 |     }  | 
19 | 20 | 
 
  | 
20 |  | -    public static int reverse(int n){ //  Returns  the reverse of the number  | 
 | 21 | +    public static int reverse(int n) { //  Returns  the reverse of the number  | 
21 | 22 |         int reverse = 0;  | 
22 |  | -        while(n!=0){  | 
23 |  | -            reverse = reverse * 10;  | 
24 |  | -            reverse = reverse + n%10;  | 
25 |  | -            n = n/10;  | 
 | 23 | +        while(n != 0) {  | 
 | 24 | +            reverse *= 10;  | 
 | 25 | +            reverse += n%10;  | 
 | 26 | +            n /= 10;  | 
26 | 27 |         }  | 
27 | 28 |         return reverse;  | 
28 | 29 |     }  | 
29 | 30 | 
 
  | 
30 |  | -    public static void funtioning(int y){  | 
31 |  | -            int count =0;  | 
32 |  | -            int num = 2;  | 
33 |  | -            while(count < y){  | 
34 |  | -                if(prime(num) && num == reverse(num)){ // number is prime and it's reverse is same  | 
35 |  | -                    count++; // counts check when to terminate while loop  | 
36 |  | -                    System.out.print(num + "\n"); // Print the Palindromic Prime  | 
37 |  | -                }  | 
38 |  | -                num++; // inrease iterator value by one  | 
 | 31 | +    public static void functioning(int y) {  | 
 | 32 | +        if (y == 0) return;  | 
 | 33 | +        System.out.print(2 + "\n"); // print the first Palindromic Prime  | 
 | 34 | +        int count = 1;  | 
 | 35 | +        int num = 3;  | 
 | 36 | +        while(count < y) {  | 
 | 37 | +            if(num == reverse(num) && prime(num)) { // number is prime and it's reverse is same  | 
 | 38 | +                count++; // counts check when to terminate while loop  | 
 | 39 | +                System.out.print(num + "\n"); // print the Palindromic Prime  | 
39 | 40 |             }  | 
 | 41 | +            num += 2; // inrease iterator value by two  | 
 | 42 | +        }  | 
40 | 43 |     }  | 
41 |  | -};  | 
 | 44 | +}  | 
0 commit comments