Given a number N, the task is to find the square root of N without using sqrt() function.
Input: N = 25
Input: N = 3
Input: N = 2.5
- 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:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Floor square root without using sqrt() function : Recursive
- Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))
- Check if a number is perfect square without finding square root
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
- Square root of a number using log
- Find square root of number upto given precision using binary search
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- C program to find square root of a given number
- Square root of a number by Repeated Subtraction method
- Min operations to reduce N by multiplying by any number or taking square root
- Find smallest perfect square number A such that N + A is also a perfect square number
- Babylonian method for square root
- Square root of an integer
- Euler's criterion (Check if square root under modulo p exists)
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Fast inverse square root
- Program to calculate Root Mean Square
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.