Add Newman–Shanks–Williams prime (#2884) (#2955)

Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:
Siddhant Swarup Mallick 2022-02-26 13:24:10 +05:30 committed by GitHub
parent 68e2ba29d4
commit cf07de8afa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,26 @@
/** Author : Siddhant Swarup Mallick
* Github : https://github.com/siddhant2002
*/
/** Program description - To find the New Man Shanks Prime. */
/** Wikipedia Link - https://en.wikipedia.org/wiki/Newman%E2%80%93Shanks%E2%80%93Williams_prime */
package com.thealgorithms.dynamicprogramming;
public class NewManShanksPrime {
public static boolean nthManShanksPrime(int n , int expected_answer)
{
int a[] = new int[n+1];
// array of n+1 size is initialized
a[0] = 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]==expected_answer;
// returns true if calculated answer matches with expected answer
}
}

View File

@ -0,0 +1,55 @@
package com.thealgorithms.others;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import com.thealgorithms.dynamicprogramming.NewManShanksPrime;
public class NewManShanksPrimeTest {
@Test
void testOne()
{
assertTrue(NewManShanksPrime.nthManShanksPrime(1,1));
}
@Test
void testTwo()
{
assertTrue(NewManShanksPrime.nthManShanksPrime(2,3));
}
@Test
void testThree()
{
assertTrue(NewManShanksPrime.nthManShanksPrime(3,7));
}
@Test
void testFour()
{
assertTrue(NewManShanksPrime.nthManShanksPrime(4,17));
}
@Test
void testFive()
{
assertTrue(NewManShanksPrime.nthManShanksPrime(5,41));
}
@Test
void testSix()
{
assertTrue(NewManShanksPrime.nthManShanksPrime(6,99));
}
@Test
void testSeven()
{
assertTrue(NewManShanksPrime.nthManShanksPrime(7,239));
}
@Test
void testEight()
{
assertTrue(NewManShanksPrime.nthManShanksPrime(8,577));
}
}