#ProjectEulter/Python/Project5.py #Matthew Ellison # Created: 01-28-19 #Modified: 07-17-20 #What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? #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: it 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 itr 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. it 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 Unsolved import Unsolved class Problem5(Problem): #Variables __startNum = 1 __stopNum = 20 #Functions #Constructor def __init__(self): super().__init__("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?") self.smallestNum = 0 #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() #Start at 20 and loop through all numbers until it find one that works #It must be at least 20 to be divisible by 20 numFound = False #A flag for finding the divisible number currentNum = 20 #The number that it are currently checking against while((currentNum > 0) and (not numFound)): #Set that it found the number to true, because it sets this flag when it doesn't find it numFound = True #See if the current number is divisible by all numbers from 1 to 20 for divisor in range(self.__startNum, self.__stopNum + 1): #If it is not set a flag to move to the next possible number if((currentNum % divisor) != 0): numFound = False break #Increment the number by 2 to check the next one if it didn't find the number if(not numFound): currentNum += 2 self.smallestNum = currentNum #Stop the timer self.timer.stop() #Save the results if(currentNum < 0): self.result = "There was an error: Could not find a number that fit the criteria" else: self.result = "The smallest positive number that is evenly divisible by all numbers 1-20 is " + str(self.smallestNum) #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.smallestNum = 0 #Gets #Returns the requested number def getNumber(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 requested number") return self.smallestNum #If it are running this file, automatically start the correct function if __name__ == '__main__': problem = Problem5() 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 smallest positive number that is evenly divisible by all numbers 1-20 is 232792560 It took an average of 54.833 seconds to run this problem through 100 iterations """