From cf07de8afae9d1344e3f0289c309b7623996a6d3 Mon Sep 17 00:00:00 2001 From: Siddhant Swarup Mallick <78552027+siddhant2002@users.noreply.github.com> Date: Sat, 26 Feb 2022 13:24:10 +0530 Subject: [PATCH] =?UTF-8?q?Add=20Newman=E2=80=93Shanks=E2=80=93Williams=20?= =?UTF-8?q?prime=20(#2884)=20(#2955)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Andrii Siriak --- .../dynamicprogramming/NewManShanksPrime.java | 26 +++++++++ .../others/NewManShanksPrimeTest.java | 55 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java create mode 100644 src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java new file mode 100644 index 00000000..645f4cfa --- /dev/null +++ b/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java @@ -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 + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java b/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java new file mode 100644 index 00000000..134f4fbd --- /dev/null +++ b/src/test/java/com/thealgorithms/others/NewManShanksPrimeTest.java @@ -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)); + } + +} \ No newline at end of file