Updated for performance

This commit is contained in:
2020-06-17 22:03:47 -04:00
parent 86b10bae89
commit f664a076a6

View File

@@ -1,11 +1,11 @@
//ProjectEuler/Java/Problem27.java //ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem27.java
//Matthew Ellison //Matthew Ellison
// Created: 09-15-19 // Created: 09-15-19
//Modified: 09-15-19 //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. //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/JavaClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/* /*
Copyright (C) 2019 Matthew Ellison Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU Lesser General Public License as published by
@@ -30,11 +30,11 @@ import java.util.ArrayList;
public class Problem27 extends Problem{ public class Problem27 extends Problem{
//The A for the most n's generated //The A for the most n's generated
private Integer topA = 0; private int topA = 0;
//The B for the most n's generated //The B for the most n's generated
private Integer topB = 0; private int topB = 0;
//The most n's generated //The most n's generated
private Integer topN = 0; private int topN = 0;
//A list of all primes that could possibly be generated with this formula //A list of all primes that could possibly be generated with this formula
private ArrayList<Integer> primes = Algorithms.getPrimes(12000); private ArrayList<Integer> primes = Algorithms.getPrimes(12000);
@@ -46,12 +46,12 @@ public class Problem27 extends Problem{
timer.start(); timer.start();
//Start with the lowest possible A and check all possibilities after that //Start with the lowest possible A and check all possibilities after that
for(Integer a = -999;a <= 999;++a){ for(int a = -999;a <= 999;++a){
//Start with the lowest possible B and check all possibilities after that //Start with the lowest possible B and check all possibilities after that
for(Integer b = -1000;b <=1000;++b){ for(int b = -1000;b <=1000;++b){
//Start with n=0 and check the formula to see how many primes you can get get with concecutive n's //Start with n=0 and check the formula to see how many primes you can get with concecutive n's
Integer n = 0; int n = 0;
Integer quadratic = (n * n) + (a * n) + b; int quadratic = (n * n) + (a * n) + b;
while(Algorithms.isFound(primes, quadratic)){ while(Algorithms.isFound(primes, quadratic)){
++n; ++n;
quadratic = (n * n) + (a * n) + b; quadratic = (n * n) + (a * n) + b;
@@ -79,5 +79,5 @@ public class Problem27 extends Problem{
The greatest number of primes found is 70 The greatest number of primes found is 70
It was found with A = -61, B = 971 It was found with A = -61, B = 971
The product of A and B is -59231 The product of A and B is -59231
It took 4.772 seconds to solve this problem. It took 3.184 seconds to solve this problem.
*/ */