diff --git a/src/Problems.rs b/src/Problems.rs index e59d99e..f50f26c 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -51,3 +51,4 @@ pub mod Problem25; pub mod Problem26; pub mod Problem27; pub mod Problem28; +pub mod Problem29; diff --git a/src/Problems/Problem29.rs b/src/Problems/Problem29.rs new file mode 100644 index 0000000..cf1bc02 --- /dev/null +++ b/src/Problems/Problem29.rs @@ -0,0 +1,67 @@ +//ProjectEulerRust/src/Problems/Problems29.rs +//Matthew Ellison +// Created: 06-17-20 +//Modified: 06-17-20 +//How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100? +//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{ + "How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?".to_string() +} + +pub fn solve() -> Answer{ + //Setup the variables + let BOTTOM_A = 2; //The lowest possible value for a + let TOP_A = 100; //The highest possible value for a + let BOTTOM_B = 2; //The lowest possible value for b + let TOP_B = 100; //The highest possible value for b + let mut unique = Vec::::new(); //Holds all unique values generated + + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Start with the first A and move towards the top + for currentA in BOTTOM_A..=TOP_A{ + //Start with the first B and move towards the top + for currentB in BOTTOM_B..=TOP_B{ + //Get the new number + let currentNum = num::pow::pow(num::BigInt::from(currentA), currentB); + //If the current number is not int he array add it + if(!unique.contains(¤tNum)){ + unique.push(currentNum); + } + } + } + + //Stop the timer + timer.stop(); + + //Return the restuls + return Answer::new(format!("The number of unique values generated by a^b for {} <= a <= {} and {} <= b <= {} is {}", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B, unique.len()), timer.getString()); +} + +/* Result: +The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183 +It took 107.204 milliseconds to solve this problem +*/ diff --git a/src/main.rs b/src/main.rs index 3996f15..91af293 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,9 +33,9 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 29] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +static problemNumbers: [u32; 30] = [ 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]; + 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]; fn main(){ let mut selection = Selections::EMPTY; @@ -244,6 +244,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem28::getDescription()); println!("{}", Problems::Problem28::solve()); } + else if(problemNumber == 29){ + println!("{}", Problems::Problem29::getDescription()); + println!("{}", Problems::Problem29::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -353,6 +357,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 28){ println!("{}", Problems::Problem28::getDescription()); } + else if(problemNumber == 29){ + println!("{}", Problems::Problem29::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");