diff --git a/src/Problems.rs b/src/Problems.rs index 3c6f0af..319498d 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -53,4 +53,5 @@ pub mod Problem27; pub mod Problem28; pub mod Problem29; pub mod Problem30; +pub mod Problem31; pub mod Problem67; diff --git a/src/Problems/Problem31.rs b/src/Problems/Problem31.rs new file mode 100644 index 0000000..deacde8 --- /dev/null +++ b/src/Problems/Problem31.rs @@ -0,0 +1,66 @@ +//ProjectEulerRust/src/Problems/Problems31.rs +//Matthew Ellison +// Created: 06-19-20 +//Modified: 06-19-20 +//How many different ways can £2 be made using any number of coins? +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses +/* + Copyright (C) 2020 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{ + "Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.".to_string() +} + +pub fn solve() -> Answer{ + let desiredValue = 200; + let mut permutations = 0; + + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Start with 200p and remove the necessary coins with each loop + for pound2 in (0..=desiredValue).rev().step_by(200){ + for pound1 in (0..=pound2).rev().step_by(100){ + for pence50 in (0..=pound1).rev().step_by(50){ + for pence20 in (0..=pence50).rev().step_by(20){ + for pence10 in (0..=pence20).rev().step_by(10){ + for pence5 in (0..=pence10).rev().step_by(5){ + for _pence2 in (0..=pence5).rev().step_by(2){ + permutations += 1; + } + } + } + } + } + } + } + + //Stop the timer + timer.stop(); + + return Answer::new(format!("There are {} ways to make 2 pounds with the given denominations of coins", permutations), timer.getString()); +} + +/* Results: +There are 73682 ways to make 2 pounds with the given denominations of coins +It took 109.300 microseconds to solve this problem +*/ diff --git a/src/main.rs b/src/main.rs index 85bc24a..1f86165 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,10 +33,10 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 32] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +static problemNumbers: [u32; 33] = [ 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, 67]; + 30, 31, 67]; fn main(){ let mut selection = Selections::EMPTY; @@ -253,6 +253,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem30::getDescription()); println!("{}", Problems::Problem30::solve()); } + else if(problemNumber == 31){ + println!("{}", Problems::Problem31::getDescription()); + println!("{}", Problems::Problem31::solve()); + } else if(problemNumber == 67){ println!("{}", Problems::Problem67::getDescription()); println!("{}", Problems::Problem67::solve()); @@ -372,6 +376,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 30){ println!("{}", Problems::Problem30::getDescription()); } + else if(problemNumber == 31){ + println!("{}", Problems::Problem31::getDescription()); + } else if(problemNumber == 67){ println!("{}", Problems::Problem67::getDescription()); }