mirror of
https://bitbucket.org/Mattrixwv/projecteulerrust.git
synced 2025-12-06 17:43:58 -05:00
Added problem 3
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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{
|
||||
"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:
|
||||
|
||||
*/
|
||||
|
||||
@@ -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;
|
||||
|
||||
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:
|
||||
|
||||
*/
|
||||
|
||||
55
src/Problems/Problem3.rs
Normal file
55
src/Problems/Problem3.rs
Normal 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:
|
||||
|
||||
*/
|
||||
16
src/main.rs
16
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: ");
|
||||
|
||||
Reference in New Issue
Block a user