|
1 | 1 | package com.thealgorithms.misc;
|
2 | 2 |
|
3 |
| -import java.util.Scanner; |
4 |
| - |
| 3 | +/** |
| 4 | + * Utility class for finding palindromic prime numbers. |
| 5 | + * |
| 6 | + * Palindromic primes are prime numbers that remain the same when their digits are reversed. |
| 7 | + * |
| 8 | + * For more information, refer to the |
| 9 | + * <a href="https://en.wikipedia.org/wiki/Palindromic_prime">Palindromic prime</a> Wikipedia page. |
| 10 | + * |
| 11 | + * <b>Example usage:</b> |
| 12 | + * <pre> |
| 13 | + * int[] palindromicPrimes = PalindromePrime.findPalindromicPrimes(5); |
| 14 | + * System.out.println(Arrays.toString(palindromicPrimes)); // Output: [2, 3, 5, 7, 11] |
| 15 | + * |
| 16 | + * int nthPalindromicPrime = PalindromePrime.findNthPalindromicPrime(5); |
| 17 | + * System.out.println(nthPalindromicPrime); // Output: 11 |
| 18 | + * </pre> |
| 19 | + */ |
5 | 20 | public final class PalindromePrime {
|
6 |
| - private PalindromePrime() { |
7 |
| - } |
8 | 21 |
|
9 |
| - public static void main(String[] args) { // Main function |
10 |
| - Scanner in = new Scanner(System.in); |
11 |
| - System.out.println("Enter the quantity of First Palindromic Primes you want"); |
12 |
| - int n = in.nextInt(); // Input of how many first palindromic prime we want |
13 |
| - functioning(n); // calling function - functioning |
14 |
| - in.close(); |
| 22 | + private PalindromePrime() { |
| 23 | + // Private constructor to prevent instantiation |
15 | 24 | }
|
16 | 25 |
|
17 |
| - public static boolean prime(int num) { // checking if number is prime or not |
18 |
| - for (int divisor = 3; divisor <= Math.sqrt(num); divisor += 2) { |
19 |
| - if (num % divisor == 0) { |
20 |
| - return false; // false if not prime |
| 26 | + /** |
| 27 | + * Finds the first n palindromic prime numbers. |
| 28 | + * |
| 29 | + * @param n The number of palindromic primes to find |
| 30 | + * @return An array containing the first n palindromic prime numbers |
| 31 | + */ |
| 32 | + public static int[] findPalindromicPrimes(int n) { |
| 33 | + if (n == 0) { |
| 34 | + return new int[0]; |
| 35 | + } |
| 36 | + int[] result = new int[n]; |
| 37 | + result[0] = 2; // The first palindromic prime |
| 38 | + int count = 1; |
| 39 | + int num = 3; |
| 40 | + while (count < n) { |
| 41 | + if (num == reverse(num) && isPrime(num)) { // number is prime and its reverse is the same |
| 42 | + result[count] = num; |
| 43 | + count++; // counts check when to terminate while loop |
21 | 44 | }
|
| 45 | + num += 2; // increase iterator value by two |
22 | 46 | }
|
23 |
| - return true; // True if prime |
| 47 | + return result; |
24 | 48 | }
|
25 | 49 |
|
26 |
| - public static int reverse(int n) { // Returns the reverse of the number |
27 |
| - int reverse = 0; |
28 |
| - while (n != 0) { |
29 |
| - reverse *= 10; |
30 |
| - reverse += n % 10; |
31 |
| - n /= 10; |
32 |
| - } |
33 |
| - return reverse; |
34 |
| - } |
35 |
| - |
36 |
| - public static void functioning(int y) { |
37 |
| - if (y == 0) { |
38 |
| - return; |
| 50 | + /** |
| 51 | + * Finds the nth palindromic prime number. |
| 52 | + * |
| 53 | + * @param n The position of the palindromic prime to find |
| 54 | + * @return The nth palindromic prime number |
| 55 | + */ |
| 56 | + public static int findNthPalindromicPrime(int n) { |
| 57 | + if (n <= 0) { |
| 58 | + throw new IllegalArgumentException("n must be greater than 0"); |
| 59 | + } |
| 60 | + if (n == 1) { |
| 61 | + return 2; // The first palindromic prime |
39 | 62 | }
|
40 |
| - System.out.print(2 + "\n"); // print the first Palindromic Prime |
41 | 63 | int count = 1;
|
42 | 64 | int num = 3;
|
43 |
| - while (count < y) { |
44 |
| - if (num == reverse(num) && prime(num)) { // number is prime and it's reverse is same |
| 65 | + while (count < n) { |
| 66 | + if (num == reverse(num) && isPrime(num)) { // number is prime and its reverse is the same |
45 | 67 | count++; // counts check when to terminate while loop
|
46 |
| - System.out.print(num + "\n"); // print the Palindromic Prime |
47 | 68 | }
|
48 |
| - num += 2; // inrease iterator value by two |
| 69 | + if (count < n) { |
| 70 | + num += 2; // increase iterator value by two |
| 71 | + } |
| 72 | + } |
| 73 | + return num; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Checks if a number is prime. |
| 78 | + * |
| 79 | + * @param num The number to check |
| 80 | + * @return true if the number is prime, false otherwise |
| 81 | + */ |
| 82 | + private static boolean isPrime(int num) { |
| 83 | + if (num < 2) { |
| 84 | + return false; |
| 85 | + } |
| 86 | + if (num == 2) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + if (num % 2 == 0) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + for (int divisor = 3; divisor <= Math.sqrt(num); divisor += 2) { |
| 93 | + if (num % divisor == 0) { |
| 94 | + return false; // false if not prime |
| 95 | + } |
| 96 | + } |
| 97 | + return true; // true if prime |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Reverses the digits of a number. |
| 102 | + * |
| 103 | + * @param num The number to reverse |
| 104 | + * @return The reversed number |
| 105 | + */ |
| 106 | + private static int reverse(int num) { |
| 107 | + int reversed = 0; |
| 108 | + while (num != 0) { |
| 109 | + reversed = reversed * 10 + num % 10; |
| 110 | + num /= 10; |
49 | 111 | }
|
| 112 | + return reversed; |
50 | 113 | }
|
51 | 114 | }
|
0 commit comments