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 @@
#Project Euler/Python/Problem10.py
#Matthew Ellison
# Created: 01-30-19
#Modified: 07-18-20
#Modified: 10-30-20
#Find the sum of all the primes below two million
#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 getPrimes
@@ -56,17 +55,21 @@ class Problem10(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The sum of all the prime numbers less than " + str(self.__numberGreaterThanPrimes + 1) + " 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()
self.sum = 0
#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 sum of all the prime numbers less than {self.__numberGreaterThanPrimes + 1} is {self.sum}"
#Returns the sum that was requested
def getSum(self) -> int:
#If the problem hasn't been solved throw an exception
@@ -75,15 +78,6 @@ class Problem10(Problem):
return self.sum
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem10()
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 sum of all the prime numbers less than 2000000 is 142913828922
It took an average of 7.187 seconds to run this problem through 100 iterations