fork download
  1. import math
  2.  
  3.  
  4. def find(n):
  5. # max of x can be sqrt of n
  6. n1 = int(math.ceil(math.sqrt(n)))
  7. lst = set()
  8. for i in range(1, n1):
  9. for j in range(i + 1, n1):
  10.  
  11. if i**2 + j**2 >= n:
  12. break
  13.  
  14. tz = int(math.sqrt(n - i**2 - j**2))
  15. if tz ** 2 == n - i**2 - j**2:
  16.  
  17. #This if-block makes the elements in tuple, sorted.
  18. #so that set can compare two values for equality
  19. #because (1, 2, 3) != (2, 3, 1)
  20. if tz < i:
  21. i, j, tz = tz, i, j
  22. elif tz < j:
  23. j, tz = tz, j
  24.  
  25. a = (i, j, tz)
  26. lst.add(a)
  27. return lst
  28.  
  29. print find(2134)
  30.  
Success #stdin #stdout 0.04s 64684KB
stdin
Standard input is empty
stdout
set([(3, 10, 45), (3, 19, 42), (3, 30, 35), (3, 3, 46), (6, 27, 37), (9, 17, 42), (17, 18, 39), (18, 21, 37), (26, 27, 27)])