mirror of
https://bitbucket.org/Mattrixwv/projecteulerrust.git
synced 2025-12-06 17:43:58 -05:00
Initial commit with a few problems
This commit is contained in:
8
.gitignore
vendored
Normal file
8
.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#vscode
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
#Executables
|
||||||
|
target/
|
||||||
|
|
||||||
|
#Cargo extra files
|
||||||
|
Cargo.lock
|
||||||
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "ProjectEulerRust"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Mattrixwv <m_ellison@ymail.com>"]
|
||||||
|
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"}
|
||||||
3
src/Problems.rs
Normal file
3
src/Problems.rs
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
pub mod Answer;
|
||||||
|
pub mod Problem1;
|
||||||
|
pub mod Problem2;
|
||||||
15
src/Problems/Answer.rs
Normal file
15
src/Problems/Answer.rs
Normal file
@@ -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}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
src/Problems/Problem1.rs
Normal file
27
src/Problems/Problem1.rs
Normal file
@@ -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());
|
||||||
|
}
|
||||||
27
src/Problems/Problem2.rs
Normal file
27
src/Problems/Problem2.rs
Normal file
@@ -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());
|
||||||
|
}
|
||||||
171
src/main.rs
Normal file
171
src/main.rs
Normal file
@@ -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::<i32>().unwrap();
|
||||||
|
while(!isValidMenu(selection)){
|
||||||
|
println!("That is an invalid option!");
|
||||||
|
printMenu();
|
||||||
|
std::io::stdin().read_line(&mut selectionStr).ok();
|
||||||
|
selection = selectionStr.trim().parse::<i32>().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::<u32>().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::<u32>().unwrap();
|
||||||
|
}
|
||||||
|
return problemNumber;
|
||||||
|
}
|
||||||
|
fn listProblems(){
|
||||||
|
print!("Problems: {}", problemNumbers[1]);
|
||||||
|
for problemNumber in 2..problemNumbers.len(){
|
||||||
|
println!(", {}", problemNumbers[problemNumber])
|
||||||
|
}
|
||||||
|
println!();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user