#ProjectEuler/Python/Problem3.py #Matthew Ellison # Created: 01-27-19 #Modified: 07-17-20 #The largest prime factor of 600851475143 #Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses """ Copyright (C) 2020 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 Problems.Problem import Problem from Stopwatch import Stopwatch from Algorithms import getFactors from Unsolved import Unsolved class Problem3(Problem): #Variables __goalNumber = 600851475143 #Functions #Constructor def __init__(self): super().__init__("What is the largest prime factor of 600851475143?") self.factors = [] #Operational functions #Solve the problem def solve(self): #If the problem has already been solved do nothing and end the function if(self.solved): return #Start the timer self.timer.start() #Get the factors of the number self.factors = getFactors(self.__goalNumber) #The last element should be the largest factor #Stop the timer self.timer.stop() #Save the results self.result = "The largest prime factor of " + str(self.__goalNumber) + " is " + str(self.factors[(len(self.factors) - 1)]) #Throw a flag to show the problem is solved self.solved = True #Reset the problem so it can be run again def reset(self): super().reset() self.factors.clear() #Gets #Returns the list of factors of the number def getFactors(self) -> list: #If the problem hasn't been solved throw an exceptions if(not self.solved): raise Unsolved("You must solve the problem before you can get the factors") return self.factors #Returns the largest factor of the number def getLargestFactor(self) -> int: #If the problem hasn't been solved throw an exceptions if(not self.solved): raise Unsolved("You must solve the problem before you can get the largest factor") return self.factors[(len(self.factors) - 1)] #Returns the number for which we are getting the factor def getGoalNumber(self) -> int: return self.__goalNumber #If you are running this file, automatically start the correct function if __name__ == '__main__': problem = Problem3() print(problem.getDescription()) #Print the description of the problem problem.solve() #Solve the problem #Print the results print(problem.getResult()) print("It took " + problem.getTime() + " to solve this algorithm") """Results: The largest prime factor of 600851475143 is 6857 It took 1.685 seconds to run this algorithm """