From 424a341e8631d866d94b776772cf134b07bbb075 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 11:21:59 +0530 Subject: [PATCH 1/4] Add tests, improve docs in `NewManShanksPrime` --- .../dynamicprogramming/NewManShanksPrime.java | 35 ++++++-- .../NewManShanksPrimeTest.java | 86 +++++++++++++++++++ 2 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java index d15ea1f78a78..3db40148f1c2 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java @@ -1,25 +1,48 @@ package com.thealgorithms.dynamicprogramming; /** + * The NewManShanksPrime class provides a method to determine whether the nth + * New Man Shanks prime matches an expected answer. + * + *

This is based on the New Man Shanks prime sequence defined by the recurrence + * relation:

+ * + *
+ * a(n) = 2 * a(n-1) + a(n-2) for n >= 2
+ * a(0) = 1
+ * a(1) = 1
+ * 
+ * + *

For more information on New Man Shanks primes, please refer to the + * + * Wikipedia article.

+ * + *

Note: The class is designed to be non-instantiable.

+ * * @author Siddhant Swarup Mallick - * Program description - To find the New Man Shanks Prime. - * Wikipedia */ public final class NewManShanksPrime { private NewManShanksPrime() { } + /** + * Calculates the nth New Man Shanks prime and checks if it equals the + * expected answer. + * + * @param n the index of the New Man Shanks prime to calculate (0-based). + * @param expectedAnswer the expected value of the nth New Man Shanks prime. + * @return true if the calculated nth New Man Shanks prime matches the + * expected answer; false otherwise. + */ public static boolean nthManShanksPrime(int n, int expectedAnswer) { int[] a = new int[n + 1]; - // array of n+1 size is initialized a[0] = 1; a[1] = 1; - // The 0th and 1st index position values are fixed. They are initialized as 1 + for (int i = 2; i <= n; i++) { a[i] = 2 * a[i - 1] + a[i - 2]; } - // The loop is continued till n + return a[n] == expectedAnswer; - // returns true if calculated answer matches with expected answer } } diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java new file mode 100644 index 000000000000..2045af88a846 --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java @@ -0,0 +1,86 @@ +package com.thealgorithms.dynamicprogramming; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; + +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the NewManShanksPrime class. + * This test class verifies the correctness of the nthManShanksPrime method + * for various input cases. + */ +class NewManShanksPrimeTest { + + /** + * Test case for the 1st New Man Shanks prime. + * The expected answer is 1. + */ + @Test + void testNthManShanksPrime1() { + int n = 1; + int expectedAnswer = 1; + assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), + "The 1st New Man Shanks prime should be 1."); + } + + /** + * Test case for the 2nd New Man Shanks prime. + * The expected answer is 3. + */ + @Test + void testNthManShanksPrime2() { + int n = 2; + int expectedAnswer = 3; + assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), + "The 2nd New Man Shanks prime should be 3."); + } + + /** + * Test case for the 3rd New Man Shanks prime. + * The expected answer is 7. + */ + @Test + void testNthManShanksPrime3() { + int n = 3; + int expectedAnswer = 7; + assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), + "The 3rd New Man Shanks prime should be 7."); + } + + /** + * Test case for the 4th New Man Shanks prime. + * The expected answer is 17. + */ + @Test + void testNthManShanksPrime4() { + int n = 4; + int expectedAnswer = 17; + assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), + "The 4th New Man Shanks prime should be 17."); + } + + /** + * Test case for the 5th New Man Shanks prime. + * The expected answer is 41. + */ + @Test + void testNthManShanksPrime5() { + int n = 5; + int expectedAnswer = 41; + assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), + "The 5th New Man Shanks prime should be 41."); + } + + /** + * Test case with an incorrect expected answer. + * For n = 2, the expected answer is 3. + */ + @Test + void testNthManShanksPrimeIncorrectAnswer() { + int n = 2; + int expectedAnswer = 4; // Incorrect expected value + assertFalse(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), + "The 2nd New Man Shanks prime should not be 4."); + } +} From cc57a7f1390a77f3589874178626270ad4dc6a03 Mon Sep 17 00:00:00 2001 From: Hardvan Date: Wed, 9 Oct 2024 05:52:19 +0000 Subject: [PATCH 2/4] Update directory --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6042dd1b5e0d..5e77fbf2a897 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -31,6 +31,7 @@ * [IsPowerTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/IsPowerTwo.java) * [LowestSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/LowestSetBit.java) * [NonRepeatingNumberFinder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinder.java) + * [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java) * [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java) * [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java) * [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java) @@ -638,6 +639,7 @@ * [IsPowerTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/IsPowerTwoTest.java) * [LowestSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/LowestSetBitTest.java) * [NonRepeatingNumberFinderTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NonRepeatingNumberFinderTest.java) + * [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java) * [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java) * [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java) * [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java) @@ -792,6 +794,7 @@ * [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java) * [MinimumPathSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumPathSumTest.java) * [MinimumSumPartitionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumSumPartitionTest.java) + * [NewManShanksPrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java) * [OptimalJobSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/OptimalJobSchedulingTest.java) * [PalindromicPartitioningTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioningTest.java) * [PartitionProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PartitionProblemTest.java) From 8d408b6138170a0491f28e5326840ee3d91e404f Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Wed, 9 Oct 2024 11:24:24 +0530 Subject: [PATCH 3/4] Fix --- .../NewManShanksPrimeTest.java | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java index 2045af88a846..a16ad67d6470 100644 --- a/src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java +++ b/src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java @@ -1,7 +1,7 @@ package com.thealgorithms.dynamicprogramming; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -20,8 +20,7 @@ class NewManShanksPrimeTest { void testNthManShanksPrime1() { int n = 1; int expectedAnswer = 1; - assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), - "The 1st New Man Shanks prime should be 1."); + assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 1st New Man Shanks prime should be 1."); } /** @@ -32,8 +31,7 @@ void testNthManShanksPrime1() { void testNthManShanksPrime2() { int n = 2; int expectedAnswer = 3; - assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), - "The 2nd New Man Shanks prime should be 3."); + assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 2nd New Man Shanks prime should be 3."); } /** @@ -44,8 +42,7 @@ void testNthManShanksPrime2() { void testNthManShanksPrime3() { int n = 3; int expectedAnswer = 7; - assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), - "The 3rd New Man Shanks prime should be 7."); + assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 3rd New Man Shanks prime should be 7."); } /** @@ -56,8 +53,7 @@ void testNthManShanksPrime3() { void testNthManShanksPrime4() { int n = 4; int expectedAnswer = 17; - assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), - "The 4th New Man Shanks prime should be 17."); + assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 4th New Man Shanks prime should be 17."); } /** @@ -68,8 +64,7 @@ void testNthManShanksPrime4() { void testNthManShanksPrime5() { int n = 5; int expectedAnswer = 41; - assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), - "The 5th New Man Shanks prime should be 41."); + assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 5th New Man Shanks prime should be 41."); } /** @@ -80,7 +75,6 @@ void testNthManShanksPrime5() { void testNthManShanksPrimeIncorrectAnswer() { int n = 2; int expectedAnswer = 4; // Incorrect expected value - assertFalse(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), - "The 2nd New Man Shanks prime should not be 4."); + assertFalse(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 2nd New Man Shanks prime should not be 4."); } } From 0ef7d343774b3ba96bd0c47ecc15c0ae5337d289 Mon Sep 17 00:00:00 2001 From: siriak Date: Thu, 10 Oct 2024 18:08:08 +0000 Subject: [PATCH 4/4] Update directory --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 429ab53a6ac0..4afe7c78e0eb 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -262,6 +262,7 @@ * [LongestValidParentheses](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestValidParentheses.java) * [MatrixChainMultiplication](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplication.java) * [MatrixChainRecursiveTopDownMemoisation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisation.java) + * [MaximumSumOfNonAdjacentElements](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MaximumSumOfNonAdjacentElements.java) * [MinimumPathSum](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MinimumPathSum.java) * [MinimumSumPartition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java) * [NewManShanksPrime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java) @@ -815,6 +816,7 @@ * [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java) * [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java) * [MatrixChainMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplicationTest.java) + * [MaximumSumOfNonAdjacentElementsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MaximumSumOfNonAdjacentElementsTest.java) * [MinimumPathSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumPathSumTest.java) * [MinimumSumPartitionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumSumPartitionTest.java) * [NewManShanksPrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java)