Find root of a number using Newton’s method
Last Updated :
30 Oct, 2023
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 = 16
Input: 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.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double squareRoot( double n, float l)
{
double x = n;
double root;
int count = 0;
while (1) {
count++;
root = 0.5 * (x + (n / x));
if ( abs (root - x) < l)
break ;
x = root;
}
return root;
}
int main()
{
double n = 327;
float l = 0.00001;
cout << squareRoot(n, l);
return 0;
}
|
Java
class GFG
{
static double squareRoot( double n, double l)
{
double x = n;
double root;
int count = 0 ;
while ( true )
{
count++;
root = 0.5 * (x + (n / x));
if (Math.abs(root - x) < l)
break ;
x = root;
}
return root;
}
public static void main (String[] args)
{
double n = 327 ;
double l = 0.00001 ;
System.out.println(squareRoot(n, l));
}
}
|
Python3
def squareRoot(n, l) :
x = n
count = 0
while ( 1 ) :
count + = 1
root = 0.5 * (x + (n / x))
if ( abs (root - x) < l) :
break
x = root
return root
if __name__ = = "__main__" :
n = 327
l = 0.00001
print (squareRoot(n, l))
|
C#
using System;
class GFG
{
static double squareRoot( double n, double l)
{
double x = n;
double root;
int count = 0;
while ( true )
{
count++;
root = 0.5 * (x + (n / x));
if (Math.Abs(root - x) < l)
break ;
x = root;
}
return root;
}
public static void Main()
{
double n = 327;
double l = 0.00001;
Console.WriteLine(squareRoot(n, l));
}
}
|
Javascript
<script>
function squareRoot(n, l)
{
let x = n;
let root;
let count = 0;
while ( true )
{
count++;
root = 0.5 * (x + (n / x));
if (Math.abs(root - x) < l)
break ;
x = root;
}
return root.toFixed(4);
}
let n = 327;
let l = 0.00001;
document.write(squareRoot(n, l));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Recursive Approach:
- Start by defining the function findSqrt that takes three arguments – the number whose square root is to be found N, the current guess guess, and the tolerance level tolerance.
- Compute the next guess using the Newton’s formula next_guess = (guess + N/guess) / 2.
- Check if the difference between the current guess and the next guess is <= tolerance level tolerance using the abs() function. If the condition is satisfied, return the next guess.
- Otherwise, recursively call the findSqrt function with the new guess.
- Last print the result
Below is the implementation of the above approach:
C++
#include <iostream>
#include <cmath>
using namespace std;
double findSqrt( double N, double guess, double tolerance)
{
double next_guess = (guess + N/guess) / 2;
if ( abs (guess - next_guess) <= tolerance) {
return next_guess;
}
else {
return findSqrt(N, next_guess, tolerance);
}
}
int main()
{
double N=327, L=0.00001;
double guess = N/2;
double sqrt = findSqrt(N, guess, L);
cout << sqrt << endl;
return 0;
}
|
Java
public class Main {
public static double findSqrt( double N, double guess,
double tolerance)
{
double nextGuess = (guess + N / guess) / 2 ;
if (Math.abs(guess - nextGuess) <= tolerance) {
return nextGuess;
}
else {
return findSqrt(N, nextGuess, tolerance);
}
}
public static void main(String[] args)
{
double N = 327 , L = 0.00001 ;
double guess = N / 2 ;
double sqrt = findSqrt(N, guess, L);
System.out.printf( "%.4f%n" , sqrt);
}
}
|
Python3
def find_sqrt(N, guess, tolerance):
next_guess = (guess + N / guess) / 2
if abs (guess - next_guess) < = tolerance:
return next_guess
else :
return find_sqrt(N, next_guess, tolerance)
if __name__ = = "__main__" :
N = 327
tolerance = 0.00001
guess = N / 2
sqrt = find_sqrt(N, guess, tolerance)
sqrt = round (sqrt, 4 )
print (sqrt)
|
C#
using System;
class Program
{
static double FindSqrt( double N, double guess, double tolerance)
{
double nextGuess = (guess + N / guess) / 2;
if (Math.Abs(guess - nextGuess) <= tolerance)
{
return nextGuess;
}
else
{
return FindSqrt(N, nextGuess, tolerance);
}
}
static void Main()
{
double N = 327;
double tolerance = 0.00001;
double guess = N / 2;
double sqrt = FindSqrt(N, guess, tolerance);
sqrt = Math.Round(sqrt, 4);
Console.WriteLine(sqrt);
}
}
|
Javascript
function findSqrt(N, guess, tolerance) {
let nextGuess = (guess + N / guess) / 2;
if (Math.abs(guess - nextGuess) <= tolerance) {
return nextGuess;
} else {
return findSqrt(N, nextGuess, tolerance);
}
}
let N = 327;
let tolerance = 0.00001;
let guess = N / 2;
let sqrt = findSqrt(N, guess, tolerance);
console.log(sqrt.toFixed(4));
|
Time Complexity: O(log N), where N is the input number.
Auxiliary Space: O(log N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...