mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
91 lines
2.7 KiB
Python
91 lines
2.7 KiB
Python
#Project Euler/Python/Problem10.py
|
|
#Matthew Ellison
|
|
# Created: 01-30-19
|
|
#Modified: 07-18-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
|
|
"""
|
|
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 getPrimes
|
|
|
|
|
|
class Problem10(Problem):
|
|
#Variables
|
|
__numberGreaterThanPrimes = 2000000 - 1 #Get all primes < this number
|
|
|
|
#Functions
|
|
#Constructor
|
|
def __init__(self):
|
|
super().__init__("Find the sum of all the primes below two million")
|
|
self.sum = 0 #The sum of all of the prime numbers
|
|
|
|
#Operational functions
|
|
#Solve the function
|
|
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 all of the primes < 2000000
|
|
primes = getPrimes(self.__numberGreaterThanPrimes)
|
|
#Get the sum of the list
|
|
self.sum = sum(primes)
|
|
|
|
#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 sum that was requested
|
|
def getSum(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 sum")
|
|
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 5.926 seconds to run this algorithm
|
|
"""
|