Square root of a number without using sqrt() function
Given a number N, the task is to find the square root of N without using sqrt() function.
Examples:
Input: N = 25
Output: 5Input: N = 3
Output: 1.73205Input: N = 2.5
Output: 1.58114
Approach:
- 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++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// Recursive function that returns square root// of a number with precision upto 5 decimal placesdouble Square(double n, double i, double j){ double mid = (i + j) / 2; double mul = mid * mid; // If mid itself is the square root, // return mid if ((mul == n) || (abs(mul - n) < 0.00001)) return mid; // If mul is less than n, recur second half else if (mul < n) return Square(n, mid, j); // Else recur first half else return Square(n, i, mid);}// Function to find the square root of nvoid findSqrt(double n){ double i = 1; // While the square root is not found bool found = false; while (!found) { // If n is a perfect square if (i * i == n) { cout << fixed << setprecision(0) << i; found = true; } else if (i * i > n) { // Square root will lie in the // interval i-1 and i double res = Square(n, i - 1, i); cout << fixed << setprecision(5) << res; found = true; } i++; }}// Driver codeint main(){ double n = 3; findSqrt(n); return 0;} |
Java
// Java implementation of the approachimport java.util.*;class GFG{// Recursive function that returns// square root of a number with// precision upto 5 decimal placesstatic double Square(double n, double i, double j){ double mid = (i + j) / 2; double mul = mid * mid; // If mid itself is the square root, // return mid if ((mul == n) || (Math.abs(mul - n) < 0.00001)) return mid; // If mul is less than n, // recur second half else if (mul < n) return Square(n, mid, j); // Else recur first half else return Square(n, i, mid);}// Function to find the square root of nstatic void findSqrt(double n){ double i = 1; // While the square root is not found boolean found = false; while (!found) { // If n is a perfect square if (i * i == n) { System.out.println(i); found = true; } else if (i * i > n) { // Square root will lie in the // interval i-1 and i double res = Square(n, i - 1, i); System.out.printf("%.5f", res); found = true; } i++; }}// Driver codepublic static void main(String[] args){ double n = 3; findSqrt(n);}}// This code is contributed by PrinciRaj1992 |
Python3
# Python3 implementation of the approachimport math# Recursive function that returns square root# of a number with precision upto 5 decimal placesdef Square(n, i, j): mid = (i + j) / 2; mul = mid * mid; # If mid itself is the square root, # return mid if ((mul == n) or (abs(mul - n) < 0.00001)): return mid; # If mul is less than n, recur second half elif (mul < n): return Square(n, mid, j); # Else recur first half else: return Square(n, i, mid);# Function to find the square root of ndef findSqrt(n): i = 1; # While the square root is not found found = False; while (found == False): # If n is a perfect square if (i * i == n): print(i); found = True; elif (i * i > n): # Square root will lie in the # interval i-1 and i res = Square(n, i - 1, i); print ("{0:.5f}".format(res)) found = True i += 1;# Driver codeif __name__ == '__main__': n = 3; findSqrt(n);# This code is contributed by 29AjayKumar |
C#
// C# implementation of the approachusing System; class GFG{// Recursive function that returns// square root of a number with// precision upto 5 decimal placesstatic double Square(double n, double i, double j){ double mid = (i + j) / 2; double mul = mid * mid; // If mid itself is the square root, // return mid if ((mul == n) || (Math.Abs(mul - n) < 0.00001)) return mid; // If mul is less than n, // recur second half else if (mul < n) return Square(n, mid, j); // Else recur first half else return Square(n, i, mid);}// Function to find the square root of nstatic void findSqrt(double n){ double i = 1; // While the square root is not found Boolean found = false; while (!found) { // If n is a perfect square if (i * i == n) { Console.WriteLine(i); found = true; } else if (i * i > n) { // Square root will lie in the // interval i-1 and i double res = Square(n, i - 1, i); Console.Write("{0:F5}", res); found = true; } i++; }}// Driver codepublic static void Main(String[] args){ double n = 3; findSqrt(n);}}// This code is contributed by Princi Singh |
Javascript
<script>// Javascript implementation of the approach// Recursive function that returns// square root of a number with// precision upto 5 decimal placesfunction Square(n, i, j){ var mid = ((i + j) / 2); var mul = mid * mid; // If mid itself is the square root, // return mid if ((mul == n) || (Math.abs(mul - n) < 0.00001)) return mid; // If mul is less than n, // recur second half else if (mul < n) return Square(n, mid, j); // Else recur first half else return Square(n, i, mid);}// Function to find the square root of nfunction findSqrt(n){ var i = 1; // While the square root is not found var found = false; while (!found) { // If n is a perfect square if (i * i == n) { document.write(i); found = true; } else if (i * i > n) { // Square root will lie in the // interval i-1 and i var res = Square(n, i - 1, i); document.write(res.toFixed(5)); found = true; } i++; }}// Driver codevar n = 3;findSqrt(n);// This code is contributed by todaysgaurav</script> |
Output:
1.73205


