princeofsudan

ProjectEuler#39 Integer right triangles

Apr 5th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.95 KB | None | 0 0
  1. #If p is the perimeter of a right angle triangle with integral length sides,
  2. #{a,b,c}, there are exactly three solutions for p = 120.
  3.  
  4. #{20,48,52}, {24,45,51}, {30,40,50}
  5.  
  6. #For which value of p <= 1000, is the number of solutions maximised?
  7.  
  8. #Characteristics:
  9. #a<=b<c
  10. #c**2 = a**2 + b**2 (Pythagoras)
  11. #smallest values a=3, b=4, c=5
  12.  
  13. #iterate over the maximum perimetr to get all the solutions and store in
  14. #a dict where solution is the key and the perimeter is the value
  15. maxP = 1000
  16. solutions = {}
  17. for a in range(3,maxP+1):
  18.     for b in range(a,maxP+1):
  19.         if a+b > maxP:
  20.             break
  21.         for c in range(b+1,maxP+1):
  22.             if a+b+c > maxP:
  23.                 break
  24.             if  c**2 == a**2 + b**2:
  25.                 solutions[(a,b,c)] = a+b+c
  26.                
  27.    
  28. pList = solutions.values() #Generate a list of all values in solutions dict
  29.  
  30. print max(set(pList), key=pList.count) #Get the most frequent value in pList
Advertisement
Add Comment
Please, Sign In to add comment