diff --git a/src/Problems.rs b/src/Problems.rs index c39f4b1..8fb87a3 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -10,3 +10,4 @@ pub mod Problem1; pub mod Problem2; pub mod Problem3; pub mod Problem4; +pub mod Problem5; diff --git a/src/Problems/Problem5.rs b/src/Problems/Problem5.rs new file mode 100644 index 0000000..ccfcb87 --- /dev/null +++ b/src/Problems/Problem5.rs @@ -0,0 +1,63 @@ +//ProjectEulerRust/src/Problems/Problems5.rs +//Matthew Ellison +// Created: 06-15-20 +//Modified: 06-15-20 +//What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses +/* + Copyright (C) 2019 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{ + "What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?".to_string() +} + +pub fn solve() -> Answer{ + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Start at 20 because it must at least be divisible by 20. Increment by 2 because it must be an even number to be divisible by 2 + let mut numFound = false; + let mut currentNum = 20; + //If the number is < 0 it has overflowed + while((currentNum > 0) && (!numFound)){ + //Start by assuming you found the number (because we throw a flad if we didn't find it) + numFound = true; + //Step through every number from 1-20 seeing if the current number is divisible by it + for divisor in 1..=20{ + if((currentNum % divisor) != 0){ + numFound = false; + break; + } + } + //If you didn't find the correct number then increment by 2 + if(!numFound){ + currentNum += 2; + } + } + + //Stop the timer + timer.stop(); + + //Save the results + return Answer::new(format!("The smallest positive number evenly divisibly by all number 1-20 is {}", currentNum), timer.getString()); +} diff --git a/src/main.rs b/src/main.rs index 6a9ed89..f8b9d29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 5] = [0, 1, 2, 3, 4]; +static problemNumbers: [u32; 6] = [0, 1, 2, 3, 4, 5]; fn main(){ let mut selection = Selections::EMPTY; @@ -129,6 +129,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem4::getDescription()); println!("{}", Problems::Problem4::solve()); } + else if(problemNumber == 5){ + println!("{}", Problems::Problem5::getDescription()); + println!("{}", Problems::Problem5::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -166,6 +170,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 4){ println!("{}", Problems::Problem4::getDescription()); } + else if(problemNumber == 5){ + println!("{}", Problems::Problem5::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");