mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Updated to use new library layout
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem30.py
|
||||
#Matthew Ellison
|
||||
# Created: 10-28-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.
|
||||
#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
|
||||
Copyright (C) 2021 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
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
|
||||
class Problem30(Problem):
|
||||
@@ -34,14 +33,14 @@ class Problem30(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.")
|
||||
self.sumOfFifthNumbers = [] #This is an ArrayList of the numbers that are the sum of the fifth power of their digits
|
||||
self.sum = 0 #This is the sum of the sumOfFifthNumbers list
|
||||
|
||||
#Operational function
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -49,6 +48,7 @@ class Problem30(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Start with the lowest number and increment until you reach the largest number
|
||||
for currentNum in range(self.__bottomNum, self.__topNum):
|
||||
#Get the digits of the number
|
||||
@@ -62,6 +62,7 @@ class Problem30(Problem):
|
||||
if(sumOfPowers == currentNum):
|
||||
self.sumOfFifthNumbers.append(currentNum)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -80,46 +81,29 @@ class Problem30(Problem):
|
||||
return listOfDigits
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.sumOfFifthNumbers.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")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("result")
|
||||
return f"The sum of all the numbers that can be written as the sum of the fifth powers of their digits is {sum(self.sumOfFifthNumbers)}"
|
||||
#Returns the top number to be checked
|
||||
def getTopNum(self) -> int:
|
||||
#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 top number")
|
||||
self.solvedCheck("largest number checked")
|
||||
return self.__topNum
|
||||
#Returns a copy of the vector holding all the number that are the sum of the fifth powers of their digits
|
||||
def getListOfSumsOfFifths(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 the list")
|
||||
self.solvedCheck("list of all numbers that are the sum of the 5th power of their digits")
|
||||
return self.sumOfFifthNumbers
|
||||
#Returns the sum of all entries in sumOfFifthNumbers
|
||||
def getSumOfList(self) -> int:
|
||||
#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 sum of the list")
|
||||
self.solvedCheck("sum of all numbers that are the sum of the 5th power of their digits")
|
||||
return sum(self.sumOfFifthNumbers)
|
||||
|
||||
|
||||
#This calls the appropriate functions if the script is called stand alone
|
||||
if __name__ == "__main__":
|
||||
problem = Problem30()
|
||||
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 sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839
|
||||
It took an average of 4.068 seconds to run this problem through 100 iterations
|
||||
|
||||
Reference in New Issue
Block a user