Skip to content

Instantly share code, notes, and snippets.

@the5fire
Created July 3, 2017 11:21
Show Gist options
  • Save the5fire/485a7604d6247f97338ba067dac00417 to your computer and use it in GitHub Desktop.
Save the5fire/485a7604d6247f97338ba067dac00417 to your computer and use it in GitHub Desktop.
import time
def is_power(c):
m = int(c ** 0.5)
candidate_list = range(m, 0, -1)
for i in candidate_list:
pos = c - i ** 2
if pos < 0:
continue
other = pos ** .5
if other.is_integer():
return True
return False
def is_power2(c):
i = 0
j = int(c ** 0.5)
while i <= j:
r = i*i + j*j
if r < c:
i += 1
elif r > c:
j -= 1
else:
return True
return False
def is_power3(c):
for i in range(int(c ** 0.5) + 1):
if ((c - i ** 2) ** .5).is_integer():
return True
return False
if __name__ == '__main__':
num = 88777661
start = time.time()
print is_power(num), time.time() - start
start = time.time()
print is_power2(num), time.time() - start
start = time.time()
print is_power3(num), time.time() - start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment