-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
PalindromePrime.java
57 lines (49 loc) · 1.44 KB
/
PalindromePrime.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.thealgorithms.misc;
import java.util.ArrayList;
import java.util.List;
public final class PalindromePrime {
private PalindromePrime() {
}
public static boolean prime(int num) {
if (num < 2) {
return false; // Handle edge case for numbers < 2
}
if (num == 2) {
return true; // 2 is prime
}
if (num % 2 == 0) {
return false; // Even numbers > 2 are not prime
}
for (int divisor = 3; divisor <= Math.sqrt(num); divisor += 2) {
if (num % divisor == 0) {
return false;
}
}
return true;
}
public static int reverse(int n) {
int reverse = 0;
while (n != 0) {
reverse = reverse * 10 + (n % 10);
n /= 10;
}
return reverse;
}
public static List<Integer> generatePalindromePrimes(int n) {
List<Integer> palindromicPrimes = new ArrayList<>();
if (n <= 0) {
return palindromicPrimes; // Handle case for 0 or negative input
}
palindromicPrimes.add(2); // 2 is the first palindromic prime
int count = 1;
int num = 3;
while (count < n) {
if (num == reverse(num) && prime(num)) {
palindromicPrimes.add(num);
count++;
}
num += 2; // Skip even numbers
}
return palindromicPrimes;
}
}