Updated all problems to use fstrings and results()

This commit is contained in:
2020-10-30 16:20:39 -04:00
parent ad458278d4
commit 46216a2807
33 changed files with 280 additions and 452 deletions

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem12.py
#Matthew Ellison
# Created: 01-31-19
#Modified: 07-18-20
#Modified: 10-30-20
#What is the value of the first triangle number to have over five hundred divisors?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
from Algorithms import getDivisors
@@ -63,14 +62,9 @@ class Problem12(Problem):
#Stop the timer
self.timer.stop()
#Save the results
if(self.sum <= 0):
self.result = "There was an error. Could not find a triangular number with " + str(self.__goalDivisors) + " divisors before overflow"
else:
self.result = "The first triangular number with more than " + str(self.__goalDivisors) + " divisors is " + str(self.sum)
#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()
@@ -79,6 +73,12 @@ class Problem12(Problem):
self.divisors.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 first triangular number with more than {self.__goalDivisors} divisors is {self.sum}"
#Returns the triangular number
def getTriangularNumber(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -104,14 +104,6 @@ class Problem12(Problem):
raise Unsolved("You must solve the problem before you can get the number of divisors")
return len(self.divisors)
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem12()
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 first triangular number with more than 500 divisors is 76576500