Floor square root without using sqrt() function : Recursive
Last Updated :
28 Dec, 2021
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: 5
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: 5
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:
- Iterate a loop from numbers 1 to N in K.
- For any K, if its square becomes greater than N, then K-1 is the floor square root of N.
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:
- Implement the Binary Search in the range 0 to N.
- Find the mid value of the range using formula:
mid = (start + end) / 2
- Base Case: The recursive call will get executed till square of mid is less than or equal to N and the square of (mid+1) is greater than equal to N.
(mid2 ? N) and ((mid + 1)2 > N)
- If the base case is not satisfied, then the range will get changed accordingly.
- If the square of mid is less than equal to N, then the range gets updated to [mid + 1, end]
if(mid2 ? N)
updated range = [mid + 1, end]
- If the square of mid is greater than N, then the range gets updated to [low, mid + 1]
if(mid2 > N)
updated range = [low, mid - 1]
Below is the implementation of the above approach:
C++
// 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
// 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
# 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#
// 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
JavaScript
<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>
Performance Analysis:
- Time Complexity: As in the above approach, there is Binary Search used over the search space of 0 to N which takes O(log N) time in worst case, Hence the Time Complexity will be O(log N).
- Space Complexity: As in the above approach, taking consideration of the stack space used in the recursive calls which can take O(logN) space in worst case, Hence the space complexity will be O(log N)
Similar Reads
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: 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 => y
14 min read
Print squares of first n natural numbers without using *, / and -
Given a natural number 'n', print squares of first n natural numbers without using *, / and -. Examples : Input: n = 5Output: 0 1 4 9 16Input: n = 6Output: 0 1 4 9 16 25We strongly recommend to minimize the browser and try this yourself first.Method 1: The idea is to calculate next square using prev
8 min read
Floor value Kth root of a number using Recursive Binary Search
Given two numbers N and K, the task is to find the floor value of Kth root of the number N. The Floor Kth root of a number N is the greatest whole number which is less than or equal to its Kth root.Examples: Input: N = 27, K = 3 Output: 3 Explanation: Kth root of 27 = 3. Therefore 3 is the greatest
9 min read
Calculate square of a number without using *, / and pow()
Given an integer n, calculate the square of a number without using *, / and pow(). Examples : Input: n = 5 Output: 25 Input: 7 Output: 49 Input: n = 12 Output: 144 A Simple Solution is to repeatedly add n to result. Below is the implementation of this idea. C++ // Simple solution to calculate square
15 min read
Check if a number is perfect square without finding square root
Check whether a number is a perfect square or not without finding its square root. Examples: Input: n = 36 Output: Yes Input: n = 12 Output: No Recommended PracticeCheck perfect squareTry It! Method 1:The idea is to run a loop from i = 1 to floor(sqrt(n)) and then check if squaring it makes n. Below
9 min read
Square root of a number using log
For a given number find the square root using log function. Number may be int, float or double. Examples: Input : n = 9Output : 3 Input : n = 2.93Output : 1.711724 We can find square root of a number using sqrt() method: C++ // C++ program to demonstrate finding // square root of a number using sqrt
3 min read
Square Root (Sqrt) Decomposition Algorithm
Square Root Decomposition Technique is one of the most common query optimization techniques used by competitive programmers. This technique helps us to reduce Time Complexity by a factor of sqrt(N) The key concept of this technique is to decompose a given array into small chunks specifically of size
15+ min read
Decimal to Binary using recursion and without using power operator
Given an integer N, the task is convert and print the binary equaiva;ent of N.Examples: Input: N = 13 Output: 1101Input: N = 15 Output: 1111 Approach Write a recursive function that takes an argument N and recursively calls itself with the value N / 2 as the new argument and prints N % 2 after the c
3 min read
Find square root of number upto given precision using binary search
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 = 3Output : 7.071Input : number = 10, precision = 4Output : 3.1622 We have discussed how to compute the in
8 min read
Find square root of a number using Bit Manipulation
Given a non-negative integer N, the task is to find the square root of N using bitwise operations. If the integer is not the perfect square, return largest integer that is smaller than or equal to square root of N i.e., floor(âN). Examples: Input: N = 36Output: 6Explanation: The square root of 36 is
7 min read