mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 09:33:59 -05:00
Added solution for problem 35
This commit is contained in:
@@ -34,7 +34,7 @@ class Benchmark:
|
||||
exit = 4
|
||||
size = 5
|
||||
|
||||
__tooLong = [3, 5, 10, 12, 14, 15, 23, 24, 25, 27, 30, 34, 67]
|
||||
__tooLong = [3, 5, 10, 12, 14, 15, 23, 24, 25, 27, 30, 34, 35, 67]
|
||||
|
||||
#The driver function for the benchmark selection
|
||||
@staticmethod
|
||||
|
||||
@@ -56,6 +56,7 @@ from Problems.Problem31 import Problem31
|
||||
from Problems.Problem32 import Problem32
|
||||
from Problems.Problem33 import Problem33
|
||||
from Problems.Problem34 import Problem34
|
||||
from Problems.Problem35 import Problem35
|
||||
from Problems.Problem67 import Problem67
|
||||
|
||||
|
||||
@@ -64,7 +65,7 @@ class ProblemSelection:
|
||||
problemNumbers = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
|
||||
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
|
||||
31, 32, 33, 34, 67]
|
||||
31, 32, 33, 34, 35, 67]
|
||||
|
||||
#Returns the problem corresponding to the given problem number
|
||||
@staticmethod
|
||||
@@ -137,6 +138,8 @@ class ProblemSelection:
|
||||
return Problem33()
|
||||
elif(problemNumber == 34):
|
||||
return Problem34()
|
||||
elif(problemNumber == 35):
|
||||
return Problem35()
|
||||
elif(problemNumber == 67):
|
||||
return Problem67()
|
||||
|
||||
|
||||
@@ -35,12 +35,14 @@ class Problem34(Problem):
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
super().__init__("")
|
||||
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 = []
|
||||
for cnt in range(0, 10):
|
||||
for _ in range(0, 10):
|
||||
self.factorials.append(0)
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
|
||||
119
Problems/Problem35.py
Normal file
119
Problems/Problem35.py
Normal file
@@ -0,0 +1,119 @@
|
||||
#ProjectEuler/ProjectEulerPython/Problems/Problem35.py
|
||||
#Matthew Ellison
|
||||
# Created: 06-05-21
|
||||
#Modified: 06-05-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 Problem35(Problem):
|
||||
#Variables
|
||||
__max_num = 999999
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
super().__init__("Find the sum of all numbers which are equal to the sum of the factorial of their digits")
|
||||
self.primes = []
|
||||
self.circularPrimes = []
|
||||
|
||||
#Returns a list of all rotations of a string passed to it
|
||||
def getRotations(self, str: str) -> list:
|
||||
rotations = []
|
||||
rotations.append(str)
|
||||
for _ in range(1, len(str)):
|
||||
str = str[1::] + str[0]
|
||||
rotations.append(str)
|
||||
return rotations
|
||||
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
#If the porblem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
#Get all primes under 1,000,000
|
||||
self.primes = Algorithms.getPrimes(self.__max_num)
|
||||
#Go through all primes, get all their rotations, and check if those numbers are also primes
|
||||
for prime in self.primes:
|
||||
allRotationsPrime = True
|
||||
#Get all of the rotations of the prime and see if they are also prime
|
||||
rotations = self.getRotations(str(prime))
|
||||
for rotation in rotations:
|
||||
p = int(rotation)
|
||||
if(p not in self.primes):
|
||||
allRotationsPrime = False
|
||||
break
|
||||
#If all rotations are prime add it to the list of circular primes
|
||||
if(allRotationsPrime):
|
||||
self.circularPrimes.append(prime)
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
super().reset()
|
||||
self.primes = []
|
||||
self.circularPrimes = []
|
||||
|
||||
#Gets
|
||||
#Returns a string with the solution to the problem
|
||||
def getResult(self) -> str:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the result")
|
||||
return f"The number of all circular prime numbers under {self.__max_num} is {len(self.circularPrimes)}"
|
||||
#Returns the list of primes < max_num
|
||||
def getPrimes(self) -> list:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the primes")
|
||||
return self.primes
|
||||
#Returns the list of circular primes < max_num
|
||||
def getCircularPrimes(self) -> list:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the circular primes")
|
||||
return self.circularPrimes
|
||||
#Returns the number of circular primes
|
||||
def getNumCircularPrimes(self) -> list:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
if(not self.solved):
|
||||
raise Unsolved("You must solve the porblem before you can see the number of circular primes")
|
||||
return len(self.circularPrimes)
|
||||
|
||||
|
||||
""" Results:
|
||||
The number of all circular prime numbers under 999999 is 55
|
||||
It took 106.369 seconds to solve this algorithm
|
||||
"""
|
||||
Reference in New Issue
Block a user