mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
126 lines
4.2 KiB
Python
126 lines
4.2 KiB
Python
#ProjectEuler/Python/Problem21.py
|
|
#Matthew Ellison
|
|
# Created: 03-18-19
|
|
#Modified: 07-19-20
|
|
#Evaluate the sum of all the amicable numbers under 10000
|
|
#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
|
|
import Algorithms
|
|
|
|
|
|
class Problem21(Problem):
|
|
#Variables
|
|
__limit = 10000 #The top number that will be evaluated
|
|
|
|
#Functions
|
|
#Constructor
|
|
def __init__(self):
|
|
super().__init__("Evaluate the sum of all the amicable numbers under 10000")
|
|
self.divisorSum = [] #Holds the sum of the divisors of the subscript number
|
|
self.amicable = [] #Holds all amicable numbers
|
|
|
|
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()
|
|
|
|
#Generate the divisors of all the numbers < 10000, get their sum, and add it to the list
|
|
self.divisorSum.append(0) #Start with a 0 in the [0] location
|
|
for cnt in range(1, self.__limit):
|
|
divisors = Algorithms.getDivisors(cnt) #Get all the divisors of a number
|
|
if(len(divisors) > 1):
|
|
divisors.pop() #Remove the last entry because it will be the number itself
|
|
self.divisorSum.append(int(sum(divisors)))
|
|
#Check every sum of divisors in the list for a matching sum
|
|
for cnt in range(1, len(self.divisorSum)):
|
|
currentSum = self.divisorSum[cnt]
|
|
#If the sum is greater than the number of divisors then it is impossible to be amicable. Skip the number and continue
|
|
if(currentSum >= len(self.divisorSum)):
|
|
continue
|
|
#We know that divisorSum[cnt] == currentSum, so if divisorSum[currentSum] == cnt we found an amicable number
|
|
if(self.divisorSum[currentSum] == cnt):
|
|
#A number can't be amicable with itself
|
|
if(currentSum == cnt):
|
|
continue
|
|
#Add the number to the amicable vector
|
|
self.amicable.append(cnt)
|
|
|
|
#Stop the timer
|
|
self.timer.stop()
|
|
|
|
#Save the results
|
|
self.result += "All amicable numbers less than 10000 are"
|
|
for num in self.amicable:
|
|
self.result += str(num) + '\n'
|
|
self.result += "The sum of all of these amicable numbers is " + str(sum(self.amicable))
|
|
|
|
#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.divisorSum.clear()
|
|
self.amicable.clear()
|
|
|
|
#Gets
|
|
#Returns a vector with all of the amicable number calculated
|
|
def getAmicable(self) -> list:
|
|
#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 amicable numbers")
|
|
return self.amicable
|
|
#Returns the sum of all of the amicable numbers
|
|
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 of the amicable numbers")
|
|
return sum(self.amicable)
|
|
|
|
#Run the correct function if this script is called stand along
|
|
if __name__ == "__main__":
|
|
problem = Problem21()
|
|
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:
|
|
All amicable numbers less than 10000 are
|
|
220
|
|
284
|
|
1184
|
|
1210
|
|
2620
|
|
2924
|
|
5020
|
|
5564
|
|
6232
|
|
6368
|
|
The sum of all of these amicable numbers is 31626
|
|
It took 59.496 milliseconds to run this algorithm
|
|
"""
|