Skip to content

Instantly share code, notes, and snippets.

@nytr0gen
Last active March 31, 2017 14:53
Show Gist options
  • Save nytr0gen/b9576abbbb578b4422c910d50166ec32 to your computer and use it in GitHub Desktop.
Save nytr0gen/b9576abbbb578b4422c910d50166ec32 to your computer and use it in GitHub Desktop.
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