Find square root of number upto given precision using binary search
Last Updated :
18 Sep, 2022
Given a positive number n and precision p, find the square root of number upto p decimal places using binary search.
Note : Prerequisite : Binary search
Examples:
Input : number = 50, precision = 3
Output : 7.071
Input : number = 10, precision = 4
Output : 3.1622
We have discussed how to compute the integral value of square root in Square Root using Binary Search
Approach :
1) As the square root of number lies in range 0 <= squareRoot <= number, therefore, initialize start and end as : start = 0, end = number.
2) Compare the square of the mid integer with the given number. If it is equal to the number, the square root is found. Else look for the same in the left or right side depending upon the scenario.
3) Once we are done with finding an integral part, start computing the fractional part.
4) Initialize the increment variable by 0.1 and iteratively compute the fractional part up to P places. For each iteration, the increment changes to 1/10th of its previous value.
5) Finally return the answer computed.
Below is the implementation of above approach :
C++
#include <bits/stdc++.h>
using namespace std;
float squareRoot( int number, int precision)
{
int start = 0, end = number;
int mid;
float ans;
while (start <= end) {
mid = (start + end) / 2;
if (mid * mid == number) {
ans = mid;
break ;
}
if (mid * mid < number) {
start = mid + 1;
ans = mid;
}
else {
end = mid - 1;
}
}
float increment = 0.1;
for ( int i = 0; i < precision; i++) {
while (ans * ans <= number) {
ans += increment;
}
ans = ans - increment;
increment = increment / 10;
}
return ans;
}
int main()
{
cout << squareRoot(50, 3) << endl;
cout << squareRoot(10, 4) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static float squareRoot( int number, int precision)
{
int start = 0 , end = number;
int mid;
double ans = 0.0 ;
while (start <= end) {
mid = (start + end) / 2 ;
if (mid * mid == number) {
ans = mid;
break ;
}
if (mid * mid < number) {
start = mid + 1 ;
ans = mid;
}
else {
end = mid - 1 ;
}
}
double increment = 0.1 ;
for ( int i = 0 ; i < precision; i++) {
while (ans * ans <= number) {
ans += increment;
}
ans = ans - increment;
increment = increment / 10 ;
}
return ( float )ans;
}
public static void main(String[] args)
{
System.out.println(squareRoot( 50 , 3 ));
System.out.println(squareRoot( 10 , 4 ));
}
}
|
Python3
def squareRoot(number, precision):
start = 0
end, ans = number, 1
while (start < = end):
mid = int ((start + end) / 2 )
if (mid * mid = = number):
ans = mid
break
if (mid * mid < number):
start = mid + 1
ans = mid
else :
end = mid - 1
increment = 0.1
for i in range ( 0 , precision):
while (ans * ans < = number):
ans + = increment
ans = ans - increment
increment = increment / 10
return ans
print ( round (squareRoot( 50 , 3 ), 4 ))
print ( round (squareRoot( 10 , 4 ), 4 ))
|
C#
using System;
class GFG {
static float squareRoot( int number, int precision)
{
int start = 0, end = number;
int mid;
double ans = 0.0;
while (start <= end) {
mid = (start + end) / 2;
if (mid * mid == number) {
ans = mid;
break ;
}
if (mid * mid < number) {
start = mid + 1;
ans = mid;
}
else {
end = mid - 1;
}
}
double increment = 0.1;
for ( int i = 0; i < precision; i++) {
while (ans * ans <= number) {
ans += increment;
}
ans = ans - increment;
increment = increment / 10;
}
return ( float )ans;
}
public static void Main()
{
Console.WriteLine(squareRoot(50, 3));
Console.WriteLine(squareRoot(10, 4));
}
}
|
PHP
<?php
function squareRoot( $number , $precision )
{
$start =0;
$end = $number ;
$mid ;
$ans ;
while ( $start <= $end )
{
$mid = ( $start + $end ) / 2;
if ( $mid * $mid == $number )
{
$ans = $mid ;
break ;
}
if ( $mid * $mid < $number )
{
$start = $mid + 1;
$ans = $mid ;
}
else
{
$end = $mid - 1;
}
}
$increment = 0.1;
for ( $i = 0; $i < $precision ; $i ++)
{
while ( $ans * $ans <= $number )
{
$ans += $increment ;
}
$ans = $ans - $increment ;
$increment = $increment / 10;
}
return $ans ;
}
echo squareRoot(50, 3), "\n" ;
echo squareRoot(10, 4), "\n" ;
?>
|
Javascript
<script>
function squareRoot(number, precision)
{
let start = 0, end = number;
let mid;
let ans = 0.0;
while (start <= end)
{
mid = (start + end) / 2;
if (mid * mid == number)
{
ans = mid;
break ;
}
if (mid * mid < number) {
start = mid + 1;
ans = mid;
}
else {
end = mid - 1;
}
}
let increment = 0.1;
for (let i = 0; i < precision; i++) {
while (ans * ans <= number) {
ans += increment;
}
ans = ans - increment;
increment = increment / 10;
}
return ans;
}
document.write(squareRoot(50, 3) + "<br/>" );
document.write(squareRoot(10, 4) + "<br/>" );
</script>
|
Output:
7.071
3.1622
Time Complexity : The time required to compute the integral part is O(log(number)) and constant i.e, = precision for computing the fractional part. Therefore, overall time complexity is O(log(number) + precision) which is approximately equal to O(log(number)).
Auxiliary Space: O(1) since it is using constant space for variables
Share your thoughts in the comments
Please Login to comment...