Added solution to problem 36

This commit is contained in:
2021-06-29 15:37:23 -04:00
parent 38803dd7f1
commit 16ceb98e31
3 changed files with 105 additions and 3 deletions

View File

@@ -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
View 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
"""