Skip to content

Commit 9c2a9a1

Browse files
Improvement: Removed main function from PalindromePrime class. Returned the output instead of just printing. Added JUnit tests
1 parent 6ef1f7c commit 9c2a9a1

File tree

3 files changed

+180
-33
lines changed

3 files changed

+180
-33
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,7 @@
10211021
* [MedianOfMatrixtest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java)
10221022
* [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
10231023
* [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
1024+
* [PalindromePrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java)
10241025
* [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
10251026
* [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
10261027
* [ThreeSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ThreeSumProblemTest.java)
Lines changed: 96 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,114 @@
11
package com.thealgorithms.misc;
22

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+
*/
520
public final class PalindromePrime {
6-
private PalindromePrime() {
7-
}
821

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
1524
}
1625

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
2144
}
45+
num += 2; // increase iterator value by two
2246
}
23-
return true; // True if prime
47+
return result;
2448
}
2549

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
3962
}
40-
System.out.print(2 + "\n"); // print the first Palindromic Prime
4163
int count = 1;
4264
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
4567
count++; // counts check when to terminate while loop
46-
System.out.print(num + "\n"); // print the Palindromic Prime
4768
}
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;
49111
}
112+
return reversed;
50113
}
51114
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.thealgorithms.misc;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import org.junit.jupiter.api.Test;
8+
9+
/**
10+
* Unit tests for the PalindromePrime class.
11+
*/
12+
public class PalindromePrimeTest {
13+
14+
/**
15+
* Test the findPalindromicPrimes method with a positive number.
16+
*/
17+
@Test
18+
public void testFindPalindromicPrimesPositive() {
19+
int[] result = PalindromePrime.findPalindromicPrimes(5);
20+
assertArrayEquals(new int[] {2, 3, 5, 7, 11}, result);
21+
}
22+
23+
/**
24+
* Test the findPalindromicPrimes method with zero.
25+
*/
26+
@Test
27+
public void testFindPalindromicPrimesZero() {
28+
int[] result = PalindromePrime.findPalindromicPrimes(0);
29+
assertArrayEquals(new int[] {}, result);
30+
}
31+
32+
/**
33+
* Test the findNthPalindromicPrime method with a positive number.
34+
*/
35+
@Test
36+
public void testFindNthPalindromicPrimePositive() {
37+
int result = PalindromePrime.findNthPalindromicPrime(5);
38+
assertEquals(11, result);
39+
}
40+
41+
/**
42+
* Test the findNthPalindromicPrime method with one.
43+
*/
44+
@Test
45+
public void testFindNthPalindromicPrimeOne() {
46+
int result = PalindromePrime.findNthPalindromicPrime(1);
47+
assertEquals(2, result);
48+
}
49+
50+
/**
51+
* Test the findNthPalindromicPrime method with zero.
52+
*/
53+
@Test
54+
public void testFindNthPalindromicPrimeZero() {
55+
assertThrows(IllegalArgumentException.class, () -> PalindromePrime.findNthPalindromicPrime(0));
56+
}
57+
58+
/**
59+
* Test the findNthPalindromicPrime method with a negative number.
60+
*/
61+
@Test
62+
public void testFindNthPalindromicPrimeNegative() {
63+
assertThrows(IllegalArgumentException.class, () -> PalindromePrime.findNthPalindromicPrime(-1));
64+
}
65+
66+
/**
67+
* Test the findPalindromicPrimes method with a large number.
68+
*/
69+
@Test
70+
public void testFindPalindromicPrimesLarge() {
71+
int[] result = PalindromePrime.findPalindromicPrimes(10);
72+
assertArrayEquals(new int[] {2, 3, 5, 7, 11, 101, 131, 151, 181, 191}, result);
73+
}
74+
75+
/**
76+
* Test the findNthPalindromicPrime method with a large number.
77+
*/
78+
@Test
79+
public void testFindNthPalindromicPrimeLarge() {
80+
int result = PalindromePrime.findNthPalindromicPrime(10);
81+
assertEquals(191, result);
82+
}
83+
}

0 commit comments

Comments
 (0)