From 0ef1be37de6d929f5763367b5f00ef7d3ea2e701 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 16 Jun 2020 01:24:30 -0400 Subject: [PATCH] Added solution to problem 10 --- src/Problems.rs | 1 + src/Problems/Problem10.rs | 52 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 9 ++++++- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/Problems/Problem10.rs diff --git a/src/Problems.rs b/src/Problems.rs index f445fe7..fda6476 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -32,3 +32,4 @@ pub mod Problem6; pub mod Problem7; pub mod Problem8; pub mod Problem9; +pub mod Problem10; diff --git a/src/Problems/Problem10.rs b/src/Problems/Problem10.rs new file mode 100644 index 0000000..efd54f1 --- /dev/null +++ b/src/Problems/Problem10.rs @@ -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 . +*/ + + +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 +*/ diff --git a/src/main.rs b/src/main.rs index fdc6c34..cfb69d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ mod Problems; #[derive(PartialEq)] 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(){ let mut selection = Selections::EMPTY; @@ -166,6 +166,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem9::getDescription()); println!("{}", Problems::Problem9::solve()); } + else if(problemNumber == 10){ + println!("{}", Problems::Problem10::getDescription()); + println!("{}", Problems::Problem10::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -218,6 +222,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 9){ println!("{}", Problems::Problem9::getDescription()); } + else if(problemNumber == 10){ + println!("{}", Problems::Problem10::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");