mirror of
https://bitbucket.org/Mattrixwv/projecteulercs.git
synced 2026-02-03 19:52:36 -05:00
Added solution to problem 27
This commit is contained in:
@@ -32,7 +32,7 @@ namespace ProjectEulerCS{
|
||||
private static readonly List<int> _PROBLEM_NUMBERS = new List<int>()
|
||||
{ 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, 67};
|
||||
20, 21, 22, 23, 24, 25, 26, 27, 67};
|
||||
public static System.Collections.Generic.List<int> PROBLEM_NUMBERS{
|
||||
get { return _PROBLEM_NUMBERS; }
|
||||
}
|
||||
@@ -67,6 +67,7 @@ namespace ProjectEulerCS{
|
||||
case 24: problem = new Problem24(); break;
|
||||
case 25: problem = new Problem25(); break;
|
||||
case 26: problem = new Problem26(); break;
|
||||
case 27: problem = new Problem27(); break;
|
||||
case 67: problem = new Problem67(); break;
|
||||
}
|
||||
return problem;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//Matthew Ellison
|
||||
// Created: 09-11-20
|
||||
//Modified: 09-11-20
|
||||
//What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
|
||||
//Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/CSClasses
|
||||
/*
|
||||
Copyright (C) 2020 Matthew Ellison
|
||||
|
||||
138
ProjectEulerCS/Problems/Problem27.cs
Normal file
138
ProjectEulerCS/Problems/Problem27.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
//ProjectEuler/ProjectEulerCS/src/Problems/Problem27.cs
|
||||
//Matthew Ellison
|
||||
// Created: 09-11-20
|
||||
//Modified: 09-11-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/CSClasses
|
||||
/*
|
||||
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/>.
|
||||
*/
|
||||
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
|
||||
namespace ProjectEulerCS.Problems{
|
||||
public class Problem27 : Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private const int LARGEST_POSSIBLE_A = 999;
|
||||
private const int LARGEST_POSSIBLE_B = 1000;
|
||||
//Instance variables
|
||||
private int topA; //The A for the most n's generated
|
||||
private int topB; //THe B for the most n's generated
|
||||
private int topN; //The most n's generated
|
||||
private List<int> primes; //A list of all primes that could possibly be generated with this formula
|
||||
public int TOP_A{
|
||||
get{
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return topA;
|
||||
}
|
||||
}
|
||||
public int TOP_B{
|
||||
get{
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return topB;
|
||||
}
|
||||
}
|
||||
public int TOP_N{
|
||||
get{
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return topN;
|
||||
}
|
||||
}
|
||||
public override string Result{
|
||||
get{
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return $"The greatest number of primes found is {topN}\nIt was found with A = {topA}, B = {topB}\nThe product of A and B is {topA * topB}";
|
||||
}
|
||||
}
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem27() : base($"Find the product of the coefficients, |a| <= {LARGEST_POSSIBLE_A} and |b| <= {LARGEST_POSSIBLE_B}, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0"){
|
||||
topA = 0;
|
||||
topB = 0;
|
||||
topN = 0;
|
||||
primes = new List<int>();
|
||||
}
|
||||
//Operational functions
|
||||
//Solve the problem
|
||||
public override void Solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
return;
|
||||
}
|
||||
|
||||
//Start the timer
|
||||
timer.Start();
|
||||
|
||||
//Get the primes
|
||||
primes = mee.Algorithms.GetPrimes(12000);
|
||||
|
||||
//Start with the lowest possible A and check all possibilities after that
|
||||
for(int a = -LARGEST_POSSIBLE_A;a <= LARGEST_POSSIBLE_A;++a){
|
||||
//Start with the lowest possible B and check all possibilities after that
|
||||
for(int b = -LARGEST_POSSIBLE_B;b <= LARGEST_POSSIBLE_B;++b){
|
||||
//Start with n=0 and check the formula to see how many primes you can get with concecutive n's
|
||||
int n = 0;
|
||||
int quadratic = (n * n) + (a * n) + b;
|
||||
while(primes.Contains(quadratic)){
|
||||
++n;
|
||||
quadratic = (n * n) + (a * n) + b;
|
||||
}
|
||||
--n; //Negate an n because the last formula failed'
|
||||
|
||||
//Set all the largest numbers if this created more primes than any other
|
||||
if(n > topN){
|
||||
topN = n;
|
||||
topB = b;
|
||||
topA = a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Top the timer
|
||||
timer.Stop();
|
||||
|
||||
//Throw a flag to show the problem is solved
|
||||
solved = true;
|
||||
}
|
||||
//Reset the problem so it can be run again
|
||||
public override void Reset(){
|
||||
base.Reset();
|
||||
topA = 0;
|
||||
topB = 0;
|
||||
topN = 0;
|
||||
primes.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Result:
|
||||
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 an average of 1.394 seconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user