Add tests, improve docs in NewManShanksPrime (#5660)

This commit is contained in:
Hardik Pawar
2024-10-10 23:41:43 +05:30
committed by GitHub
parent 44b0fc9408
commit 41f7e6aa9d
3 changed files with 112 additions and 6 deletions

View File

@@ -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
}
}

View File

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