From a2298b5b8c48b365bbb265ad9664dab3882265aa Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 7 Feb 2021 12:41:17 -0500 Subject: [PATCH] Added solution to problem 33 --- .../ProjectEuler/ProblemSelection.java | 3 +- .../ProjectEuler/Problems/Problem33.java | 166 ++++++++++++++++++ 2 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java diff --git a/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java b/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java index 92798a5..a1fa721 100644 --- a/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java +++ b/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java @@ -36,7 +36,7 @@ public class ProblemSelection{ public static final ArrayList PROBLEM_NUMBERS = new ArrayList(Arrays.asList( 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 public static Problem getProblem(Integer problemNumber){ @@ -74,6 +74,7 @@ public class ProblemSelection{ case 30 : problem = new Problem30(); break; case 31 : problem = new Problem31(); break; case 32 : problem = new Problem32(); break; + case 33 : problem = new Problem33(); break; case 67 : problem = new Problem67(); break; } return problem; diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java new file mode 100644 index 0000000..8ca51dd --- /dev/null +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java @@ -0,0 +1,166 @@ +//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java +//Matthew Ellison +// Created: 02-05-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 non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses +/* + 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 . +*/ +package mattrixwv.ProjectEuler.Problems; + + +import java.util.ArrayList; + +import mattrixwv.Algorithms; +import mattrixwv.ProjectEuler.Unsolved; + + +public class Problem33 extends Problem{ + //Variables + //Static variables + private static final int MIN_NUMERATOR = 10; //The lowest the numerator can be + private static final int MAX_NUMERATOR = 98; //The highest the numerator can be + private static final int MIN_DENOMINATOR = 11; //The lowest the denominator can be + private static final int MAX_DENOMINATOR = 99; //The highest the denominator can be + //Instance variables + private ArrayList numerators; //Holds the numerators that were found + private ArrayList denominators; //Holds the denominators that were found + private int prodDenominator; //Holds the answer to the question + + //Functions + //Constructor + public Problem33(){ + super("If the product of these four fractions is given in its lowest common terms, find the value of the denominator"); + prodDenominator = 1; + numerators = new ArrayList(); + denominators = new ArrayList(); + } + //Operational functions + //Solve the problem + @Override + public void solve(){ + //If the problem has already been solved do nothing and end the function + if(solved){ + return; + } + + //Start the timer + timer.start(); + + //Search every possible numerator/denominator pair + for(int denominator = MIN_DENOMINATOR;denominator <= MAX_DENOMINATOR;++denominator){ + for(int numerator = MIN_NUMERATOR;(numerator < denominator) && (numerator <= MAX_NUMERATOR);++numerator){ + String denom = Integer.toString(denominator); + String num = Integer.toString(numerator); + int tempNum = 0; + int tempDenom = 1; + + //Check that this isn't a trivial example + if((num.charAt(1) == '0') && (denom.charAt(1) == '0')){ + continue; + } + //Remove the offending digits if they exist + else if(num.charAt(0) == denom.charAt(0)){ + tempNum = num.charAt(1) - 48; + tempDenom = denom.charAt(1) - 48; + } + else if(num.charAt(0) == denom.charAt(1)){ + tempNum = num.charAt(1) - 48; + tempDenom = denom.charAt(0) - 48; + } + else if(num.charAt(1) == denom.charAt(0)){ + tempNum = num.charAt(0) - 48; + tempDenom = denom.charAt(1) - 48; + } + else if(num.charAt(1) == denom.charAt(1)){ + tempNum = num.charAt(0) - 48; + tempDenom = denom.charAt(0) - 48; + } + + //Test if the new fraction is the same as the old one + if(((double)tempNum / (double)tempDenom) == ((double)numerator / (double)denominator)){ + numerators.add(numerator); + denominators.add(denominator); + } + } + } + + //Get the product of the numbers + int numProd = Algorithms.getProd(numerators); + int denomProd = Algorithms.getProd(denominators); + //Get the gcd to reduce to lowest terms + int gcd = Algorithms.gcd(numProd, denomProd); + //Save the denominator + prodDenominator = denomProd / gcd; + + //Stop the timer + timer.stop(); + + //Throw a flag to show the problem is solved + solved = true; + } + //Reset the problem so it can be run again + @Override + public void reset(){ + super.reset(); + numerators.clear(); + denominators.clear(); + } + //Gets + //Returns the result of solving the problem + @Override + public String getResult(){ + if(!solved){ + throw new Unsolved(); + } + return String.format("The denominator of the product is %d", prodDenominator); + } + //Returns the list of numerators + ArrayList getNumerators(){ + //If the problem hasn't been solved throw an exception + if(!solved){ + throw new Unsolved(); + } + return numerators; + } + //Returns the list of denominators + ArrayList getDenominators(){ + //If the problem hasn't been solved throw an exception + if(!solved){ + throw new Unsolved(); + } + return denominators; + } + //Returns the answer to the question + int getProdDenominator(){ + //If the problem hasn't been solved throw an exception + if(!solved){ + throw new Unsolved(); + } + return prodDenominator; + } +} + +/* Results: +The denominator of the product is 100 +It took an average of 522.671 microseconds to run this problem through 100 iterations +*/