mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Updated all problems to use fstrings and results()
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#Project Eulter/Python/Problem7.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-29-19
|
||||
#Modified: 07-18-20
|
||||
#What is the 10001th prime number?
|
||||
#Modified: 10-30-20
|
||||
#What is the 10001st prime number?
|
||||
#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
|
||||
@@ -23,7 +23,6 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Stopwatch import Stopwatch
|
||||
from Unsolved import Unsolved
|
||||
from Algorithms import getNumPrimes
|
||||
|
||||
@@ -35,7 +34,7 @@ class Problem7(Problem):
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
super().__init__("What is the 10001th prime number?")
|
||||
super().__init__("What is the 10001st prime number?")
|
||||
self.primes = []
|
||||
|
||||
#Operational functions
|
||||
@@ -54,16 +53,21 @@ class Problem7(Problem):
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Save the results
|
||||
self.result = "The " + str(self.__numPrimes) + "th prime number is " + str(self.primes[self.__numPrimes - 1])
|
||||
|
||||
#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.primes.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 {self.__numPrimes}st prime number is {self.primes[self.__numPrimes - 1]}"
|
||||
#Returns the requested prime number
|
||||
def getPrime(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
@@ -71,17 +75,8 @@ class Problem7(Problem):
|
||||
raise Unsolved("You must solve the problem before you can get the requested prime")
|
||||
return self.primes[len(self.primes) - 1]
|
||||
|
||||
#If you are running this file, automatically start the correct function
|
||||
if __name__ == '__main__':
|
||||
problem = Problem7()
|
||||
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 10001th prime number is 104743
|
||||
The 10001st prime number is 104743
|
||||
It took an average of 109.101 milliseconds to run this problem through 100 iterations
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user