mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
86 lines
2.8 KiB
Python
86 lines
2.8 KiB
Python
#ProjectEuler/Python/Problem21.py
|
|
#Matthew Ellison
|
|
# Created: 03-18-19
|
|
#Modified: 03-28-19
|
|
#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) 2019 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 Stopwatch import Stopwatch
|
|
import Algorithms
|
|
|
|
__limit = 10000 #The top number that will be evaluated
|
|
|
|
|
|
def Problem21():
|
|
#Setup the variables
|
|
divisorSum = [] #Holds the sum of the divisors of the subscript number
|
|
divisorSum.append(0) #Start with a 0 in the [0] location
|
|
|
|
#Generate the divisors of all the numbers < 10000, get their sum, and add it to the list
|
|
for cnt in range(1, __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
|
|
divisorSum.append(int(sum(divisors)))
|
|
#Check every sum of divisors in the list for a matching sum
|
|
amicable = []
|
|
for cnt in range(1, len(divisorSum)):
|
|
currentSum = 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(divisorSum)):
|
|
continue
|
|
#We know that divisorSum[cnt] == currentSum, so if divisorSum[currentSum] == cnt we found an amicable number
|
|
if(divisorSum[currentSum] == cnt):
|
|
#A number can't be amicable with itself
|
|
if(currentSum == cnt):
|
|
continue
|
|
#Add the number to the amicable vector
|
|
amicable.append(cnt)
|
|
|
|
#Print the results
|
|
print("All amicable numbers less than 10000 are")
|
|
for num in amicable:
|
|
print(str(num))
|
|
print("The sum of all of these amicable numbers is " + str(sum(amicable)))
|
|
|
|
#Run the correct function if this script is called stand along
|
|
if __name__ == "__main__":
|
|
timer = Stopwatch()
|
|
timer.start()
|
|
Problem21()
|
|
timer.stop()
|
|
print("It took " + timer.getString() + " to run 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
|
|
"""
|