mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2026-02-03 19:52:28 -05:00
Added solution to problem 34
This commit is contained in:
@@ -1,10 +1,10 @@
|
|||||||
#ProjectEulerPython/Bechmark.py
|
#ProjectEulerPython/Bechmark.py
|
||||||
#Matthew Ellison
|
#Matthew Ellison
|
||||||
# Created: 07-19-20
|
# Created: 07-19-20
|
||||||
#Modified: 07-19-20
|
#Modified: 06-01-21
|
||||||
#This is the driver function for the Java version of the ProjectEuler project
|
#This is the driver function for the Java version of the ProjectEuler project
|
||||||
"""
|
"""
|
||||||
Copyright (C) 2020 Matthew Ellison
|
Copyright (C) 2021 Matthew Ellison
|
||||||
|
|
||||||
This program is free software: you can redistribute it and/or modify
|
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
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
@@ -34,7 +34,7 @@ class Benchmark:
|
|||||||
exit = 4
|
exit = 4
|
||||||
size = 5
|
size = 5
|
||||||
|
|
||||||
__tooLong = [3, 5, 10, 12, 14, 15, 23, 24, 25, 27, 30, 67]
|
__tooLong = [3, 5, 10, 12, 14, 15, 23, 24, 25, 27, 30, 34, 67]
|
||||||
|
|
||||||
#The driver function for the benchmark selection
|
#The driver function for the benchmark selection
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ from Problems.Problem30 import Problem30
|
|||||||
from Problems.Problem31 import Problem31
|
from Problems.Problem31 import Problem31
|
||||||
from Problems.Problem32 import Problem32
|
from Problems.Problem32 import Problem32
|
||||||
from Problems.Problem33 import Problem33
|
from Problems.Problem33 import Problem33
|
||||||
|
from Problems.Problem34 import Problem34
|
||||||
from Problems.Problem67 import Problem67
|
from Problems.Problem67 import Problem67
|
||||||
|
|
||||||
|
|
||||||
@@ -63,7 +64,7 @@ class ProblemSelection:
|
|||||||
problemNumbers = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
problemNumbers = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
||||||
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||||
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
|
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
|
||||||
31, 32, 33, 67]
|
31, 32, 33, 34, 67]
|
||||||
|
|
||||||
#Returns the problem corresponding to the given problem number
|
#Returns the problem corresponding to the given problem number
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@@ -134,6 +135,8 @@ class ProblemSelection:
|
|||||||
return Problem32()
|
return Problem32()
|
||||||
elif(problemNumber == 33):
|
elif(problemNumber == 33):
|
||||||
return Problem33()
|
return Problem33()
|
||||||
|
elif(problemNumber == 34):
|
||||||
|
return Problem34()
|
||||||
elif(problemNumber == 67):
|
elif(problemNumber == 67):
|
||||||
return Problem67()
|
return Problem67()
|
||||||
|
|
||||||
|
|||||||
102
Problems/Problem34.py
Normal file
102
Problems/Problem34.py
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
#ProjectEuler/ProjectEulerPython/Problems/Problem34.py
|
||||||
|
#Matthew Ellison
|
||||||
|
# Created: 06-01-21
|
||||||
|
#Modified: 06-01-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
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
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 Problems.Problem import Problem
|
||||||
|
from Unsolved import Unsolved
|
||||||
|
|
||||||
|
import Algorithms
|
||||||
|
|
||||||
|
|
||||||
|
class Problem34(Problem):
|
||||||
|
#Variables
|
||||||
|
__max_num = 1499999
|
||||||
|
|
||||||
|
#Functions
|
||||||
|
#Constructor
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__("")
|
||||||
|
self.totalSum = 0
|
||||||
|
self.factorials = []
|
||||||
|
for cnt in range(0, 10):
|
||||||
|
self.factorials.append(0)
|
||||||
|
|
||||||
|
def solve(self):
|
||||||
|
#If the problem has already been solved do nothing and end the function
|
||||||
|
if(self.solved):
|
||||||
|
return
|
||||||
|
|
||||||
|
#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)
|
||||||
|
#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)
|
||||||
|
currentSum = 0
|
||||||
|
for char in numString:
|
||||||
|
currentSum += self.factorials[int(char)]
|
||||||
|
#If the number is equal to the sum add the sum to the running sum
|
||||||
|
if(currentSum == cnt):
|
||||||
|
self.totalSum += currentSum
|
||||||
|
|
||||||
|
#Stop the timer
|
||||||
|
self.timer.stop()
|
||||||
|
|
||||||
|
#Throw a flag to show the porblem is solved
|
||||||
|
self.solved = True
|
||||||
|
|
||||||
|
#Reset the problem so it can be run again
|
||||||
|
def reset(self):
|
||||||
|
super().reset()
|
||||||
|
self.totalSum = 0
|
||||||
|
self.factorials = []
|
||||||
|
for _ in range(0, 10):
|
||||||
|
self.factorials.append(0)
|
||||||
|
|
||||||
|
#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")
|
||||||
|
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")
|
||||||
|
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")
|
||||||
|
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