- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Guess Nearest Square Root in Python
Suppose we have a non-negative number n, we have to find a number r such that r * r = n and we have to round down to the nearest integer. We have to solve this problem without using the builtin square-root function.
So, if the input is like 1025, then the output will be 32.
To solve this, we will follow these steps −
- if n <= 1, then
- return n
- start := 1, end := n
- while start < end, do
- mid := start + end/2
- if mid * mid <= n, then
- start := mid + 1
- end := mid
- return start - 1
Let us see the following implementation to get better understanding −
class Solution: def solve(self, n): if n <= 1: return n start, end = 1, n while start < end: mid = start + end >> 1 if mid * mid <= n: start = mid + 1 else: end = mid return start - 1 ob = Solution() print(ob.solve(1025))
- Square and Square root in Arduino
- 8086 program to find the square root of a perfect square root number
- Fast inverse square root in C++
- How to find Square root of complex numbers in Python?
- How to calculate square root of a number in Python?
- Compute the square root of input with emath in Python
- How to perform square root without using math module in Python?
- Compute the square root of negative input with emath in Python
- Compute the square root of complex inputs with scimath in Python
- Babylonian method to find the square root
- C++ Program to count square root cube root and both in given range
- Check if a number is perfect square without finding square root in C++
- 8085 program to find square root of a number
- 8086 program to find Square Root of a number