#ProjectEuler/Python/Problem4.py #Matthew Ellison # Created: 01-28-19 #Modified: 03-28-19 #Find the largest palindrome made from the product of two 3-digit numbers #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 __lowestNum = 100 __highestNum = 1000 def Problem4(): #Setup the variables palindromes = [] #Holds all of the palindromes currentNum = 0 #Holds the product of the two numbers I am currently working on #Loop through every number from __lowestNum to __highestNum twice and multiply every number together for num1 in range(__lowestNum, __highestNum + 1): for num2 in range(num1, __highestNum + 1): #You can start at num1 because 100 * 101 == 101 * 100 currentNum = num1 * num2 #If the number is a palindrome add it to the list of palindromes, otherwise ignore it #Using strings makes it easier to determine a palindrome if(str(currentNum) == str(currentNum)[::-1]): palindromes.append(currentNum) #Sort the palindromes so that the last element is the largest palindromes.sort() #Print the results print("The largest palindrome made from the product of two 3-digit numbers is " + str(palindromes[len(palindromes) - 1])) #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 Problem4() #Call the function that answers the question timer.stop() #Stop the timer #Print the results of the timer print("It took " + timer.getString() + " to run this algorithm") """Results: The largest palindrome made from the product of two 3-digit numbers is 906609 It took 177.314 milliseconds to run this algorithm """