Project Euler - Problem 9


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/python
  2.  
  3. # Project Euler - Problem 9
  4.  
  5. print "\nProject Euler - Problem 9"
  6. print "Find the Pythagorean triplet (a^2 + b^2 = c^2)"
  7. print "where a+b+c = 1000"
  8.  
  9. hasResult = False
  10.  
  11. for a in range(1,994):
  12. b = a + 1
  13. c = b + 1
  14. while (a+b+c) < 1000:
  15. b = b + 1
  16. c = b + 1 # c is always greater than b
  17. while (a+b+c) < 1000:
  18. c = c + 1
  19. if (c*c) == (a*a) + (b*b):
  20. hasResult = True
  21. else:
  22. c = b+1
  23. if hasResult:
  24. break
  25.  
  26. print "result: " + str(a*b*c)

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.