mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Added solution to problem 36
This commit is contained in:
@@ -57,6 +57,7 @@ from Problems.Problem32 import Problem32
|
||||
from Problems.Problem33 import Problem33
|
||||
from Problems.Problem34 import Problem34
|
||||
from Problems.Problem35 import Problem35
|
||||
from Problems.Problem36 import Problem36
|
||||
from Problems.Problem67 import Problem67
|
||||
|
||||
|
||||
@@ -65,7 +66,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, 35, 67]
|
||||
31, 32, 33, 34, 35, 36, 67]
|
||||
|
||||
#Returns the problem corresponding to the given problem number
|
||||
@staticmethod
|
||||
@@ -140,6 +141,8 @@ class ProblemSelection:
|
||||
return Problem34()
|
||||
elif(problemNumber == 35):
|
||||
return Problem35()
|
||||
elif(problemNumber == 36):
|
||||
return Problem36()
|
||||
elif(problemNumber == 67):
|
||||
return Problem67()
|
||||
|
||||
|
||||
@@ -51,7 +51,7 @@ class Problem35(Problem):
|
||||
#Operational functions
|
||||
#Solve the problem
|
||||
def solve(self):
|
||||
#If the porblem has already been solved do nothing and end the function
|
||||
#If the problem has already been solved do nothing and end the function
|
||||
if(self.solved):
|
||||
return
|
||||
|
||||
@@ -91,7 +91,7 @@ class Problem35(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")
|
||||
raise Unsolved("You must solve the problem 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:
|
||||
|
||||
99
Problems/Problem36.py
Normal file
99
Problems/Problem36.py
Normal file
@@ -0,0 +1,99 @@
|
||||
#ProjectEuler/ProjectEulerPython/Problems/Problem36.py
|
||||
#Matthew Ellison
|
||||
# Created: 06-29-21
|
||||
#Modified: 06-29-21
|
||||
#Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
|
||||
#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 Problem36(Problem):
|
||||
#Variables
|
||||
__max_num = 999999 #The largest number that will be checked
|
||||
|
||||
#Functions
|
||||
#Constructor
|
||||
def __init__(self):
|
||||
super().__init__("Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.")
|
||||
self.palindromes = []
|
||||
self.sumOfPal = 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):
|
||||
return
|
||||
|
||||
#Start the timer
|
||||
self.timer.start()
|
||||
|
||||
#Start with 1, check if it is a palindrome in base 10 and 2, and continue to __max_num
|
||||
for num in range(1, self.__max_num + 1):
|
||||
#Check if num is a palindrome
|
||||
if(Algorithms.isPalindrome(str(num))):
|
||||
#Convert num to base 2 and see if that is a palindrome
|
||||
binNum = Algorithms.toBin(num)
|
||||
if(Algorithms.isPalindrome(binNum)):
|
||||
#Add num to the list of palindromes
|
||||
self.palindromes.append(num)
|
||||
#Get the sum of all palindromes in the list
|
||||
self.sumOfPal = sum(self.palindromes)
|
||||
|
||||
#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.palindromes = []
|
||||
self.sum = 0
|
||||
|
||||
#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 problem before you can see the result")
|
||||
return f"The sum of all base 10 and base 2 palindromic numbers < {self.__max_num} is {self.sumOfPal}"
|
||||
#Return the list of palindromes < MAX_NUM
|
||||
def getPalindromes(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 see the palindromes")
|
||||
return self.palindromes
|
||||
#Return the sum of all elements in the list of palindromes
|
||||
def getSumOfPalindromes(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 see the sum of the palindromes")
|
||||
return self.sumOfPal
|
||||
|
||||
""" Results:
|
||||
The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187
|
||||
It took an average of 295.861 milliseconds to run this problem through 100 iterations
|
||||
"""
|
||||
Reference in New Issue
Block a user