Skip to content

Commit 41f7e6a

Browse files
authored
Add tests, improve docs in NewManShanksPrime (#5660)
1 parent 44b0fc9 commit 41f7e6a

File tree

3 files changed

+112
-6
lines changed

3 files changed

+112
-6
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@
262262
* [LongestValidParentheses](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestValidParentheses.java)
263263
* [MatrixChainMultiplication](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplication.java)
264264
* [MatrixChainRecursiveTopDownMemoisation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisation.java)
265+
* [MaximumSumOfNonAdjacentElements](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MaximumSumOfNonAdjacentElements.java)
265266
* [MinimumPathSum](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MinimumPathSum.java)
266267
* [MinimumSumPartition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java)
267268
* [NewManShanksPrime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java)
@@ -815,8 +816,10 @@
815816
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java)
816817
* [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java)
817818
* [MatrixChainMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplicationTest.java)
819+
* [MaximumSumOfNonAdjacentElementsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MaximumSumOfNonAdjacentElementsTest.java)
818820
* [MinimumPathSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumPathSumTest.java)
819821
* [MinimumSumPartitionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumSumPartitionTest.java)
822+
* [NewManShanksPrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java)
820823
* [OptimalJobSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/OptimalJobSchedulingTest.java)
821824
* [PalindromicPartitioningTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioningTest.java)
822825
* [PartitionProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PartitionProblemTest.java)
Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,48 @@
11
package com.thealgorithms.dynamicprogramming;
22

33
/**
4+
* The NewManShanksPrime class provides a method to determine whether the nth
5+
* New Man Shanks prime matches an expected answer.
6+
*
7+
* <p>This is based on the New Man Shanks prime sequence defined by the recurrence
8+
* relation:</p>
9+
*
10+
* <pre>
11+
* a(n) = 2 * a(n-1) + a(n-2) for n >= 2
12+
* a(0) = 1
13+
* a(1) = 1
14+
* </pre>
15+
*
16+
* <p>For more information on New Man Shanks primes, please refer to the
17+
* <a href="https://en.wikipedia.org/wiki/Newman%E2%80%93Shanks%E2%80%93Williams_prime">
18+
* Wikipedia article</a>.</p>
19+
*
20+
* <p>Note: The class is designed to be non-instantiable.</p>
21+
*
422
* @author <a href="https://github.com/siddhant2002">Siddhant Swarup Mallick</a>
5-
* Program description - To find the New Man Shanks Prime.
6-
* <a href="https://en.wikipedia.org/wiki/Newman%E2%80%93Shanks%E2%80%93Williams_prime">Wikipedia</a>
723
*/
824
public final class NewManShanksPrime {
925
private NewManShanksPrime() {
1026
}
1127

28+
/**
29+
* Calculates the nth New Man Shanks prime and checks if it equals the
30+
* expected answer.
31+
*
32+
* @param n the index of the New Man Shanks prime to calculate (0-based).
33+
* @param expectedAnswer the expected value of the nth New Man Shanks prime.
34+
* @return true if the calculated nth New Man Shanks prime matches the
35+
* expected answer; false otherwise.
36+
*/
1237
public static boolean nthManShanksPrime(int n, int expectedAnswer) {
1338
int[] a = new int[n + 1];
14-
// array of n+1 size is initialized
1539
a[0] = 1;
1640
a[1] = 1;
17-
// The 0th and 1st index position values are fixed. They are initialized as 1
41+
1842
for (int i = 2; i <= n; i++) {
1943
a[i] = 2 * a[i - 1] + a[i - 2];
2044
}
21-
// The loop is continued till n
45+
2246
return a[n] == expectedAnswer;
23-
// returns true if calculated answer matches with expected answer
2447
}
2548
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Unit tests for the NewManShanksPrime class.
10+
* This test class verifies the correctness of the nthManShanksPrime method
11+
* for various input cases.
12+
*/
13+
class NewManShanksPrimeTest {
14+
15+
/**
16+
* Test case for the 1st New Man Shanks prime.
17+
* The expected answer is 1.
18+
*/
19+
@Test
20+
void testNthManShanksPrime1() {
21+
int n = 1;
22+
int expectedAnswer = 1;
23+
assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 1st New Man Shanks prime should be 1.");
24+
}
25+
26+
/**
27+
* Test case for the 2nd New Man Shanks prime.
28+
* The expected answer is 3.
29+
*/
30+
@Test
31+
void testNthManShanksPrime2() {
32+
int n = 2;
33+
int expectedAnswer = 3;
34+
assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 2nd New Man Shanks prime should be 3.");
35+
}
36+
37+
/**
38+
* Test case for the 3rd New Man Shanks prime.
39+
* The expected answer is 7.
40+
*/
41+
@Test
42+
void testNthManShanksPrime3() {
43+
int n = 3;
44+
int expectedAnswer = 7;
45+
assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 3rd New Man Shanks prime should be 7.");
46+
}
47+
48+
/**
49+
* Test case for the 4th New Man Shanks prime.
50+
* The expected answer is 17.
51+
*/
52+
@Test
53+
void testNthManShanksPrime4() {
54+
int n = 4;
55+
int expectedAnswer = 17;
56+
assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 4th New Man Shanks prime should be 17.");
57+
}
58+
59+
/**
60+
* Test case for the 5th New Man Shanks prime.
61+
* The expected answer is 41.
62+
*/
63+
@Test
64+
void testNthManShanksPrime5() {
65+
int n = 5;
66+
int expectedAnswer = 41;
67+
assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 5th New Man Shanks prime should be 41.");
68+
}
69+
70+
/**
71+
* Test case with an incorrect expected answer.
72+
* For n = 2, the expected answer is 3.
73+
*/
74+
@Test
75+
void testNthManShanksPrimeIncorrectAnswer() {
76+
int n = 2;
77+
int expectedAnswer = 4; // Incorrect expected value
78+
assertFalse(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 2nd New Man Shanks prime should not be 4.");
79+
}
80+
}

0 commit comments

Comments
 (0)