Files
ProjectEulerPython/Problems/Problem29.py

122 lines
4.3 KiB
Python

#ProjectEuler/Python/Problem29.py
#Matthew Ellison
# Created: 10-10-19
#Modified: 07-19-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 <https://www.gnu.org/licenses/>.
"""
from Problems.Problem import Problem
from Stopwatch import Stopwatch
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()
#Print the results
self.result = "The number of unique values generated by a^b for " + str(self.__bottomA) + " <= a < = " + str(self.__topA) + " and " + str(self.__bottomB) + " <= b <= " + str(self.__topB) + " is " + str(len(self.unique))
#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 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
#This calls the appropriate functions if the script is called stand alone
if __name__ == "__main__":
problem = Problem29()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The number of unique values generated by a^b for 2 <= a < = 100 and 2 <= b <= 100 is 9183
It took 304.630 milliseconds to run this algorithm
"""