From 58c966711dc4547145d1a20b60a9cada6a737a1e Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 12 Jun 2020 19:31:24 -0400 Subject: [PATCH] Initial commit with a few problems --- .gitignore | 8 ++ Cargo.toml | 10 +++ src/Problems.rs | 3 + src/Problems/Answer.rs | 15 ++++ src/Problems/Problem1.rs | 27 +++++++ src/Problems/Problem2.rs | 27 +++++++ src/main.rs | 171 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 261 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/Problems.rs create mode 100644 src/Problems/Answer.rs create mode 100644 src/Problems/Problem1.rs create mode 100644 src/Problems/Problem2.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5a8509 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +#vscode +.vscode/ + +#Executables +target/ + +#Cargo extra files +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a391134 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ProjectEulerRust" +version = "0.1.0" +authors = ["Mattrixwv "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +myClasses = {path = "D:/Programs/Rust/myClasses"} diff --git a/src/Problems.rs b/src/Problems.rs new file mode 100644 index 0000000..81f9dad --- /dev/null +++ b/src/Problems.rs @@ -0,0 +1,3 @@ +pub mod Answer; +pub mod Problem1; +pub mod Problem2; diff --git a/src/Problems/Answer.rs b/src/Problems/Answer.rs new file mode 100644 index 0000000..e901f8f --- /dev/null +++ b/src/Problems/Answer.rs @@ -0,0 +1,15 @@ +//A structure to hold the answer to the problem +pub struct Answer{ + result: String, + time: String, +} +impl std::fmt::Display for Answer{ + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result{ + write!(f, "{}\nIt took {} to solve this problem", self.result, self.time) + } +} +impl Answer{ + pub fn new(result: String, time: String) -> Answer{ + Answer{result, time} + } +} diff --git a/src/Problems/Problem1.rs b/src/Problems/Problem1.rs new file mode 100644 index 0000000..586d931 --- /dev/null +++ b/src/Problems/Problem1.rs @@ -0,0 +1,27 @@ +extern crate myClasses; + +pub use crate::Problems::Answer::Answer; + +pub fn getDescription() -> &'static str{ + "What is the sum of all the multiples of 3 or 5 that are less than 1000" +} + +pub fn solve() -> Answer{ + let mut fullSum = 0; + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + //Set through every number < 1000 and see if either 3 or 5 divide it evenly + for cnt in 1..1000{ + //If either divides it then add it to the vector + if((cnt % 3) == 0){ + fullSum += cnt; + } + else if((cnt % 5) == 0){ + fullSum += cnt; + } + } + timer.stop(); + + //Return the answer + return Answer::new("The sum of all numbers < 1000 is ".to_string() + &fullSum.to_string(), timer.getString()); +} diff --git a/src/Problems/Problem2.rs b/src/Problems/Problem2.rs new file mode 100644 index 0000000..1b6c3d9 --- /dev/null +++ b/src/Problems/Problem2.rs @@ -0,0 +1,27 @@ + +extern crate myClasses; + +use crate::Problems::Answer::Answer; + +pub fn getDescription() -> &'static str{ + "What is the sum of the even Fibonacci numbers less than 4,000,000?" +} + +pub fn solve() -> Answer{ + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + //Get a list of all fibonacci numbers < 4,000,000 + let fibNums = myClasses::Algorithms::getAllFib(3_999_999); + let mut sum = 0; + //Set through every element in the list checking if it is even + for num in fibNums{ + //If the number is even add it to the running tally + if((num % 2) == 0){ + sum += num; + } + } + timer.stop(); + + //Return the answer + return Answer::new("The sum of all even fibonacci numbers <= 3999999 is ".to_string() + &sum.to_string(), timer.getString()); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..b5effc3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,171 @@ +#![allow(non_snake_case)] +#![allow(unused_parens)] +#![allow(non_upper_case_globals)] + + +mod Problems; + + +#[derive(PartialEq)] +enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} + +static problemNumbers: [u32; 3] = [0, 1, 2]; + +fn main(){ + let mut selection = Selections::EMPTY; + while(selection != Selections::EXIT){ + printMenu(); + selection = getMenuSelection(); + + if(selection == Selections::SOLVE){ + solveMenu(); + } + else if(selection == Selections::DESCRIPTION){ + descriptionMenu(); + } + else if(selection == Selections::LIST){ + listProblems(); + } + else if(selection == Selections::EXIT){ + + } + else{ + printErrorMessage(); + } + } +} + +fn printMenu(){ + println!("{}. Solve a problem", Selections::SOLVE as i32); + println!("{}. Print a problem description", Selections::DESCRIPTION as i32); + println!("{}. List valid problem numbers", Selections::LIST as i32); + println!("{}. Exit\n", Selections::EXIT as i32); +} + +fn getMenuSelection() -> Selections{ + //Setup reading from the console + let mut selectionStr = String::new(); + + std::io::stdin().read_line(&mut selectionStr).ok(); + //Convert the read to an int + let mut selection = selectionStr.trim().parse::().unwrap(); + while(!isValidMenu(selection)){ + println!("That is an invalid option!"); + printMenu(); + std::io::stdin().read_line(&mut selectionStr).ok(); + selection = selectionStr.trim().parse::().unwrap(); + } + return getSelection(selection); +} +fn isValidMenu(selection: i32) -> bool{ + if((selection > 0) && (selection < Selections::SIZE as i32)){ + return true; + } + else{ + return false; + } +} +fn getSelection(selection: i32) -> Selections{ + if(selection == Selections::SOLVE as i32){ + return Selections::SOLVE; + } + else if(selection == Selections::DESCRIPTION as i32){ + return Selections::DESCRIPTION; + } + else if(selection == Selections::LIST as i32){ + return Selections::LIST; + } + else if(selection == Selections::EXIT as i32){ + return Selections::EXIT; + } + else{ + return Selections::EMPTY; + } +} +fn printErrorMessage(){ + println!("That is an invalid selection!"); +} +fn solveMenu(){ + let problemNumber = getProblemNumber(); + println!("\n\n"); + //This selection solves all problems in order + if(problemNumber == 0){ + //Solve to every valid problem number + for problemLocation in 1..problemNumbers.len() as u32{ + //Solve the problems + print!("{}. ", problemNumbers[problemLocation as usize]); + solveProblem(problemNumbers[problemLocation as usize]); + println!("\n\n"); + } + } + //This is if a single problem number was chosen + else{ + //Solve the problem + solveProblem(problemNumber); + } + println!("\n\n"); +} +fn solveProblem(problemNumber: u32){ + if(problemNumber == 1){ + println!("{}", Problems::Problem1::getDescription()); + println!("{}", Problems::Problem1::solve()); + } + else if(problemNumber == 2){ + println!("{}", Problems::Problem2::getDescription()); + println!("{}", Problems::Problem2::solve()); + } +} +fn descriptionMenu(){ + //Give some extra space to print the description + println!("\n\n"); + + //Get the problem number + let problemNumber = getProblemNumber(); + + //If the problem number is 0 then print out all the descriptions + if(problemNumber == 0){ + //Print decription for every valid problem number + for problemLocation in 1..problemNumbers.len(){ + //Print the problem's description + print!("{}. ", problemNumbers[problemLocation]); + printDescription(problemNumbers[problemLocation]); + println!(); + } + } + //Otherwise print out a single problem's description + else{ + printDescription(problemNumber); + } + println!("\n\n"); +} +fn printDescription(problemNumber: u32){ + if(problemNumber == 1){ + println!("{}", Problems::Problem1::getDescription()); + } + else if(problemNumber == 2){ + println!("{}", Problems::Problem2::getDescription()); + } +} +fn getProblemNumber() -> u32{ + println!("Enter a problem number: "); + //Setup reading from the console + let mut problemNumberStr = String::new(); + std::io::stdin().read_line(&mut problemNumberStr).ok(); + //Convert the read to an int + let mut problemNumber = problemNumberStr.trim().parse::().unwrap(); + + //Loop through all valid problem numbers and see if the one entered matches it + while(!problemNumbers.contains(&problemNumber)){ + println!("That is an invalid problem number!\nEnter a problem number: "); + std::io::stdin().read_line(&mut problemNumberStr).ok(); + problemNumber = problemNumberStr.trim().parse::().unwrap(); + } + return problemNumber; +} +fn listProblems(){ + print!("Problems: {}", problemNumbers[1]); + for problemNumber in 2..problemNumbers.len(){ + println!(", {}", problemNumbers[problemNumber]) + } + println!(); +}