From 696609744558c165261d46bbca99635f42bcfc76 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Mon, 15 Jun 2020 14:43:56 -0400 Subject: [PATCH] Added solution to problem 7 --- src/Problems.rs | 1 + src/Problems/Problem7.rs | 52 ++++++++++++++++++++++++++++++++++++++++ src/main.rs | 9 ++++++- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/Problems/Problem7.rs diff --git a/src/Problems.rs b/src/Problems.rs index 9f02bd9..489324e 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -12,3 +12,4 @@ pub mod Problem3; pub mod Problem4; pub mod Problem5; pub mod Problem6; +pub mod Problem7; diff --git a/src/Problems/Problem7.rs b/src/Problems/Problem7.rs new file mode 100644 index 0000000..ec34664 --- /dev/null +++ b/src/Problems/Problem7.rs @@ -0,0 +1,52 @@ +//ProjectEulerRust/src/Problems/Problems7.rs +//Matthew Ellison +// Created: 06-15-20 +//Modified: 06-15-20 +//What is the 10001th prime number? +//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 fn getDescription() -> String{ + "What is the 10001th prime number?".to_string() +} + +pub fn solve() -> Answer{ + let NUMBER_OF_PRIMES = num::BigInt::from(10001); + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Get all the prime numbers + let primes = myClasses::Algorithms::getNumPrimesBig(num::BigInt::new(NUMBER_OF_PRIMES.sign(), NUMBER_OF_PRIMES.to_u32_digits().1)); + + //Stop the timer + timer.stop(); + + //Return the results + return Answer::new(format!("The {}th prime number is {}", NUMBER_OF_PRIMES, primes[primes.len() - 1]), timer.getString()); +} + +/* Results: +The 10001th prime number is 104743 +It took 101.113 milliseconds to solve this problem +*/ diff --git a/src/main.rs b/src/main.rs index cc9cd82..2424847 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 7] = [0, 1, 2, 3, 4, 5, 6]; +static problemNumbers: [u32; 8] = [0, 1, 2, 3, 4, 5, 6, 7]; fn main(){ let mut selection = Selections::EMPTY; @@ -137,6 +137,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem6::getDescription()); println!("{}", Problems::Problem6::solve()); } + else if(problemNumber == 7){ + println!("{}", Problems::Problem7::getDescription()); + println!("{}", Problems::Problem7::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -180,6 +184,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 6){ println!("{}", Problems::Problem6::getDescription()); } + else if(problemNumber == 7){ + println!("{}", Problems::Problem7::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");