Files
ProjectEulerPython/Problem29.py

63 lines
2.3 KiB
Python

#ProjectEuler/Python/Problem29.py
#Matthew Ellison
# Created: 10-10-19
#Modified: 10-10-19
#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) 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 <https://www.gnu.org/licenses/>.
"""
from Stopwatch import Stopwatch
#Setup the variables
__BOTTOM_A = 2 #The lowest possible value for A
__TOP_A = 100 #The highest possible value for A
__BOTTOM_B = 2 #The lowest possible value for B
__TOP_B = 100 #The highest possible value for B
def Problem29():
unique = [] #This will hold all of the unique answers
#Start with the first A and move towards the top
for currentA in range(__BOTTOM_A, __TOP_A + 1):
#Start with the first B and move towards the top
for currentB in range(__BOTTOM_B, __TOP_B + 1):
#Get the new number
currentNum = currentA ** currentB
#If the new number isn't in the list add it
if currentNum not in unique:
unique.append(currentNum)
#Print the results
print("The number of unique values generated by a^b for " + str(__BOTTOM_A) + " <= a < = " + str(__TOP_A) + " and " + str(__BOTTOM_B) + " <= b <= " + str(__TOP_B) + " is " + str(len(unique)))
#This calls the appropriate functions if the script is called stand alone
if __name__ == "__main__":
timer = Stopwatch()
timer.start()
Problem29()
timer.stop()
print("It took " + timer.getString() + " to run 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
"""