39.py 615 B

12345678910111213141516171819202122232425
  1. def pythsolutions(p):
  2. """Return the number of valid Pythagorean triples with given perimeter (must be int)"""
  3. rightlist = []
  4. for x in range(1,p):
  5. y = x + 1
  6. z = y + 1
  7. while z <= p:
  8. while z**2 < x**2 + y**2:
  9. z += 1
  10. if z**2 == x**2 + y**2 and z <= p and x + y + z == p:
  11. rightlist.append([x, y, z])
  12. y += 1
  13. return len(rightlist)
  14. # Max number of solutions
  15. maxsolutions = 1
  16. # Perimeter for the max number of solutions
  17. bestpsolution = 1
  18. for i in range(1000):
  19. numsolutions = pythsolutions(i)
  20. if numsolutions > maxsolutions:
  21. bestpsolution = i
  22. maxsolutions = numsolutions
  23. print bestpsolution