mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Updated to use new library layout
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
#ProjectEuler/Python/Problem23.py
|
||||
#Matthew Ellison
|
||||
# Created: 03-22-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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) 2020 Matthew Ellison
|
||||
Copyright (C) 2021 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
|
||||
@@ -23,8 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem23(Problem):
|
||||
@@ -33,7 +32,7 @@ class Problem23(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers")
|
||||
self.divisorSums = []
|
||||
self.reserveArray()
|
||||
@@ -41,7 +40,7 @@ class Problem23(Problem):
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -49,9 +48,10 @@ class Problem23(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Get the sum of the divisors of all numbers < __maxNum
|
||||
for cnt in range(1, self.__maxNum):
|
||||
div = Algorithms.getDivisors(cnt)
|
||||
div = NumberAlgorithms.getDivisors(cnt)
|
||||
if(len(div) > 1):
|
||||
div.remove(div[len(div) - 1])
|
||||
self.divisorSums[cnt] = sum(div)
|
||||
@@ -68,15 +68,16 @@ class Problem23(Problem):
|
||||
if(not self.isSum(abund, cnt)):
|
||||
self.sum += cnt
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
#Reserve the size of the array to speed up insertion
|
||||
def reserveArray(self):
|
||||
def reserveArray(self) -> None:
|
||||
#Make sure every element has a 0 in it's location
|
||||
for cnt in range(0, self.__maxNum):
|
||||
for _ in range(0, self.__maxNum):
|
||||
self.divisorSums.append(0)
|
||||
#A function that returns true if num can be created by adding two elements from abund and false if it cannot
|
||||
def isSum(self, abund: list, num: int) -> bool:
|
||||
@@ -93,7 +94,7 @@ class Problem23(Problem):
|
||||
#If you have run through the entire list and did not find a sum then it is false
|
||||
return False
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.divisorSums.clear()
|
||||
self.reserveArray()
|
||||
@@ -101,16 +102,12 @@ class Problem23(Problem):
|
||||
|
||||
#Gets
|
||||
#Returns the result of solving the problem
|
||||
def getResult(self):
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the problem before you can see the result")
|
||||
def getResult(self) -> str:
|
||||
self.solvedCheck("result")
|
||||
return f"The answer is {self.sum}"
|
||||
#Returns the sum of the numbers asked for
|
||||
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 can you see the sum")
|
||||
self.solvedCheck("sum")
|
||||
return self.sum
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user