princeofsudan

ProjectEuler#9 Special Pythagorean triplet

Mar 2nd, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. #A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
  2. #a**2 + b**2 = c**2
  3.  
  4. #For example, 3**2 + 4**2 = 9 + 16 = 25 = 52.
  5.  
  6. #There exists exactly one Pythagorean triplet for which a + b + c = 1000.
  7. #Find the product abc.
  8.  
  9. #Analysis
  10. # loop over a and b and then check whether a**2 + b**2 == (s - a - b)**2
  11. # From the condition a < b < c, we conclude that a<= (s-3)/3) and b < (s-a)/2)
  12.  
  13. s = 1000
  14. for a in range(3,int((s-3)/3)+1):
  15.     for b in range(a+1,int((s-a)/2)+1):
  16.         c = s - a - b
  17.         if c**2 == a**2 + b**2:
  18.             print a,b,c
  19. print a*b*c
Advertisement
Add Comment
Please, Sign In to add comment