diff --git a/Cargo.toml b/Cargo.toml index a391134..d3166cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,5 @@ 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"} +myClasses = {path = "/home/matthew/Programs/Rust/RustClasses"} +num = "0.2.1" diff --git a/src/Problems.rs b/src/Problems.rs index 81f9dad..685288d 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -1,3 +1,11 @@ +//ProjectEulerRust/src/Problems.rs +//Matthew Ellison +// Created: 06-11-20 +//Modified: 06-13-20 +//This file just forwards all the problem modules + + pub mod Answer; pub mod Problem1; pub mod Problem2; +pub mod Problem3; diff --git a/src/Problems/Answer.rs b/src/Problems/Answer.rs index e901f8f..72c4264 100644 --- a/src/Problems/Answer.rs +++ b/src/Problems/Answer.rs @@ -1,3 +1,7 @@ +//ProjectEulerRust/src/Problems/Answer.rs +//Matthew Ellison +// Created: 06-11-20 +//Modified: 06-13-20 //A structure to hold the answer to the problem pub struct Answer{ result: String, diff --git a/src/Problems/Problem1.rs b/src/Problems/Problem1.rs index 586d931..6dea9f0 100644 --- a/src/Problems/Problem1.rs +++ b/src/Problems/Problem1.rs @@ -1,6 +1,30 @@ -extern crate myClasses; +//ProjectEulerRust/src/Problems/Problems1.rs +//Matthew Ellison +// Created: 06-12-20 +//Modified: 06-13-20 +//What is the sum of all the multiples of 3 or 5 that are less than 1000 +//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses +/* + 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 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" @@ -25,3 +49,7 @@ pub fn solve() -> Answer{ //Return the answer return Answer::new("The sum of all numbers < 1000 is ".to_string() + &fullSum.to_string(), timer.getString()); } + +/* Results: + +*/ diff --git a/src/Problems/Problem2.rs b/src/Problems/Problem2.rs index 1b6c3d9..1373c97 100644 --- a/src/Problems/Problem2.rs +++ b/src/Problems/Problem2.rs @@ -1,6 +1,28 @@ +//ProjectEulerRust/src/Problems/Problems2.rs +//Matthew Ellison +// Created: 06-12-20 +//Modified: 06-13-20 +//The sum of the even Fibonacci numbers less than 4,000,000 +//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() -> &'static str{ @@ -25,3 +47,7 @@ pub fn solve() -> Answer{ //Return the answer return Answer::new("The sum of all even fibonacci numbers <= 3999999 is ".to_string() + &sum.to_string(), timer.getString()); } + +/* Results: + +*/ diff --git a/src/Problems/Problem3.rs b/src/Problems/Problem3.rs new file mode 100644 index 0000000..10429c7 --- /dev/null +++ b/src/Problems/Problem3.rs @@ -0,0 +1,55 @@ +//ProjectEulerRust/src/Problems/Problems3.rs +//Matthew Ellison +// Created: 06-13-20 +//Modified: 06-13-20 +//The largest prime factor of 600851475143 +//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; + +extern crate num; + + +pub fn getDescription() -> String{ + "What is the largest prime factor of 600851475143?".to_string() +} + +pub fn solve() -> Answer{ + //Setup the number + let number = "600851475143".parse::().unwrap(); + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Get all the factors of the number + let factors = myClasses::Algorithms::getFactorsBig(num::BigInt::new(number.sign(), number.to_u32_digits().1)); + //The last element should be the largest factor + + //Stop the timer + timer.stop(); + + //Save the results + return Answer::new(format!("The largest factor of the number {} is {}", number, factors.last().unwrap()), timer.getString()); +} + +/* Results: + +*/ diff --git a/src/main.rs b/src/main.rs index b5effc3..3489ccd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,10 @@ +//ProjectEulerRust/src/main.rs +//Matthew Ellison +// Created: 06-11-20 +//Modified: 06-11-20 +//This is a driver function for the Project Euler solutions in Rust + + #![allow(non_snake_case)] #![allow(unused_parens)] #![allow(non_upper_case_globals)] @@ -9,7 +16,7 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 3] = [0, 1, 2]; +static problemNumbers: [u32; 4] = [0, 1, 2, 3]; fn main(){ let mut selection = Selections::EMPTY; @@ -114,6 +121,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem2::getDescription()); println!("{}", Problems::Problem2::solve()); } + else if(problemNumber == 3){ + println!("{}", Problems::Problem3::getDescription()); + println!("{}", Problems::Problem3::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -145,6 +156,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 2){ println!("{}", Problems::Problem2::getDescription()); } + else if(problemNumber == 3){ + println!("{}", Problems::Problem3::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");