Skip to content

Add tests, improve docs in NewManShanksPrime #5660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@
* [MatrixChainMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplicationTest.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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>This is based on the New Man Shanks prime sequence defined by the recurrence
* relation:</p>
*
* <pre>
* a(n) = 2 * a(n-1) + a(n-2) for n >= 2
* a(0) = 1
* a(1) = 1
* </pre>
*
* <p>For more information on New Man Shanks primes, please refer to the
* <a href="https://en.wikipedia.org/wiki/Newman%E2%80%93Shanks%E2%80%93Williams_prime">
* Wikipedia article</a>.</p>
*
* <p>Note: The class is designed to be non-instantiable.</p>
*
* @author <a href="https://github.com/siddhant2002">Siddhant Swarup Mallick</a>
* Program description - To find the New Man Shanks Prime.
* <a href="https://en.wikipedia.org/wiki/Newman%E2%80%93Shanks%E2%80%93Williams_prime">Wikipedia</a>
*/
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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.thealgorithms.dynamicprogramming;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

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.");
}
}