mirror of
https://bitbucket.org/Mattrixwv/projecteulerrust.git
synced 2025-12-06 17:43:58 -05:00
Added solution to problem 10
This commit is contained in:
@@ -32,3 +32,4 @@ pub mod Problem6;
|
|||||||
pub mod Problem7;
|
pub mod Problem7;
|
||||||
pub mod Problem8;
|
pub mod Problem8;
|
||||||
pub mod Problem9;
|
pub mod Problem9;
|
||||||
|
pub mod Problem10;
|
||||||
|
|||||||
52
src/Problems/Problem10.rs
Normal file
52
src/Problems/Problem10.rs
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
//ProjectEulerRust/src/Problems/Problems10.rs
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 06-15-20
|
||||||
|
//Modified: 06-15-20
|
||||||
|
//Find the sum of all the primes below two million
|
||||||
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/RustClasses
|
||||||
|
/*
|
||||||
|
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 fn getDescription() -> String{
|
||||||
|
"Find the sum of all the primes below two million".to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn solve() -> Answer{
|
||||||
|
let GOAL_NUMBER = 2_000_000;
|
||||||
|
//Start the timer
|
||||||
|
let mut timer = myClasses::Stopwatch::Stopwatch::new();
|
||||||
|
timer.start();
|
||||||
|
|
||||||
|
//Get the sum of all prime numbers < GOAL_NUMBER
|
||||||
|
let sum: i64 = myClasses::Algorithms::getPrimes(GOAL_NUMBER - 1).iter().sum();
|
||||||
|
|
||||||
|
//Stop the timer
|
||||||
|
timer.stop();
|
||||||
|
|
||||||
|
//Return the results
|
||||||
|
return Answer::new(format!("The sum of all the primes < {} is {}", GOAL_NUMBER, sum), timer.getString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Results:
|
||||||
|
The sum of all the primes < 2000000 is 142913828922
|
||||||
|
It took 157.704 milliseconds to solve this problem
|
||||||
|
*/
|
||||||
@@ -33,7 +33,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; 10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
static problemNumbers: [u32; 11] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
||||||
|
|
||||||
fn main(){
|
fn main(){
|
||||||
let mut selection = Selections::EMPTY;
|
let mut selection = Selections::EMPTY;
|
||||||
@@ -166,6 +166,10 @@ fn solveProblem(problemNumber: u32){
|
|||||||
println!("{}", Problems::Problem9::getDescription());
|
println!("{}", Problems::Problem9::getDescription());
|
||||||
println!("{}", Problems::Problem9::solve());
|
println!("{}", Problems::Problem9::solve());
|
||||||
}
|
}
|
||||||
|
else if(problemNumber == 10){
|
||||||
|
println!("{}", Problems::Problem10::getDescription());
|
||||||
|
println!("{}", Problems::Problem10::solve());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn descriptionMenu(){
|
fn descriptionMenu(){
|
||||||
//Give some extra space to print the description
|
//Give some extra space to print the description
|
||||||
@@ -218,6 +222,9 @@ fn printDescription(problemNumber: u32){
|
|||||||
else if(problemNumber == 9){
|
else if(problemNumber == 9){
|
||||||
println!("{}", Problems::Problem9::getDescription());
|
println!("{}", Problems::Problem9::getDescription());
|
||||||
}
|
}
|
||||||
|
else if(problemNumber == 10){
|
||||||
|
println!("{}", Problems::Problem10::getDescription());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn getProblemNumber() -> u32{
|
fn getProblemNumber() -> u32{
|
||||||
println!("Enter a problem number: ");
|
println!("Enter a problem number: ");
|
||||||
|
|||||||
Reference in New Issue
Block a user