diff --git a/src/Problems.rs b/src/Problems.rs index 01e5bfe..3d1ab60 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -55,13 +55,14 @@ pub mod Problem29; pub mod Problem30; pub mod Problem31; pub mod Problem32; +pub mod Problem33; pub mod Problem67; -pub static problemNumbers: [u32; 34] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +pub static problemNumbers: [u32; 35] = [ 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]; + 30, 31, 32, 33, 67]; pub static tooLong: [u32; 7] = [3, 5, 15, 23, 24, 25, 27]; pub fn solveProblem(problemNumber: u32, description: bool, solve: bool) -> Answer::Answer{ @@ -326,6 +327,14 @@ pub fn solveProblem(problemNumber: u32, description: bool, solve: bool) -> Answe answer = Problem32::solve(); } } + else if(problemNumber == 33){ + if(description){ + println!("{}", Problem33::getDescription()); + } + if(solve){ + answer = Problem33::solve(); + } + } else if(problemNumber == 67){ if(description){ println!("{}", Problem67::getDescription()); diff --git a/src/Problems/Problem33.rs b/src/Problems/Problem33.rs new file mode 100644 index 0000000..3af1fb2 --- /dev/null +++ b/src/Problems/Problem33.rs @@ -0,0 +1,99 @@ +//ProjectEulerRust/src/Problems/Problems33.rs +//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 non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses +/* + 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 . +*/ + + +extern crate myClasses; +use crate::Problems::Answer::Answer; + +pub fn getDescription() -> String{ + "If the product of these four fractions is given in its lowest common terms, find the value of the denominator".to_string() +} + +pub fn solve() -> Answer{ + let mut numerators = Vec::::new(); + let mut denominators = Vec::::new(); + + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + for denominator in 11..=99{ + for numerator in 10..denominator{ + let num1 = numerator.to_string(); + let denom = denominator.to_string(); + let mut tempNum = 0; + let mut tempDenom = 1; + + //Check that this isn't a trivial example + if((num1.chars().nth(1).unwrap() == '0') && (denom.chars().nth(1).unwrap() == '0')){ + continue; + } + //Remove the offending digits if they exist + else if(num1.chars().nth(0).unwrap() == denom.chars().nth(0).unwrap()){ + tempNum = num1.chars().nth(1).unwrap() as i32 - 48; + tempDenom = denom.chars().nth(1).unwrap() as i32 - 48; + } + else if(num1.chars().nth(0).unwrap() == denom.chars().nth(1).unwrap()){ + tempNum = num1.chars().nth(1).unwrap() as i32 - 48; + tempDenom = denom.chars().nth(0).unwrap() as i32 - 48; + } + else if(num1.chars().nth(1).unwrap() == denom.chars().nth(0).unwrap()){ + tempNum = num1.chars().nth(0).unwrap() as i32 - 48; + tempDenom = denom.chars().nth(1).unwrap() as i32 - 48; + } + else if(num1.chars().nth(1).unwrap() == denom.chars().nth(1).unwrap()){ + tempNum = num1.chars().nth(0).unwrap() as i32 - 48; + tempDenom = denom.chars().nth(0).unwrap() as i32 - 48; + } + + //Test if the new fraction is the same as the old one + if((tempNum as f64 / tempDenom as f64) == (numerator as f64 / denominator as f64)){ + numerators.push(numerator); + denominators.push(denominator); + } + } + } + + //Get the product of the numbers + let numProd = numerators.iter().product::(); + let denomProd = denominators.iter().product::(); + //Get the gcd to reduce to lowest terms + let gcd = myClasses::Algorithms::gcd(numProd, denomProd); + //Save the denominator + let prodDenominator = denomProd / gcd; + + //Stop the timer + timer.stop(); + + return Answer::new(format!("The denominator of the product is {}", prodDenominator), timer.getString(), timer.getNano()); +} + +/* Results: +The denominator of the product is 100 +It took an average of 717.868 microseconds to run this problem through 100 iterations +*/