Square root of a number without using sqrt() function
Last Updated :
30 Mar, 2023
Given a number N, the task is to find the square root of N without using sqrt() function.
Examples:
Input: N = 25
Output: 5
Input: N = 3
Output: 1.73205
Input: N = 2.5
Output: 1.58114
Approach 1:
We can consider (?x-?x)2 = 0.
Replacing one of the ?x‘s with y, then the equation becomes (y-?x)2 => y2 – 2y?x + x = 0
=> ?x = (y2 + x) / 2y
=> ?x = (y + x/y) / 2
In the above equation, we are considering ?x as z.
So, to get a required decimal value we can compare the difference of y and z to 10-p (to get the result upto 5 decimal digits, compare y-z to 10-5=0.00001). Until y-z exceeds it, the iteration continues.
Below is the implementation of the above idea:
C
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double findSqrt( double x)
{
if (x < 2)
return x;
double y = x;
double z = (y + (x / y)) / 2;
while ( fabs (y - z) >= 0.00001) {
y = z;
z = (y + (x / y)) / 2;
}
return z;
}
int main()
{
double n = 3;
double ans = findSqrt(n);
printf ( "%.5f is the square root of 3\n" , ans);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
double findSqrt( double x)
{
if (x < 2)
return x;
double y = x;
double z = (y + (x / y)) / 2;
while ( abs (y - z) >= 0.00001) {
y = z;
z = (y + (x / y)) / 2;
}
return z;
}
int main()
{
double n = 3;
double ans = findSqrt(n);
cout << setprecision(6) << ans
<< " is the square root of 3" << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
public static double findSqrt( double x)
{
if (x < 2 )
return x;
double y = x;
double z = (y + (x / y)) / 2 ;
while (Math.abs(y - z) >= 0.00001 ) {
y = z;
z = (y + (x / y)) / 2 ;
}
return z;
}
public static void main(String[] args)
{
double n = 3 ;
double ans = findSqrt(n);
System.out.println(String.format( "%.5f" , ans)
+ " is the square root of 3" );
}
}
|
Python3
def findSqrt(x):
if x < 2 :
return x
y = x
z = (y + (x / y)) / 2
while abs (y - z) > = 0.00001 :
y = z
z = (y + (x / y)) / 2
return z
if __name__ = = '__main__' :
n = 323242
ans = findSqrt(n)
print (ans)
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static double findSqrt( double x)
{
if (x < 2)
return x;
double y = x;
double z = (y + (x / y)) / 2;
while (Math.Abs(y - z) >= 0.00001) {
y = z;
z = (y + (x / y)) / 2;
}
return z;
}
static public void Main()
{
double n = 3;
double ans = findSqrt(n);
ans = Math.Round(ans, 5);
Console.WriteLine(ans + " is the square root of 3" );
}
}
|
Javascript
function findSqrt(x){
if (x < 2){
return x;
}
let y = x;
let z = (y + (x/y))/2;
while (Math.abs(y-z)>=0.00001){
y = z;
z = (y + (x/y))/2;
}
return z;
}
let n = 3;
let ans = findSqrt(n);
console.log(ans.toPrecision(6) + " is the square root of 3" );
|
Output
1.73205 is the square root of 3
Time Complexity: O(log N)
Auxiliary Space: O(1)
Approach 2:
- Start iterating from i = 1. If i * i = n, then print i as n is a perfect square whose square root is i.
- Else find the smallest i for which i * i is strictly greater than n.
- Now we know square root of n lies in the interval i – 1 and i and we can use Binary Search algorithm to find the square root.
- Find mid of i – 1 and i and compare mid * mid with n, with precision upto 5 decimal places.
- If mid * mid = n then return mid.
- If mid * mid < n then recur for the second half.
- If mid * mid > n then recur for the first half.
Below is the implementation of the above approach:
C
#include<stdio.h>
#include<math.h>
#include <stdbool.h>
double Square( double n, double i, double j)
{
double mid = (i + j) / 2;
double mul = mid * mid;
if ((mul == n) || ( fabs (mul - n) < 0.00001))
return mid;
else if (mul < n)
return Square(n, mid, j);
else
return Square(n, i, mid);
}
void findSqrt( double n)
{
double i = 1;
bool found = false ;
while (!found) {
if (i * i == n) {
printf ( "%.0lf" ,i);
found = true ;
}
else if (i * i > n) {
double res = Square(n, i - 1, i);
printf ( "%.5lf" , res);
found = true ;
}
i++;
}
}
int main()
{
double n = 3;
findSqrt(n);
return 0;
}
|
C++
#include <bits/stdc++.h>
using namespace std;
double Square( double n, double i, double j)
{
double mid = (i + j) / 2;
double mul = mid * mid;
if ((mul == n) || ( abs (mul - n) < 0.00001))
return mid;
else if (mul < n)
return Square(n, mid, j);
else
return Square(n, i, mid);
}
void findSqrt( double n)
{
double i = 1;
bool found = false ;
while (!found) {
if (i * i == n) {
cout << fixed << setprecision(0) << i;
found = true ;
}
else if (i * i > n) {
double res = Square(n, i - 1, i);
cout << fixed << setprecision(5) << res;
found = true ;
}
i++;
}
}
int main()
{
double n = 3;
findSqrt(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static double Square( double n,
double i, double j)
{
double mid = (i + j) / 2 ;
double mul = mid * mid;
if ((mul == n) ||
(Math.abs(mul - n) < 0.00001 ))
return mid;
else if (mul < n)
return Square(n, mid, j);
else
return Square(n, i, mid);
}
static void findSqrt( double n)
{
double i = 1 ;
boolean found = false ;
while (!found)
{
if (i * i == n)
{
System.out.println(i);
found = true ;
}
else if (i * i > n)
{
double res = Square(n, i - 1 , i);
System.out.printf( "%.5f" , res);
found = true ;
}
i++;
}
}
public static void main(String[] args)
{
double n = 3 ;
findSqrt(n);
}
}
|
Python3
import math
def Square(n, i, j):
mid = (i + j) / 2 ;
mul = mid * mid;
if ((mul = = n) or ( abs (mul - n) < 0.00001 )):
return mid;
elif (mul < n):
return Square(n, mid, j);
else :
return Square(n, i, mid);
def findSqrt(n):
i = 1 ;
found = False ;
while (found = = False ):
if (i * i = = n):
print (i);
found = True ;
elif (i * i > n):
res = Square(n, i - 1 , i);
print ( "{0:.5f}" . format (res))
found = True
i + = 1 ;
if __name__ = = '__main__' :
n = 3 ;
findSqrt(n);
|
C#
using System;
class GFG
{
static double Square( double n,
double i, double j)
{
double mid = (i + j) / 2;
double mul = mid * mid;
if ((mul == n) ||
(Math.Abs(mul - n) < 0.00001))
return mid;
else if (mul < n)
return Square(n, mid, j);
else
return Square(n, i, mid);
}
static void findSqrt( double n)
{
double i = 1;
Boolean found = false ;
while (!found)
{
if (i * i == n)
{
Console.WriteLine(i);
found = true ;
}
else if (i * i > n)
{
double res = Square(n, i - 1, i);
Console.Write( "{0:F5}" , res);
found = true ;
}
i++;
}
}
public static void Main(String[] args)
{
double n = 3;
findSqrt(n);
}
}
|
Javascript
<script>
function Square(n, i, j)
{
var mid = ((i + j) / 2);
var mul = mid * mid;
if ((mul == n) || (Math.abs(mul - n) < 0.00001))
return mid;
else if (mul < n)
return Square(n, mid, j);
else
return Square(n, i, mid);
}
function findSqrt(n)
{
var i = 1;
var found = false ;
while (!found)
{
if (i * i == n)
{
document.write(i);
found = true ;
}
else if (i * i > n)
{
var res = Square(n, i - 1, i);
document.write(res.toFixed(5));
found = true ;
}
i++;
}
}
var n = 3;
findSqrt(n);
</script>
|
Time complexity: O(log N) where N is the given integer.
Auxiliary space: O(log N) for recursive stack space.
Method 3: Using binary search
1. This method uses binary search to find the square root of a number.
2. It starts by initializing the search range from 1 to n. It then calculates the mid-point of the search range and checks if the square of the mid-point is equal to the number we want to find the square root of.
3. If it is, the mid-point is the square root of the number. If not, the search range is updated based on whether the square of the mid-point is less than or greater than the number we want to find the square root of.
4. This process continues until we find the square root of the number.
C++
#include <iostream>
using namespace std;
int sqrt ( int n) {
if (n < 2) {
return n;
}
int low = 1, high = n;
while (low <= high) {
int mid = (low + high) / 2;
if (mid * mid == n) {
return mid;
} else if (mid * mid < n) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high;
}
int main() {
int n = 25;
cout << sqrt (n) << std::endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int sqrt( int n)
{
if (n < 2 ) {
return n;
}
int low = 1 , high = n;
while (low <= high) {
int mid = (low + high) / 2 ;
if (mid * mid == n) {
return mid;
}
else if (mid * mid < n) {
low = mid + 1 ;
}
else {
high = mid - 1 ;
}
}
return high;
}
public static void main(String[] args)
{
int n = 25 ;
System.out.println(sqrt(n));
}
}
|
Python3
def sqrt(n):
if n < 2 :
return n
low, high = 1 , n
while low < = high:
mid = (low + high) / / 2
if mid * mid = = n:
return mid
elif mid * mid < n:
low = mid + 1
else :
high = mid - 1
return high
n = 25
print (sqrt(n))
|
C#
using System;
class Program {
static int Sqrt( int n) {
if (n < 2) {
return n;
}
int low = 1, high = n;
while (low <= high) {
int mid = (low + high) / 2;
if (mid * mid == n) {
return mid;
} else if (mid * mid < n) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high;
}
static void Main( string [] args) {
int n = 25;
Console.WriteLine(Sqrt(n));
}
}
|
Javascript
function sqrt(n) {
if (n < 2) {
return n;
}
let low = 1, high = n;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (mid * mid === n) {
return mid;
} else if (mid * mid < n) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high;
}
let n = 25;
console.log(sqrt(n));
|
Time complexity:
The time complexity of this algorithm is O(log n) since it uses binary search to find the square root of a number.
Auxiliary space:
The algorithm does not use any extra space, so the space complexity is O(1).
Share your thoughts in the comments
Please Login to comment...