mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Initial commit with existing files
This commit is contained in:
87
Problem23.py
Normal file
87
Problem23.py
Normal file
@@ -0,0 +1,87 @@
|
||||
#ProjectEuler/Python/Problem23.py
|
||||
#Matthew Ellison
|
||||
# Created: 03-22-19
|
||||
#Modified: 03-28-19
|
||||
#Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers
|
||||
#All of my 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
|
||||
|
||||
__maxNum = 28123
|
||||
|
||||
|
||||
#A function that returns true if num can be created by adding two elements from abund and false if it cannot
|
||||
def isSum(abund: list, num: int) -> bool:
|
||||
sumOfNums = 0
|
||||
#Pick a number for the first part of the sum
|
||||
for firstNum in range(0, len(abund)):
|
||||
#Pick a number for the second part of the sum
|
||||
for secondNum in range(0, len(abund)):
|
||||
sumOfNums = abund[firstNum] + abund[secondNum]
|
||||
if(sumOfNums == num):
|
||||
return True
|
||||
elif(sumOfNums > num):
|
||||
break
|
||||
#If you have run through the entire list and did not find a sum then it is false
|
||||
return False
|
||||
|
||||
|
||||
def Problem23():
|
||||
#Setup the variables
|
||||
divisorSums = []
|
||||
#Make sure every element has a 0 in it's location
|
||||
for cnt in range(0, __maxNum):
|
||||
divisorSums.append(0)
|
||||
|
||||
#Get the sum of the divisors of all numbers < __maxNum
|
||||
for cnt in range(1, __maxNum):
|
||||
div = Algorithms.getDivisors(cnt)
|
||||
if(len(div) > 1):
|
||||
div.remove(div[len(div) - 1])
|
||||
divisorSums[cnt] = sum(div)
|
||||
|
||||
#Get the abundant numbers
|
||||
abund = []
|
||||
for cnt in range(0, len(divisorSums)):
|
||||
if(divisorSums[cnt] > cnt):
|
||||
abund.append(cnt)
|
||||
|
||||
#Check if each number can be the sum of 2 abundant numbers and add to the sum if no
|
||||
sumOfNums = 0
|
||||
for cnt in range(1, __maxNum):
|
||||
if(not isSum(abund, cnt)):
|
||||
sumOfNums += cnt
|
||||
|
||||
#Print the results
|
||||
print("The answer is " + str(sumOfNums))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
timer = Stopwatch()
|
||||
timer.start()
|
||||
Problem23()
|
||||
timer.stop()
|
||||
print("It took " + timer.getString() + " to run this algorithm")
|
||||
|
||||
""" Results:
|
||||
The answer is 4179871
|
||||
It took 27.738 minutes to run this algorithm
|
||||
"""
|
||||
Reference in New Issue
Block a user