mirror of
https://bitbucket.org/Mattrixwv/projecteulerrust.git
synced 2025-12-06 17:43:58 -05:00
Added solution for problem 27
This commit is contained in:
@@ -49,3 +49,4 @@ pub mod Problem23;
|
|||||||
pub mod Problem24;
|
pub mod Problem24;
|
||||||
pub mod Problem25;
|
pub mod Problem25;
|
||||||
pub mod Problem26;
|
pub mod Problem26;
|
||||||
|
pub mod Problem27;
|
||||||
|
|||||||
76
src/Problems/Problem27.rs
Normal file
76
src/Problems/Problem27.rs
Normal file
@@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
*/
|
||||||
11
src/main.rs
11
src/main.rs
@@ -33,9 +33,9 @@ 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; 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,
|
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(){
|
fn main(){
|
||||||
let mut selection = Selections::EMPTY;
|
let mut selection = Selections::EMPTY;
|
||||||
@@ -236,6 +236,10 @@ fn solveProblem(problemNumber: u32){
|
|||||||
println!("{}", Problems::Problem26::getDescription());
|
println!("{}", Problems::Problem26::getDescription());
|
||||||
println!("{}", Problems::Problem26::solve());
|
println!("{}", Problems::Problem26::solve());
|
||||||
}
|
}
|
||||||
|
else if(problemNumber == 27){
|
||||||
|
println!("{}", Problems::Problem27::getDescription());
|
||||||
|
println!("{}", Problems::Problem27::solve());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn descriptionMenu(){
|
fn descriptionMenu(){
|
||||||
//Give some extra space to print the description
|
//Give some extra space to print the description
|
||||||
@@ -339,6 +343,9 @@ fn printDescription(problemNumber: u32){
|
|||||||
else if(problemNumber == 26){
|
else if(problemNumber == 26){
|
||||||
println!("{}", Problems::Problem26::getDescription());
|
println!("{}", Problems::Problem26::getDescription());
|
||||||
}
|
}
|
||||||
|
else if(problemNumber == 27){
|
||||||
|
println!("{}", Problems::Problem27::getDescription());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fn getProblemNumber() -> u32{
|
fn getProblemNumber() -> u32{
|
||||||
println!("Enter a problem number: ");
|
println!("Enter a problem number: ");
|
||||||
|
|||||||
Reference in New Issue
Block a user