Find root of a number using Newton's method - GeeksforGeeks

06 Feb, 2022
Given an integer N and a tolerance level L, the task is to find the square root of that number using Newton’s Method.Examples:  
Input: N = 16, L = 0.0001 Output: 4 42 = 16Input: N = 327, L = 0.00001 Output: 18.0831  
 
Newton’s Method: Let N be any number then the square root of N can be given by the formula:  
root = 0.5 * (X + (N / X)) where X is any guess which can be assumed to be N or 1.  
 
In the above formula, X is any assumed square root of N and root is the correct square root of N.
Tolerance limit is the maximum difference between X and root allowed.
Approach: The following steps can be followed to compute the answer:  
Assign X to the N itself.Now, start a loop and keep calculating the root which will surely move towards the correct square root of N.Check for the difference between the assumed X and calculated root, if not yet inside tolerance then update root and continue.If the calculated root comes inside the tolerance allowed then break out of the loop.Print the root.
Assign X to the N itself.
Now, start a loop and keep calculating the root which will surely move towards the correct square root of N.
Check for the difference between the assumed X and calculated root, if not yet inside tolerance then update root and continue.
If the calculated root comes inside the tolerance allowed then break out of the loop.
Print the root.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function to return the square root of// a number using Newtons methoddouble squareRoot(double n, float l){    // Assuming the sqrt of n as n only    double x = n;     // The closed guess will be stored in the root    double root;     // To count the number of iterations    int count = 0;     while (1) {        count++;         // Calculate more closed x        root = 0.5 * (x + (n / x));         // Check for closeness        if (abs(root - x) < l)            break;         // Update root        x = root;    }     return root;} // Driver codeint main(){    double n = 327;    float l = 0.00001;     cout << squareRoot(n, l);     return 0;}
// Java implementation of the approachclass GFG{         // Function to return the square root of    // a number using Newtons method    static double squareRoot(double n, double l)    {        // Assuming the sqrt of n as n only        double x = n;             // The closed guess will be stored in the root        double root;             // To count the number of iterations        int count = 0;             while (true)        {            count++;                 // Calculate more closed x            root = 0.5 * (x + (n / x));                 // Check for closeness            if (Math.abs(root - x) < l)                break;                 // Update root            x = root;        }             return root;    }         // Driver code    public static void main (String[] args)    {        double n = 327;        double l = 0.00001;             System.out.println(squareRoot(n, l));    }} // This code is contributed by AnkitRai01
# Python3 implementation of the approach # Function to return the square root of# a number using Newtons methoddef squareRoot(n, l) :     # Assuming the sqrt of n as n only    x = n     # To count the number of iterations    count = 0     while (1) :        count += 1         # Calculate more closed x        root = 0.5 * (x + (n / x))         # Check for closeness        if (abs(root - x) < l) :            break         # Update root        x = root     return root # Driver codeif __name__ == "__main__" :     n = 327    l = 0.00001     print(squareRoot(n, l)) # This code is contributed by AnkitRai01
// C# implementation of the approachusing System; class GFG{         // Function to return the square root of    // a number using Newtons method    static double squareRoot(double n, double l)    {        // Assuming the sqrt of n as n only        double x = n;             // The closed guess will be stored in the root        double root;             // To count the number of iterations        int count = 0;             while (true)        {            count++;                 // Calculate more closed x            root = 0.5 * (x + (n / x));                 // Check for closeness            if (Math.Abs(root - x) < l)                break;                 // Update root            x = root;        }             return root;    }         // Driver code    public static void Main()    {        double n = 327;        double l = 0.00001;             Console.WriteLine(squareRoot(n, l));    }} // This code is contributed by AnkitRai01
<script>    // Javascript implementation of the approach         // Function to return the square root of    // a number using Newtons method    function squareRoot(n, l)    {        // Assuming the sqrt of n as n only        let x = n;               // The closed guess will be stored in the root        let root;               // To count the number of iterations        let count = 0;               while (true)        {            count++;                   // Calculate more closed x            root = 0.5 * (x + (n / x));                   // Check for closeness            if (Math.abs(root - x) < l)                break;                   // Update root            x = root;        }               return root.toFixed(4);    }         let n = 327;    let l = 0.00001;     document.write(squareRoot(n, l));   // This code is contributed by divyesh072019.</script>
18.0831
 
Time Complexity: O(log N)
Auxiliary Space: O(1)
ankthon
himangshushekharjha
divyesh072019
subhammahato348
root
Technical Scripter 2019
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Modular multiplicative inverse
Program to multiply two matrices
Program to print prime numbers from 1 to N.
Count ways to reach the n'th stair
Program to convert a given number to words
Fizz Buzz Implementation
Singular Value Decomposition (SVD)
Check if a number is Palindrome
Find first and last digits of a number