Skip to content

Instantly share code, notes, and snippets.

@mallyvai
Created January 12, 2011 03:01
Show Gist options
  • Save mallyvai/775620 to your computer and use it in GitHub Desktop.
Save mallyvai/775620 to your computer and use it in GitHub Desktop.
Facebook Hacker Cup Qual Round Problem #1
from math import sqrt, floor, ceil
def compute(num):
upper_bound = int(ceil(sqrt(num)))
counter = 0
lower_bound = int(sqrt(floor(num/2)))
for i in range(lower_bound, upper_bound+1):
diff = num - i**2
if diff < 0:
continue
s = sqrt(diff)
if s == int(s):
counter += 1
return counter
lines = open("input.txt").readlines()
print(lines)
fh = open("temp.txt", 'w')
for line in lines[1:]:
fh.write(str(compute(int(line.strip())))+"\n")
fh.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment