#Project Euler/Python/Problem9.py #Matthew Ellison # Created: 01-29-19 #Modified: 03-28-19 #There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc. #Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses """ Copyright (C) 2019 Matthew Ellison This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . """ from Stopwatch import Stopwatch import math def Problem9(): #Setup the variables foundTriplet = False sideA = 1 #Start with the lowest possible a , 1, and search for the b and c to complete the triplet while((sideA <= (1000 / 3)) and (not foundTriplet)): #Setup b and c sideB = sideA + 1 #b must be > a to be a triplet sideC = math.hypot(sideA, sideB) #C is the hyp #Loop through possible b's and calculate c's until you find the numbers or the sum gets too large while((sideA + sideB + sideC) < 1000): sideB += 1 sideC = math.hypot(sideA, sideB) #If c is an integer make it one if((sideC % 1) == 0): sideC = int(round(sideC)) #Check if the correct sides were found if((sideA + sideB + sideC) == 1000): foundTriplet = True #Otherwise increment a to the next possible number else: sideA += 1 #Print the results if(foundTriplet): print("The Pythagorean triplet where a + b + c = 1000 is " + str(sideA) + " " + str(sideB) + " " + str(int(sideC))) print("The product of those numbers is " + str(int(sideA * sideB * sideC))) else: print("Could not find the triplet where a + b + c = 1000") #If you are running this file, automatically start the correct function if __name__ == "__main__": timer = Stopwatch() #Determines the algorithm's run time timer.start() #Start the timer Problem9() #Call the function that answers the question timer.stop() #Start the timer #Print the results of the timer print("It took " + timer.getString() + " to run this algorithm") """Results: The Pythagorean triplet where a + b + c = 1000 is 200 375 425 The product of those numbers is 31875000 It took 22.106 milliseconds to run this algorithm """