mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-30 12:46:11 -04:00
28 lines
545 B
Python
28 lines
545 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# author: bt3gl
|
|
|
|
|
|
def sqrt(x) -> int:
|
|
|
|
if x < 2:
|
|
return x
|
|
|
|
left, right = 2, x // 2
|
|
|
|
while left <= right:
|
|
|
|
pivot = left + (right - left) // 2
|
|
num = pivot * pivot
|
|
|
|
if num > x:
|
|
right = pivot - 1
|
|
|
|
elif num < x:
|
|
left = pivot + 1
|
|
|
|
else:
|
|
return pivot
|
|
|
|
return right
|