Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions code/online_challenges/src/leetcode/Sqrt(x)/Sqrt(x).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Part of Cosmos by OpenGenus Foundation

def sqrt(x: int) -> int:

"""
Returns the integer square root of x.
Example: print(sqrt(16)) => 4
"""

if x < 2:
return x

left, right = 1, x // 2

while left <= right:

mid = (left + right) // 2

if mid * mid == x:
return mid

elif mid * mid < x:
left = mid + 1

else:
right = mid - 1

return right