Updated to use new library layout

This commit is contained in:
2021-07-24 16:13:05 -04:00
parent d18b3fa9f6
commit 84555edd31
39 changed files with 515 additions and 709 deletions

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem33.py
#Matthew Ellison
# Created: 02-07-21
#Modified: 02-07-21
#Modified: 07-24-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
@@ -28,9 +28,7 @@ If the product of these four fractions is given in its lowest common terms, find
from Problems.Problem import Problem
from Unsolved import Unsolved
import Algorithms
import ArrayAlgorithms
class Problem33(Problem):
@@ -47,12 +45,12 @@ class Problem33(Problem):
#Functions
#Constructor
def __init__(self):
def __init__(self) -> None:
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):
def solve(self) -> None:
#If the problem has already been solved do nothing and end the function
if(self.solved):
return
@@ -60,6 +58,7 @@ class Problem33(Problem):
#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):
@@ -93,13 +92,14 @@ class Problem33(Problem):
self.__denominators.append(denominator)
#Get the product of the numbers
numProd = Algorithms.prod(self.__numerators)
denomProd = Algorithms.prod(self.__denominators)
numProd = ArrayAlgorithms.prod(self.__numerators)
denomProd = ArrayAlgorithms.prod(self.__denominators)
#Get the gcd to reduce to lowest terms
gcd = Algorithms.gcd(numProd, denomProd)
gcd = ArrayAlgorithms.gcd(numProd, denomProd)
#Save the denominator
self.prodDenominator = int(denomProd / gcd)
#Stop the timer
self.timer.stop()
@@ -107,40 +107,33 @@ class Problem33(Problem):
self.solved = True
#Reset the problem so it can be run again
def reset(self):
def reset(self) -> None:
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")
def getResult(self) -> str:
self.solvedCheck("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")
def getNumerators(self) -> list:
self.solvedCheck("list of 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")
def getDenominators(self) -> list:
self.solvedCheck("list of 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")
def getProdDenominator(self) -> int:
self.solvedCheck("denominator")
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