Skip to content

Instantly share code, notes, and snippets.

@danlamanna
Created December 24, 2014 06:02
Show Gist options
  • Select an option

  • Save danlamanna/565f414e364ae803ebb8 to your computer and use it in GitHub Desktop.

Select an option

Save danlamanna/565f414e364ae803ebb8 to your computer and use it in GitHub Desktop.
n = 12427281805225 # 3525235
# brute force, ~.5s
for i in xrange(1, n):
if i**2 == n:
print i
break
# binary search.. ~.06s
def find_sq_root(n, lower=1, upper=n):
m = (lower + upper) / 2
if m**2 == n:
return m
if m**2 > n:
return find_sq_root(n, lower, m)
else:
return find_sq_root(n, m, upper)
print find_sq_root(n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment