Repository: TheAlgorithms/Java
Pull request #3228 (2022-08-22 07:40:48)

'Fixes #3199'
### **Describe your change:**

- [x] Add an algorithm?
- [] Fix a bug or typo in an existing algorithm?
- [] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

### **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] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.


siddhant2002: ok
siddhant2002: @siriak please check the updated pr

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java b/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java
new file mode 100644
index 00000000..205447c9
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java
@@ -0,0 +1,59 @@
+/** Author : Siddhant Swarup Mallick
+ * Github : https://github.com/siddhant2002
+ */
+
+
+/** Program description - To find out the inverse square root of the given number*/
+
+/** Wikipedia Link - https://en.wikipedia.org/wiki/Fast_inverse_square_root */
+
+
+package com.thealgorithms.maths;
+
+public class FastInverseSqrt {
+    public static boolean inverseSqrt(float number) {
+        float x = number;
+        float xhalf = 0.5f * x;
+        int i = Float.floatToIntBits(x);
+        i = 0x5f3759df - (i >> 1);
+        x = Float.intBitsToFloat(i);
+        x = x * (1.5f - xhalf * x * x);
+        return x == (float)((float)1/(float)Math.sqrt(number));
+    }
+    
+    /**
+     * Returns the inverse square root of the given number upto 6 - 8 decimal places.
+     * calculates the inverse square root of the given number and returns true if calculated answer matches with given answer else returns false
+     */
+
+    
+    public static boolean inverseSqrt(double number) {
+        double x = number;
+        double xhalf = 0.5d * x;
+        long i = Double.doubleToLongBits(x);
+        i = 0x5fe6ec85e7de30daL - (i >> 1);
+        x = Double.longBitsToDouble(i);
+        for (int it = 0; it < 4; it++) {
+            x = x * (1.5d - xhalf * x * x);
+        }
+        x *= number;
+        return x == 1/Math.sqrt(number);
+    }
+    /**
+     * Returns the inverse square root of the given number upto 14 - 16 decimal places.
+     * calculates the inverse square root of the given number and returns true if calculated answer matches with given answer else returns false
+     */
+}
+
+
+/**
+     * OUTPUT :
+     * Input - number = 4522
+     * Output: it calculates the inverse squareroot of a number and returns true with it matches the given answer else returns false.
+     * 1st approach Time Complexity : O(1)
+     * Auxiliary Space Complexity : O(1)
+     * Input - number = 4522
+     * Output: it calculates the inverse squareroot of a number and returns true with it matches the given answer else returns false.
+     * 2nd approach Time Complexity : O(1)
+     * Auxiliary Space Complexity : O(1)
+     */


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/FastInverseSqrtTests.java b/src/test/java/com/thealgorithms/maths/FastInverseSqrtTests.java
new file mode 100644
index 00000000..6c64ba44
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/FastInverseSqrtTests.java
@@ -0,0 +1,56 @@
+package com.thealgorithms.maths;
+import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.*;
+
+
+
+public class FastInverseSqrtTests {
+    @Test
+	void testForOneElement() 
+	{
+		assertFalse(FastInverseSqrt.inverseSqrt(1332));
+        // calls for the 2nd inverse method
+    }
+	@Test
+	void testForsecond() 
+	{
+		assertFalse(FastInverseSqrt.inverseSqrt(1332f));
+        // calls for the 1st inverse method
+	}
+
+	@Test
+	void testForThird() 
+	{
+		assertFalse(FastInverseSqrt.inverseSqrt(1));
+	}
+    
+	@Test
+	void testForFourth() 
+	{
+		assertFalse(FastInverseSqrt.inverseSqrt(1f));
+	}
+
+	@Test
+	void testForFifth() 
+	{
+		assertFalse(FastInverseSqrt.inverseSqrt(4522));
+	}
+    
+	@Test
+	void testForSixth() 
+	{
+		assertFalse(FastInverseSqrt.inverseSqrt(4522f));
+	}
+
+	@Test
+	void testForSeventh() 
+	{
+		assertFalse(FastInverseSqrt.inverseSqrt(21));
+    }
+
+	@Test
+	void testForEighth() 
+	{
+		assertFalse(FastInverseSqrt.inverseSqrt(21f));
+	}
+}