From cf4a159aad69b8a108b53dc2d9b040cd55ddea38 Mon Sep 17 00:00:00 2001 From: Shashwat Gupta Date: Sat, 20 Aug 2022 11:50:43 +0530 Subject: [PATCH 1/4] Addeing the program to calculate square root using Newton Raphson method --- .../SquareRootWithNewtonRaphsonMethod.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java 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 000000000000..df38470061e1 --- /dev/null +++ b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java @@ -0,0 +1,48 @@ +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 void main(String[] args) { + + Scanner in = new Scanner(System.in); + System.out.print("Enter n to find it's square root: "); + int n = in.nextInt(); + + System.out.println("Square root of " + n + " is " + sqRoot(n)); + } + + public static double sqRoot (int n) { + + double x = n; //initially taking a guess that x = n. + double root; + + while (true) { + + root = 0.5 * (x + n/x); //applying Newton-Raphson Method. + + if (Math.abs(root - x) < 0.1){ //root - x = error and error < 0.1, 0.1 is the precision value taken over here. + break; //if error is below the stated precision value, break out of the loop. + } + + x = root; //decreasing the value of x to root, i.e. decreasing the guess. + } + + return root; + } +} From bbda5815916d16cd620757450aac68c329b42fa7 Mon Sep 17 00:00:00 2001 From: Shashwat Gupta Date: Wed, 24 Aug 2022 10:41:00 +0530 Subject: [PATCH 2/4] Changed the name of the function from sqRoot to squareRoot --- .../maths/SquareRootWithNewtonRaphsonMethod.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java index df38470061e1..488d7498fa8b 100644 --- a/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java +++ b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java @@ -24,10 +24,10 @@ public static void main(String[] args) { System.out.print("Enter n to find it's square root: "); int n = in.nextInt(); - System.out.println("Square root of " + n + " is " + sqRoot(n)); + System.out.println("Square root of " + n + " is " + squareRoot(n)); } - public static double sqRoot (int n) { + public static double squareRoot (int n) { double x = n; //initially taking a guess that x = n. double root; From 3f17503178646d2726776eb406d16c55548c1dc8 Mon Sep 17 00:00:00 2001 From: Shashwat Gupta Date: Fri, 26 Aug 2022 11:43:16 +0530 Subject: [PATCH 3/4] Modified the main program, removed main method, and added JUnit Test --- .../SquareRootWithNewtonRaphsonMethod.java | 20 +++------------- ...SquareRootWithNewtonRaphsonTestMethod.java | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+), 17 deletions(-) create mode 100644 src/test/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonTestMethod.java diff --git a/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java index 488d7498fa8b..ad1ac86f9b7d 100644 --- a/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java +++ b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java @@ -18,29 +18,15 @@ public class SquareRootWithNewtonRaphsonMethod { - public static void main(String[] args) { - - Scanner in = new Scanner(System.in); - System.out.print("Enter n to find it's square root: "); - int n = in.nextInt(); - - System.out.println("Square root of " + n + " is " + squareRoot(n)); - } - public static double squareRoot (int n) { double x = n; //initially taking a guess that x = n. - double root; - - while (true) { - - root = 0.5 * (x + n/x); //applying Newton-Raphson Method. + double root = 0.5 * (x + n/x); //applying Newton-Raphson Method. - if (Math.abs(root - x) < 0.1){ //root - x = error and error < 0.1, 0.1 is the precision value taken over here. - break; //if error is below the stated precision value, break out of the loop. - } + while (Math.abs(root - x) > 0.0000001) { //root - x = error and error < 0.01, 0.01 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 000000000000..0158ff35ad2b --- /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)); + } +} From fde6af389acdb066bb7835da1acef4280b3919a7 Mon Sep 17 00:00:00 2001 From: Shashwat Gupta Date: Fri, 26 Aug 2022 11:45:27 +0530 Subject: [PATCH 4/4] Documentation changes --- .../thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java index ad1ac86f9b7d..05bb0e7a9e3d 100644 --- a/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java +++ b/src/main/java/com/thealgorithms/maths/SquareRootWithNewtonRaphsonMethod.java @@ -23,7 +23,7 @@ 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.01, 0.01 is the precision value taken over here. + 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);