Skip to content

Commit cf07de8

Browse files
siddhant2002siriak
andauthored
Add Newman–Shanks–Williams prime (TheAlgorithms#2884) (TheAlgorithms#2955)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
1 parent 68e2ba2 commit cf07de8

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/** Author : Siddhant Swarup Mallick
2+
* Github : https://github.com/siddhant2002
3+
*/
4+
5+
6+
/** Program description - To find the New Man Shanks Prime. */
7+
/** Wikipedia Link - https://en.wikipedia.org/wiki/Newman%E2%80%93Shanks%E2%80%93Williams_prime */
8+
9+
package com.thealgorithms.dynamicprogramming;
10+
11+
public class NewManShanksPrime {
12+
public static boolean nthManShanksPrime(int n , int expected_answer)
13+
{
14+
int a[] = new int[n+1];
15+
// array of n+1 size is initialized
16+
a[0] = a[1] = 1;
17+
// The 0th and 1st index position values are fixed. They are initialized as 1
18+
for(int i=2;i<=n;i++)
19+
{
20+
a[i]=2*a[i-1]+a[i-2];
21+
}
22+
// The loop is continued till n
23+
return a[n]==expected_answer;
24+
// returns true if calculated answer matches with expected answer
25+
}
26+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.thealgorithms.others;
2+
import org.junit.jupiter.api.Test;
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import com.thealgorithms.dynamicprogramming.NewManShanksPrime;
6+
public class NewManShanksPrimeTest {
7+
@Test
8+
void testOne()
9+
{
10+
assertTrue(NewManShanksPrime.nthManShanksPrime(1,1));
11+
}
12+
13+
@Test
14+
void testTwo()
15+
{
16+
assertTrue(NewManShanksPrime.nthManShanksPrime(2,3));
17+
}
18+
19+
@Test
20+
void testThree()
21+
{
22+
assertTrue(NewManShanksPrime.nthManShanksPrime(3,7));
23+
}
24+
25+
@Test
26+
void testFour()
27+
{
28+
assertTrue(NewManShanksPrime.nthManShanksPrime(4,17));
29+
}
30+
31+
@Test
32+
void testFive()
33+
{
34+
assertTrue(NewManShanksPrime.nthManShanksPrime(5,41));
35+
}
36+
37+
@Test
38+
void testSix()
39+
{
40+
assertTrue(NewManShanksPrime.nthManShanksPrime(6,99));
41+
}
42+
43+
@Test
44+
void testSeven()
45+
{
46+
assertTrue(NewManShanksPrime.nthManShanksPrime(7,239));
47+
}
48+
49+
@Test
50+
void testEight()
51+
{
52+
assertTrue(NewManShanksPrime.nthManShanksPrime(8,577));
53+
}
54+
55+
}

0 commit comments

Comments
 (0)