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,7 +1,7 @@
|
||||
#ProjectEuler/ProjectEulerPython/Problems/Problem34.py
|
||||
#Matthew Ellison
|
||||
# Created: 06-01-21
|
||||
#Modified: 06-01-21
|
||||
#Modified: 07-21-21
|
||||
#Find the sum of all numbers which are equal to the sum of the factorial of their digits
|
||||
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
|
||||
"""
|
||||
@@ -23,9 +23,7 @@
|
||||
|
||||
|
||||
from Problems.Problem import Problem
|
||||
from Unsolved import Unsolved
|
||||
|
||||
import Algorithms
|
||||
import NumberAlgorithms
|
||||
|
||||
|
||||
class Problem34(Problem):
|
||||
@@ -34,7 +32,7 @@ class Problem34(Problem):
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__("Find the sum of all numbers which are equal to the sum of the factorial of their digits")
|
||||
self.totalSum = 0
|
||||
self.factorials = []
|
||||
@@ -43,7 +41,7 @@ class Problem34(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
|
||||
@@ -51,9 +49,10 @@ class Problem34(Problem):
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
|
||||
#Pre-compute the possible factorials from 0! to 9!
|
||||
for cnt in range(0, 10):
|
||||
self.factorials[cnt] = Algorithms.factorial(cnt)
|
||||
self.factorials[cnt] = NumberAlgorithms.factorial(cnt)
|
||||
#Run through all possible numbers from 3-MAX_NUM and see if they equal the sum of their digit's factorials
|
||||
for cnt in range(3, self.__max_num + 1):
|
||||
numString = str(cnt)
|
||||
@@ -64,6 +63,7 @@ class Problem34(Problem):
|
||||
if(currentSum == cnt):
|
||||
self.totalSum += currentSum
|
||||
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
@@ -71,7 +71,7 @@ class Problem34(Problem):
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
def reset(self) -> None:
|
||||
super().reset()
|
||||
self.totalSum = 0
|
||||
self.factorials = []
|
||||
@@ -80,24 +80,19 @@ class Problem34(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 sum of all numbers that are the sum of their digit's factorials is {self.totalSum}"
|
||||
#Returns the list of factorials from 0-9
|
||||
def getFactorials(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 getFactorials(self) -> list:
|
||||
self.solvedCheck("list of factorials")
|
||||
return self.factorials
|
||||
#Returns the sum of all numbers equal to the sum of their digit's factorials
|
||||
def getSum(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 getSum(self) -> int:
|
||||
self.solvedCheck("sum")
|
||||
return self.totalSum
|
||||
|
||||
|
||||
""" Results:
|
||||
The sum of all numbers that are the sum of their digit's factorials is 40730
|
||||
It took an average of 2.337 seconds to run this problem through 100 iterations
|
||||
|
||||
Reference in New Issue
Block a user