diff --git a/src/Problems.rs b/src/Problems.rs index 058470e..deeba76 100644 --- a/src/Problems.rs +++ b/src/Problems.rs @@ -49,3 +49,4 @@ pub mod Problem23; pub mod Problem24; pub mod Problem25; pub mod Problem26; +pub mod Problem27; diff --git a/src/Problems/Problem27.rs b/src/Problems/Problem27.rs new file mode 100644 index 0000000..f7f3b4b --- /dev/null +++ b/src/Problems/Problem27.rs @@ -0,0 +1,76 @@ +//ProjectEulerRust/src/Problems/Problems27.rs +//Matthew Ellison +// Created: 06-17-20 +//Modified: 06-17-20 +//Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0. +//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 product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0".to_string() +} + +pub fn solve() -> Answer{ + let mut topA = 0; //The A for the most n's generated + let mut topB = 0; //The B for the most n's generated + let mut topN = 0; //The most n's generated + let primes = myClasses::Algorithms::getPrimes(12000); //A list of all primes that could possible be generated with this formula + + //Start the timer + let mut timer = myClasses::Stopwatch::Stopwatch::new(); + timer.start(); + + //Start with the lowest possible A and check all possibilities after that + for a in -999..=999{ + //Start with the lowest possible B and check all possibilities after that + for b in -1000..=1000{ + //Start with n=0 and check the formula to see how many primes you can get with concecutive n's + let mut n = 0; + let mut quadratic = (n * n) + (a * n) + b; + while(primes.contains(&quadratic)){ + n += 1; + quadratic = (n * n) + (a * n) + b; + } + n -= 1; //Remove an n because the last formula failed + + //Set all the largest number if this created more primes than any other + if(n > topN){ + topN = n; + topB = b; + topA = a; + } + } + } + + //Stop the timer + timer.stop(); + + //Return the results + return Answer::new(format!("The greatest number of primes found is {}\nIt was found with A = {}, B = {}\nTHe product of A and B is {}", topN, topA, topB, topA * topB), timer.getString()); +} + +/* Results: +The greatest number of primes found is 70 +It was found with A = -61, B = 971 +THe product of A and B is -59231 +It took 2.100 seconds to solve this problem +*/ diff --git a/src/main.rs b/src/main.rs index 478a2c2..e3d2277 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,9 +33,9 @@ mod Problems; #[derive(PartialEq)] enum Selections{EMPTY, SOLVE, DESCRIPTION, LIST, EXIT, SIZE} -static problemNumbers: [u32; 27] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +static problemNumbers: [u32; 28] = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, - 20, 21, 22, 23, 24, 25, 26]; + 20, 21, 22, 23, 24, 25, 26, 27]; fn main(){ let mut selection = Selections::EMPTY; @@ -236,6 +236,10 @@ fn solveProblem(problemNumber: u32){ println!("{}", Problems::Problem26::getDescription()); println!("{}", Problems::Problem26::solve()); } + else if(problemNumber == 27){ + println!("{}", Problems::Problem27::getDescription()); + println!("{}", Problems::Problem27::solve()); + } } fn descriptionMenu(){ //Give some extra space to print the description @@ -339,6 +343,9 @@ fn printDescription(problemNumber: u32){ else if(problemNumber == 26){ println!("{}", Problems::Problem26::getDescription()); } + else if(problemNumber == 27){ + println!("{}", Problems::Problem27::getDescription()); + } } fn getProblemNumber() -> u32{ println!("Enter a problem number: ");