Repository: TheAlgorithms/Java
Pull request #2962 (2022-02-24 11:03:39)

Issue/2961 - small bug FFT.java
### **Describe your change:**

- [x] Fix a bug or typo in an existing algorithm?

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
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/main/java/com/thealgorithms/maths/FFT.java b/src/main/java/com/thealgorithms/maths/FFT.java
index 56bb89de..374d3fca 100644
--- a/src/main/java/com/thealgorithms/maths/FFT.java
+++ b/src/main/java/com/thealgorithms/maths/FFT.java
@@ -142,8 +142,10 @@ public class FFT {
          */
         public Complex divide(Complex z) {
             Complex temp = new Complex();
-            temp.real = (this.real * z.real + this.img * z.img) / (z.abs() * z.abs());
-            temp.img = (this.img * z.real - this.real * z.img) / (z.abs() * z.abs());
+            double d = z.abs() * z.abs();
+            d = (double)Math.round(d * 1000000000d) / 1000000000d;
+            temp.real = (this.real * z.real + this.img * z.img) / (d);
+            temp.img = (this.img * z.real - this.real * z.img) / (d);
             return temp;
         }
 
diff --git a/src/main/java/com/thealgorithms/maths/Gaussian.java b/src/main/java/com/thealgorithms/maths/Gaussian.java
index 29ea4f61..75450654 100644
--- a/src/main/java/com/thealgorithms/maths/Gaussian.java
+++ b/src/main/java/com/thealgorithms/maths/Gaussian.java
@@ -1,58 +1,53 @@
-/**
- * \file
- * \brief [Gaussian elimination
- * method](https://en.wikipedia.org/wiki/Gaussian_elimination)
- * @author [Sachwin Kohli](https://github.com/Sachwin-Kohli)
- */
 package com.thealgorithms.maths;
 
-import java.util.*;
+import java.util.ArrayList;
 
-/**
- * Main function
- */
 public class Gaussian {
 
-    public static void main(String[] args) {
-        int mat_size, i, j, step;
-        Scanner sc = new Scanner(System.in);
-
-        System.out.println("Matrix Size : ");
-        mat_size = sc.nextInt();
+    public static ArrayList<Double> gaussian(int mat_size, ArrayList<Double> matrix) {
+        ArrayList<Double> answerArray = new ArrayList<Double>();
+        int i, j = 0;
 
         double[][] mat = new double[mat_size + 1][mat_size + 1];
         double[][] x = new double[mat_size][mat_size + 1];
 
-        System.out.println("Enter value of the matrix");
-        System.out.println(' ');
+        // Values from arraylist to matrix
         for (i = 0; i < mat_size; i++) {
             for (j = 0; j <= mat_size; j++) {
-                mat[i][j] = sc.nextInt();
+                mat[i][j] = matrix.get(i);
             }
         }
 
-        // perform Gaussian elimination
+        mat = gaussianElimination(mat_size, i, mat);
+        answerArray = valueOfGaussian(mat_size, x, mat);
+        return answerArray;
+    }
+
+    // Perform Gaussian elimination
+    public static double[][] gaussianElimination(int mat_size, int i, double[][] mat) {
+        int step = 0;
         for (step = 0; step < mat_size - 1; step++) {
             for (i = step; i < mat_size - 1; i++) {
                 double a = (mat[i + 1][step] / mat[step][step]);
 
-                for (j = step; j <= mat_size; j++) {
+                for (int j = step; j <= mat_size; j++) {
                     mat[i + 1][j] = mat[i + 1][j] - (a * mat[step][j]);
                 }
             }
         }
+        return mat;
+    }
+
+    // calcilate the x_1, x_2,... values of the gaussian and save it in an arraylist.
+    public static ArrayList<Double> valueOfGaussian(int mat_size, double[][] x, double[][] mat) {
+        ArrayList<Double> answerArray = new ArrayList<Double>();
+        int i, j;
 
-        System.out.println("Matrix using Gaussian Elimination method: ");
-        System.out.println(" ");
         for (i = 0; i < mat_size; i++) {
             for (j = 0; j <= mat_size; j++) {
                 x[i][j] = mat[i][j];
-                System.out.print(mat[i][j] + " ");
             }
-            System.out.println();
         }
-        System.out.println("Value of the Gaussian Elimination method: ");
-        System.out.println(" ");
 
         for (i = mat_size - 1; i >= 0; i--) {
             double sum = 0;
@@ -65,8 +60,8 @@ public class Gaussian {
             } else {
                 x[i][i] = (x[i][mat_size] - sum) / (x[i][i]);
             }
-            System.out.print("x" + i + "=" + x[i][i]);
-            System.out.println(" ");
+            answerArray.add(x[i][j]);
         }
+        return answerArray;
     }
-}
+}
\ No newline at end of file


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/GaussianTest.java b/src/test/java/com/thealgorithms/maths/GaussianTest.java
new file mode 100644
index 00000000..85e3b7e8
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/GaussianTest.java
@@ -0,0 +1,32 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Test;
+import java.util.ArrayList;
+
+import static com.thealgorithms.maths.Gaussian.gaussian;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class GaussianTest {
+
+    // easy pass test for the whole class. Matrix of 2*3.
+    @Test
+    void passTest1()
+    {
+        ArrayList<Double> list = new ArrayList<Double>();
+        ArrayList<Double> gaussian = new ArrayList<Double>();
+        ArrayList<Double> answer = new ArrayList<Double>();
+        answer.add(0.0);
+        answer.add(1.0);
+
+        int matrixSize = 2;
+        list.add(1.0);
+        list.add(1.0);
+        list.add(1.0);
+        list.add(2.0);
+        list.add(1.0);
+        list.add(1.0);
+        gaussian=gaussian(matrixSize,list);
+
+        assertEquals(answer,gaussian);
+    }
+}
\ 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