File tree Expand file tree Collapse file tree 2 files changed +81
-0
lines changed
main/java/com/thealgorithms/dynamicprogramming
test/java/com/thealgorithms/others Expand file tree Collapse file tree 2 files changed +81
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments