Last active
March 31, 2017 14:53
-
-
Save nytr0gen/b9576abbbb578b4422c910d50166ec32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bin_sqrt(v): | |
lo, hi = 0, v | |
while (hi - lo) > 1: | |
mid = (hi + lo) // 2 | |
sqrmid = mid * mid | |
if sqrmid < v: | |
lo = mid | |
elif sqrmid > v: | |
hi = mid | |
else: | |
return mid | |
return (hi + lo) // 2 | |
def sqrt2(v): | |
return [x for x in range(0, v // 2 + 1) if x * x >= v][0] | |
def sqrt3(v): | |
return list(filter(lambda x: x*x >= v, range(0, v//2 + 1)))[0] | |
def sqrt4(v): | |
return int(v ** 0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment