Skip to content

Commit 439c405

Browse files
committed
Add tests, remove main in PalindromePrime
1 parent 596c614 commit 439c405

File tree

2 files changed

+72
-24
lines changed

2 files changed

+72
-24
lines changed
Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,49 @@
11
package com.thealgorithms.misc;
22

3-
import java.util.Scanner;
3+
import java.util.ArrayList;
4+
import java.util.List;
45

56
public final class PalindromePrime {
67
private PalindromePrime() {
78
}
89

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();
15-
}
10+
public static boolean prime(int num) {
11+
if (num < 2) return false; // Handle edge case for numbers < 2
12+
if (num == 2) return true; // 2 is prime
13+
if (num % 2 == 0) return false; // Even numbers > 2 are not prime
1614

17-
public static boolean prime(int num) { // checking if number is prime or not
1815
for (int divisor = 3; divisor <= Math.sqrt(num); divisor += 2) {
1916
if (num % divisor == 0) {
20-
return false; // false if not prime
17+
return false;
2118
}
2219
}
23-
return true; // True if prime
20+
return true;
2421
}
2522

26-
public static int reverse(int n) { // Returns the reverse of the number
23+
public static int reverse(int n) {
2724
int reverse = 0;
2825
while (n != 0) {
29-
reverse *= 10;
30-
reverse += n % 10;
26+
reverse = reverse * 10 + (n % 10);
3127
n /= 10;
3228
}
3329
return reverse;
3430
}
3531

36-
public static void functioning(int y) {
37-
if (y == 0) {
38-
return;
39-
}
40-
System.out.print(2 + "\n"); // print the first Palindromic Prime
32+
public static List<Integer> generatePalindromePrimes(int n) {
33+
List<Integer> palindromicPrimes = new ArrayList<>();
34+
if (n <= 0) return palindromicPrimes; // Handle case for 0 or negative input
35+
36+
palindromicPrimes.add(2); // 2 is the first palindromic prime
4137
int count = 1;
4238
int num = 3;
43-
while (count < y) {
44-
if (num == reverse(num) && prime(num)) { // number is prime and it's reverse is same
45-
count++; // counts check when to terminate while loop
46-
System.out.print(num + "\n"); // print the Palindromic Prime
39+
40+
while (count < n) {
41+
if (num == reverse(num) && prime(num)) {
42+
palindromicPrimes.add(num);
43+
count++;
4744
}
48-
num += 2; // inrease iterator value by two
45+
num += 2; // Skip even numbers
4946
}
47+
return palindromicPrimes;
5048
}
5149
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
import org.junit.jupiter.api.Test;
5+
import java.util.List;
6+
7+
public class PalindromePrimeTest {
8+
9+
@Test
10+
public void testPrimeWithPrimeNumbers() {
11+
assertTrue(PalindromePrime.prime(2), "2 should be prime");
12+
assertTrue(PalindromePrime.prime(3), "3 should be prime");
13+
assertTrue(PalindromePrime.prime(5), "5 should be prime");
14+
assertTrue(PalindromePrime.prime(11), "11 should be prime");
15+
}
16+
17+
@Test
18+
public void testPrimeWithNonPrimeNumbers() {
19+
assertFalse(PalindromePrime.prime(1), "1 is not prime");
20+
assertFalse(PalindromePrime.prime(4), "4 is not prime");
21+
assertFalse(PalindromePrime.prime(9), "9 is not prime");
22+
assertFalse(PalindromePrime.prime(15), "15 is not prime");
23+
}
24+
25+
@Test
26+
public void testReverse() {
27+
assertEquals(123, PalindromePrime.reverse(321), "Reverse of 321 should be 123");
28+
assertEquals(7, PalindromePrime.reverse(7), "Reverse of 7 should be 7");
29+
assertEquals(1221, PalindromePrime.reverse(1221), "Reverse of 1221 should be 1221");
30+
}
31+
32+
@Test
33+
public void testGeneratePalindromePrimes() {
34+
List<Integer> result = PalindromePrime.generatePalindromePrimes(5);
35+
List<Integer> expected = List.of(2, 3, 5, 7, 11);
36+
assertEquals(expected, result, "The first 5 palindromic primes should be [2, 3, 5, 7, 11]");
37+
}
38+
39+
@Test
40+
public void testGeneratePalindromePrimesWithZero() {
41+
List<Integer> result = PalindromePrime.generatePalindromePrimes(0);
42+
assertTrue(result.isEmpty(), "Generating 0 palindromic primes should return an empty list");
43+
}
44+
45+
@Test
46+
public void testGeneratePalindromePrimesWithNegativeInput() {
47+
List<Integer> result = PalindromePrime.generatePalindromePrimes(-5);
48+
assertTrue(result.isEmpty(), "Generating a negative number of palindromic primes should return an empty list");
49+
}
50+
}

0 commit comments

Comments
 (0)