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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Square root of a number using log
- C program to find square root of a given number
- Find square root of number upto given precision using binary search
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Square root of an integer
- Find Cube root of a number using Log function
- Program to calculate Root Mean Square
- Babylonian method for square root
- Fast inverse square root
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Euler's criterion (Check if square root under modulo p exists)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Long Division Method to find Square root with Examples
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Find smallest perfect square number A such that N + A is also a perfect square number
- N-th root of a number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.