Open In App

Floor square root without using sqrt() function : Recursive

Given a number N, the task is to find the floor square root of the number N without using the built-in square root function. Floor square root of a number is the greatest whole number which is less than or equal to its square root.

Examples:  

Input: N = 25 
Output:
Explanation: 
Square root of 25 = 5. Therefore 5 is the greatest whole number less than equal to Square root of 25. 

Input: N = 30 
Output:
Explanation: 
Square root of 30 = 5.47 
Therefore 5 is the greatest whole number less than equal to Square root of 30(5.47) 
 

Naive Approach: 
In the basic approach to find the floor square root of a number, find the square of numbers from 1 to N, till the square of some number K becomes greater than N. Hence the value of (K – 1) will be the floor square root of N.

Below is the algorithm to solve this problem using Naive approach: 

Time Complexity: O(?N)

Efficient Approach: 
From the Naive approach, it is clear that the floor square root of N will lie in the range [1, N]. Hence instead of checking each number in this range, we can efficiently search the required number in this range. Therefore, the idea is to use the binary search in order to efficiently find the floor square root of the number N in log N.

Below is the recursive algorithm to solve the above problem using Binary Search: 

mid = (start + end) / 2
(mid2 ? N) and ((mid + 1)2 > N)
if(mid2 ? N)
    updated range = [mid + 1, end]
if(mid2 > N)
    updated range = [low, mid - 1]

Below is the implementation of the above approach: 




// C++ implementation to find the
// square root of the number N
// without using sqrt() function
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the square
// root of the number N using BS
int sqrtSearch(int low, int high, int N)
{
 
    // If the range is still valid
    if (low <= high) {
 
        // Find the mid-value of the range
        int mid = (low + high) / 2;
 
        // Base Case
        if ((mid * mid <= N)
            && ((mid + 1) * (mid + 1) > N)) {
            return mid;
        }
 
        // Condition to check if the
        // left search space is useless
        else if (mid * mid < N) {
            return sqrtSearch(mid + 1, high, N);
        }
        else {
            return sqrtSearch(low, mid - 1, N);
        }
    }
    return low;
}
 
// Driver Code
int main()
{
    int N = 25;
    cout << sqrtSearch(0, N, N)
         << endl;
    return 0;
}




// Java implementation to find the
// square root of the number N
// without using sqrt() function
class GFG {
     
    // Function to find the square
    // root of the number N using BS
    static int sqrtSearch(int low, int high, int N)
    {
     
        // If the range is still valid
        if (low <= high) {
     
            // Find the mid-value of the range
            int mid = (int)(low + high) / 2;
     
            // Base Case
            if ((mid * mid <= N)
                && ((mid + 1) * (mid + 1) > N)) {
                return mid;
            }
     
            // Condition to check if the
            // left search space is useless
            else if (mid * mid < N) {
                return sqrtSearch(mid + 1, high, N);
            }
            else {
                return sqrtSearch(low, mid - 1, N);
            }
        }
        return low;
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int N = 25;
        System.out.println(sqrtSearch(0, N, N));
    }
}
 
// This code is contributed by Yash_R




# Python3 implementation to find the
# square root of the number N
# without using sqrt() function
 
# Function to find the square
# root of the number N using BS
def sqrtSearch(low, high, N) :
 
    # If the range is still valid
    if (low <= high) :
 
        # Find the mid-value of the range
        mid = (low + high) // 2;
 
        # Base Case
        if ((mid * mid <= N) and ((mid + 1) * (mid + 1) > N)) :
            return mid;
 
        # Condition to check if the
        # left search space is useless
        elif (mid * mid < N) :
            return sqrtSearch(mid + 1, high, N);
     
        else :
            return sqrtSearch(low, mid - 1, N);
 
    return low;
 
# Driver Code
if __name__ == "__main__" :
 
    N = 25;
    print(sqrtSearch(0, N, N))
 
# This code is contributed by Yash_R




// C# implementation to find the
// square root of the number N
// without using sqrt() function
using System;
 
class GFG {
      
    // Function to find the square
    // root of the number N using BS
    static int sqrtSearch(int low, int high, int N)
    {
      
        // If the range is still valid
        if (low <= high) {
      
            // Find the mid-value of the range
            int mid = (int)(low + high) / 2;
      
            // Base Case
            if ((mid * mid <= N)
                && ((mid + 1) * (mid + 1) > N)) {
                return mid;
            }
      
            // Condition to check if the
            // left search space is useless
            else if (mid * mid < N) {
                return sqrtSearch(mid + 1, high, N);
            }
            else {
                return sqrtSearch(low, mid - 1, N);
            }
        }
        return low;
    }
      
    // Driver Code
    public static void Main(String[] args)
    {
        int N = 25;
        Console.WriteLine(sqrtSearch(0, N, N));
    }
}
 
// This code is contributed by PrinciRaj1992




<script>
 
// Javascript implementation to find the
// square root of the number N
// without using sqrt() function
 
// Function to find the square
// root of the number N using BS
function sqrtSearch(low, high, N)
{
     
    // If the range is still valid
    if (low <= high)
    {
         
        // Find the mid-value of the range
        var mid = parseInt( (low + high) / 2);
 
        // Base Case
        if ((mid * mid <= N) &&
           ((mid + 1) * (mid + 1) > N))
        {
            return mid;
        }
 
        // Condition to check if the
        // left search space is useless
        else if (mid * mid < N)
        {
            return sqrtSearch(mid + 1, high, N);
        }
        else
        {
            return sqrtSearch(low, mid - 1, N);
        }
    }
    return low;
}
 
// Driver Code
var N = 25;
 
document.write(sqrtSearch(0, N, N));
 
// This code is contributed by todaysgaurav
 
</script>

Output: 
5

 

Performance Analysis: 

 


Article Tags :