My solutions to the problems found at Project Euler.

Jump to: | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 20 | 21 | 22 | 25 | 3-2 |

Problem 9


#! /usr/bin/python
# Problem: Find the pythagorean triplet for which a + b + c = 1000, knowing that
#          a*a + b*b = c*c.
#
# Approach: Generate pythagorean triplets until the answer is found. 
#           This should happen pretty quickly.

if __name__ == "__main__":
  triplets = []
  
  test_a = 1
  test_b = 1
  counter = 0
  found_solution = False
  
  while found_solution == False:
    triplets.append(((test_a),(test_b),((test_a**2)+(test_b**2))**0.5))
    
    if test_b >= 1000:
      test_a += 1
      test_b = 1
    else:
      test_b += 1
          
    if triplets[counter][0] + triplets[counter][1] \
       + triplets[counter][2] == 1000:
       print 'Soultion to triplet where a + b + c == 1000 found:'
       print '\ta:' + str(triplets[counter][0])
       print '\tb:' + str(triplets[counter][1])
       print '\tc:' + str(int(triplets[counter][2]))
       print 'Product of triplet: ' + str(int( triplets[counter][0] *  
                                               triplets[counter][1] * 
                                               triplets[counter][2] ))
       
       found_solution = True
       
       
    counter += 1

jb