Added solution for problem 33

This commit is contained in:
2021-02-07 12:41:07 -05:00
parent ba5117ce0b
commit 2ed753a9bb
2 changed files with 151 additions and 1 deletions

View File

@@ -54,6 +54,7 @@ from Problems.Problem29 import Problem29
from Problems.Problem30 import Problem30
from Problems.Problem31 import Problem31
from Problems.Problem32 import Problem32
from Problems.Problem33 import Problem33
from Problems.Problem67 import Problem67
@@ -62,7 +63,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, 67]
31, 32, 33, 67]
#Returns the problem corresponding to the given problem number
@staticmethod
@@ -131,6 +132,8 @@ class ProblemSelection:
return Problem31()
elif(problemNumber == 32):
return Problem32()
elif(problemNumber == 33):
return Problem33()
elif(problemNumber == 67):
return Problem67()

147
Problems/Problem33.py Normal file
View File

@@ -0,0 +1,147 @@
#ProjectEuler/Python/Problem33.py
#Matthew Ellison
# Created: 02-07-21
#Modified: 02-07-21
"""
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s
We shall consider fractions like, 30/50 = 3/5, to be trivial examples
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator
If the product of these four fractions is given in its lowest common terms, find the value of the denominator
"""
#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 Problem33(Problem):
#Variables
#Static variables
__minNumerator = 10
__maxNumerator = 98
__minDenominator = 11
__maxDenominator = 99
#Instance variables
__numerators = []
__denominators = []
prodDenominator = 1
#Functions
#Constructor
def __init__(self):
super().__init__("If the product of these four fractions is given in its lowest common terms, find the value of the denominator")
#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()
#Search every possible numerator/denominator pair
for denominator in range(self.__minDenominator, self.__maxDenominator + 1):
for numerator in range(self.__minNumerator, denominator):
denom = str(denominator)
num = str(numerator)
tempNum = 0
tempDenom = 1
#Check that this isn't a trivial example
if((num[1] == '0') and (denom[1] == '0')):
continue
#Remove the offending digits if they exist
elif(num[0] == denom[0]):
tempNum = int(num[1])
tempDenom = int(denom[1])
elif(num[0] == denom[1]):
tempNum = int(num[1])
tempDenom = int(denom[0])
elif(num[1] == denom[0]):
tempNum = int(num[0])
tempDenom = int(denom[1])
elif(num[1] == denom[1]):
tempNum = int(num[0])
tempDenom = int(denom[0])
if(tempDenom == 0):
continue
#Test if the new fraction is the same as the old one
if((float(tempNum) / float(tempDenom)) == (float(numerator) / float(denominator))):
self.__numerators.append(numerator)
self.__denominators.append(denominator)
#Get the product of the numbers
numProd = Algorithms.prod(self.__numerators)
denomProd = Algorithms.prod(self.__denominators)
#Get the gcd to reduce to lowest terms
gcd = Algorithms.gcd(numProd, denomProd)
#Save the denominator
self.prodDenominator = int(denomProd / gcd)
#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.__numerators.clear()
self.__denominators.clear()
#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 denominator of the product is {self.prodDenominator}"
#Returns the list of numerators
def getNumerators(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 numerators")
return self.__numerators
#Returns the list of denominators
def getDenominators(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 denominators")
return self.__denominator
#Returns the answer to the question
def getProdDenominator(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 answer")
return self.prodDenominator
"""Results:
The denominator of the product is 100
It took an average of 5.130 milliseconds to run this problem through 100 iterations
"""