Added problem 3

This commit is contained in:
2020-06-13 16:28:07 -04:00
parent 58c966711d
commit 9c16d61a09
7 changed files with 141 additions and 5 deletions

View File

@@ -7,4 +7,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
myClasses = {path = "D:/Programs/Rust/myClasses"} myClasses = {path = "/home/matthew/Programs/Rust/RustClasses"}
num = "0.2.1"

View File

@@ -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 Answer;
pub mod Problem1; pub mod Problem1;
pub mod Problem2; pub mod Problem2;
pub mod Problem3;

View File

@@ -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 //A structure to hold the answer to the problem
pub struct Answer{ pub struct Answer{
result: String, result: String,

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
extern crate myClasses;
use crate::Problems::Answer::Answer;
pub use crate::Problems::Answer::Answer;
pub fn getDescription() -> &'static str{ pub fn getDescription() -> &'static str{
"What is the sum of all the multiples of 3 or 5 that are less than 1000" "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 the answer
return Answer::new("The sum of all numbers < 1000 is ".to_string() + &fullSum.to_string(), timer.getString()); return Answer::new("The sum of all numbers < 1000 is ".to_string() + &fullSum.to_string(), timer.getString());
} }
/* Results:
*/

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
extern crate myClasses; extern crate myClasses;
use crate::Problems::Answer::Answer; use crate::Problems::Answer::Answer;
pub fn getDescription() -> &'static str{ pub fn getDescription() -> &'static str{
@@ -25,3 +47,7 @@ pub fn solve() -> Answer{
//Return the answer //Return the answer
return Answer::new("The sum of all even fibonacci numbers <= 3999999 is ".to_string() + &sum.to_string(), timer.getString()); return Answer::new("The sum of all even fibonacci numbers <= 3999999 is ".to_string() + &sum.to_string(), timer.getString());
} }
/* Results:
*/

55
src/Problems/Problem3.rs Normal file
View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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::<num::BigInt>().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:
*/

View File

@@ -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(non_snake_case)]
#![allow(unused_parens)] #![allow(unused_parens)]
#![allow(non_upper_case_globals)] #![allow(non_upper_case_globals)]
@@ -9,7 +16,7 @@ mod Problems;
#[derive(PartialEq)] #[derive(PartialEq)]
enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} 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(){ fn main(){
let mut selection = Selections::EMPTY; let mut selection = Selections::EMPTY;
@@ -114,6 +121,10 @@ fn solveProblem(problemNumber: u32){
println!("{}", Problems::Problem2::getDescription()); println!("{}", Problems::Problem2::getDescription());
println!("{}", Problems::Problem2::solve()); println!("{}", Problems::Problem2::solve());
} }
else if(problemNumber == 3){
println!("{}", Problems::Problem3::getDescription());
println!("{}", Problems::Problem3::solve());
}
} }
fn descriptionMenu(){ fn descriptionMenu(){
//Give some extra space to print the description //Give some extra space to print the description
@@ -145,6 +156,9 @@ fn printDescription(problemNumber: u32){
else if(problemNumber == 2){ else if(problemNumber == 2){
println!("{}", Problems::Problem2::getDescription()); println!("{}", Problems::Problem2::getDescription());
} }
else if(problemNumber == 3){
println!("{}", Problems::Problem3::getDescription());
}
} }
fn getProblemNumber() -> u32{ fn getProblemNumber() -> u32{
println!("Enter a problem number: "); println!("Enter a problem number: ");