diff --git a/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java new file mode 100644 index 000000000..05bb0e7a9 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java @@ -0,0 +1,34 @@ +package com.thealgorithms.maths; + +import java.util.Scanner; + +/* +*To learn about the method, visit the link below : +* https://en.wikipedia.org/wiki/Newton%27s_method +* +* To obtain the square root, no built-in functions should be used +* +* The formula to calculate the root is : root = 0.5(x + n/x), +* here, n is the no. whose square root has to be calculated and +* x has to be guessed such that, the calculation should result into +* the square root of n. +* And the root will be obtained when the error < 0.5 or the precision value can also +* be changed according to the user preference. +*/ + +public class SquareRootWithNewtonRaphsonMethod { + + public static double squareRoot (int n) { + + double x = n; //initially taking a guess that x = n. + double root = 0.5 * (x + n/x); //applying Newton-Raphson Method. + + while (Math.abs(root - x) > 0.0000001) { //root - x = error and error < 0.0000001, 0.0000001 is the precision value taken over here. + + x = root; //decreasing the value of x to root, i.e. decreasing the guess. + root = 0.5 * (x + n/x); + } + + return root; + } +} diff --git a/src/test/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonTestMethod.java b/src/test/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonTestMethod.java new file mode 100644 index 000000000..0158ff35a --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonTestMethod.java @@ -0,0 +1,23 @@ +package com.thealgorithms.maths; + + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class SquareRootWithNewtonRaphsonTestMethod +{ + @Test + void testfor1(){ + Assertions.assertEquals(1,SquareRootWithNewtonRaphsonMethod.squareRoot(1)); + } + + @Test + void testfor2(){ + Assertions.assertEquals(1.414213562373095,SquareRootWithNewtonRaphsonMethod.squareRoot(2)); + } + + @Test + void testfor625(){ + Assertions.assertEquals(25.0,SquareRootWithNewtonRaphsonMethod.squareRoot(625)); + } +}