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/Problem21.py
|
||||
#Matthew Ellison
|
||||
# Created: 03-18-19
|
||||
#Modified: 10-30-20
|
||||
#Modified: 07-24-21
|
||||
#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
|
||||
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 Problem21(Problem):
|
||||
@@ -33,12 +32,12 @@ class Problem21(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
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):
|
||||
def solve(self) -> None:
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
@@ -46,10 +45,11 @@ class Problem21(Problem):
|
||||
#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
|
||||
divisors = NumberAlgorithms.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)))
|
||||
@@ -67,6 +67,7 @@ class Problem21(Problem):
|
||||
#Add the number to the amicable vector
|
||||
self.amicable.append(cnt)
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -74,18 +75,15 @@ class Problem21(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.divisorSum.clear()
|
||||
self.amicable.clear()
|
||||
|
||||
#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")
|
||||
result = "All amicable numbers less than 10000 are\n"
|
||||
for num in self.amicable:
|
||||
result += f"{num}\n"
|
||||
@@ -94,15 +92,11 @@ class Problem21(Problem):
|
||||
return result
|
||||
#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")
|
||||
self.solvedCheck("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")
|
||||
self.solvedCheck("sum of the amicable numbers")
|
||||
return sum(self.amicable)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user