mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Changed problems to work with a driver function
This commit is contained in:
87
Problems/Problem7.py
Normal file
87
Problems/Problem7.py
Normal file
@@ -0,0 +1,87 @@
|
||||
#Project Eulter/Python/Problem7.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-29-19
|
||||
#Modified: 07-18-20
|
||||
#What is the 10001th 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
|
||||
|
||||
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 Problems.Problem import Problem
|
||||
from Stopwatch import Stopwatch
|
||||
from Unsolved import Unsolved
|
||||
from Algorithms import getNumPrimes
|
||||
|
||||
|
||||
class Problem7(Problem):
|
||||
#Variables
|
||||
__numPrimes = 10001 #The number of the prime number desired
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
super().__init__("What is the 10001th prime number?")
|
||||
self.primes = []
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
#Get the correct number of primes
|
||||
self.primes = getNumPrimes(self.__numPrimes)
|
||||
|
||||
#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 requested prime number
|
||||
def getPrime(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
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
|
||||
It took 139.545 milliseconds to run this algorithm
|
||||
"""
|
||||
Reference in New Issue
Block a user