#ProjectEuler/Python/Problem29.py #Matthew Ellison # Created: 10-10-19 #Modified: 10-30-20 #How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100? #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 Unsolved import Unsolved class Problem29(Problem): #Variables __bottomA = 2 #The lowest possible value for A __topA = 100 #The highest possible value for A __bottomB = 2 #The lowest possible value for B __topB = 100 #The highest possible value for B #Functions #Constructor def __init__(self): super().__init__("How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?") self.unique = [] #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 with the first A and move towards the top for currentA in range(self.__bottomA, self.__topA + 1): #Start with the first B and move towards the top for currentB in range(self.__bottomB, self.__topB + 1): #Get the new number currentNum = currentA ** currentB #If the new number isn't in the list add it if currentNum not in self.unique: self.unique.append(currentNum) #Stop the timer self.timer.stop() #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.unique.clear() #Gets #Returns the result of solving the problem def getResult(self): #If the problem hasn't been solved throw an exception if(not self.solved): raise Unsolved("You must solve the problem before you can see the result") return f"The number of unique values generated by a^b for {self.__bottomA} <= a < = {self.__topA} and {self.__bottomB} <= b <= {self.__topB} is {len(self.unique)}" #Returns the lowest possible value for a def getBottomA(self): #If the problem hasn't been solved throw an exception if(not self.solved): raise Unsolved("You must solve the problem before can you see the lowest possible A") return self.__bottomA #Returns the lowest possible value for a def getTopA(self): #If the problem hasn't been solved throw an exception if(not self.solved): raise Unsolved("You must solve the problem before can you see the highest possible A") return self.__topA #Returns the lowest possible value for a def getBottomB(self): #If the problem hasn't been solved throw an exception if(not self.solved): raise Unsolved("You must solve the problem before can you see the lowest possible B") return self.__bottomB #Returns the lowest possible value for a def getTopB(self): #If the problem hasn't been solved throw an exception if(not self.solved): raise Unsolved("You must solve the problem before can you see the highest possible B") return self.__topB #Returns a list of all unique values for a^b def getUnique(self) -> list: #If the problem hasn't been solved throw an exception if(not self.solved): raise Unsolved("You must solve the problem before can you see list of unique values") return self.unique """ Results: The number of unique values generated by a^b for 2 <= a < = 100 and 2 <= b <= 100 is 9183 It took an average of 304.306 milliseconds to run this problem through 100 iterations """