mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Added problems up to 50
This commit is contained in:
@@ -36,10 +36,11 @@ public class ProblemSelection{
|
||||
private static final Scanner input = new Scanner(System.in);
|
||||
//Holds the valid problem numbers
|
||||
public static final List<Integer> PROBLEM_NUMBERS = new ArrayList<>(Arrays.asList( 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, 27, 28, 29, 30,
|
||||
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
41, 42, 43, 44, 45, 67));
|
||||
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
|
||||
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
|
||||
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
|
||||
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
|
||||
67));
|
||||
|
||||
//Returns the problem corresponding to the given problem number
|
||||
public static Problem getProblem(Integer problemNumber){
|
||||
@@ -90,6 +91,11 @@ public class ProblemSelection{
|
||||
case 43 : problem = new Problem43(); break;
|
||||
case 44 : problem = new Problem44(); break;
|
||||
case 45 : problem = new Problem45(); break;
|
||||
case 46 : problem = new Problem46(); break;
|
||||
case 47 : problem = new Problem47(); break;
|
||||
case 48 : problem = new Problem48(); break;
|
||||
case 49 : problem = new Problem49(); break;
|
||||
case 50 : problem = new Problem50(); break;
|
||||
case 67 : problem = new Problem67(); break;
|
||||
}
|
||||
return problem;
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
//ProjectEulerJava/src/main/java/mattrixwv/project_euler/Problems/Problem46.java
|
||||
//Mattrixwv
|
||||
// Created: 08-22-22
|
||||
//Modified: 08-22-22
|
||||
//What is the smallest odd coposite number that cannot be written as the sum of a prime and twice a square?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||
/*
|
||||
Copyright (C) 2022 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/>.
|
||||
*/
|
||||
package com.mattrixwv.project_euler.problems;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import com.mattrixwv.generators.SieveOfEratosthenes;
|
||||
|
||||
|
||||
public class Problem46 extends Problem{
|
||||
//Variables
|
||||
//Instance variables
|
||||
private long num;
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem46(){
|
||||
super("What is the smallest odd composite number that connot be written as the sum of a prime and twice a square?");
|
||||
num = 0;
|
||||
}
|
||||
//Operational functions
|
||||
//Solve the problem
|
||||
public void solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
return;
|
||||
}
|
||||
|
||||
//Start the timer
|
||||
timer.start();
|
||||
|
||||
|
||||
//Setup a prime sieve
|
||||
SieveOfEratosthenes sieve = new SieveOfEratosthenes();
|
||||
ArrayList<Long> primes = new ArrayList<>();
|
||||
primes.add(sieve.next());
|
||||
//Run until we find a composite number
|
||||
for(int currentOdd = 3;num == 0;currentOdd += 2){
|
||||
boolean foundEquation = false;
|
||||
//Add elements to the prime array until you reach a number greater than the current number to test
|
||||
while(primes.get(primes.size() - 1) < currentOdd){
|
||||
primes.add(sieve.next());
|
||||
}
|
||||
//Make sure the number isn't prime
|
||||
if(primes.get(primes.size() - 1) == currentOdd){
|
||||
continue;
|
||||
}
|
||||
//Check if the number can be calculated with the formula
|
||||
//Start with the smallest prime and work your way up
|
||||
for(long prime : primes){
|
||||
//Start with 1 and work your way up until the number becomes too large
|
||||
boolean tooLarge = false;
|
||||
for(int cnt = 1;(!tooLarge) && (!foundEquation);++cnt){
|
||||
//Find the value of the equation for these numbers
|
||||
long equation = prime + (2 * cnt * cnt);
|
||||
//If the number is too large break the loop
|
||||
if(equation > currentOdd){
|
||||
tooLarge = true;
|
||||
}
|
||||
//If the number == equation set a flag to break the loops
|
||||
else if(equation == currentOdd){
|
||||
foundEquation = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!foundEquation){
|
||||
num = currentOdd;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Set a flag to show the problem is solved
|
||||
solved = true;
|
||||
}
|
||||
//Reset the problem so it can be run again
|
||||
@Override
|
||||
public void reset(){
|
||||
super.reset();
|
||||
num = 0;
|
||||
}
|
||||
|
||||
//Gets
|
||||
//Returns a string with the solution to the problem
|
||||
public String getResult(){
|
||||
solvedCheck("results");
|
||||
return String.format("The smallest odd composite that cannot be written as the sum of a prime and twice a square is %d", num);
|
||||
}
|
||||
//Returns the number
|
||||
public long getCompositeNum(){
|
||||
solvedCheck("composite number");
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The smallest odd composite that cannot be written as the sum of a prime and twice a square is 5777
|
||||
It took an average of 5.603 milliseconds to run this problem through 100 iterations
|
||||
*/
|
||||
@@ -0,0 +1,140 @@
|
||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem47.java
|
||||
//Mattrixwv
|
||||
// Created: 08-22-22
|
||||
//Modified: 08-22-22
|
||||
//What is the first of four consecutive integers to have four distinct prime factors each?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||
/*
|
||||
Copyright (C) 2022 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/>.
|
||||
*/
|
||||
package com.mattrixwv.project_euler.problems;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.mattrixwv.NumberAlgorithms;
|
||||
|
||||
|
||||
public class Problem47 extends Problem{
|
||||
//Variables
|
||||
//Instance variables
|
||||
private long num; //The first of the consecutive numbers
|
||||
|
||||
//Function
|
||||
//Constructor
|
||||
public Problem47(){
|
||||
super("What is the first of four consecutive integers to have four distinct prime factors each?");
|
||||
num = 0;
|
||||
}
|
||||
//Operational functions
|
||||
//Solve the problem
|
||||
public void solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
return;
|
||||
}
|
||||
|
||||
//Start the timer
|
||||
timer.start();
|
||||
|
||||
|
||||
//Go through every number
|
||||
HashMap<Long, List<Long>> factors = new HashMap<>();
|
||||
factors.put(1L, NumberAlgorithms.getFactors(1L));
|
||||
factors.put(2L, NumberAlgorithms.getFactors(2L));
|
||||
factors.put(3L, NumberAlgorithms.getFactors(3L));
|
||||
for(long cnt = 1;num == 0;++cnt){
|
||||
boolean found = false;
|
||||
//Get the next set of factors
|
||||
factors.put(cnt + 3, NumberAlgorithms.getFactors(cnt + 3));
|
||||
|
||||
//Make maps of the factors and their powers
|
||||
HashMap<Long, Long> factors0 = new HashMap<>();
|
||||
for(Long factor : factors.get(cnt)){
|
||||
factors0.merge(factor, 1L, Long::sum);
|
||||
}
|
||||
HashMap<Long, Long> factors1 = new HashMap<>();
|
||||
for(Long factor : factors.get(cnt + 1)){
|
||||
factors1.merge(factor, 1L, Long::sum);
|
||||
}
|
||||
HashMap<Long, Long> factors2 = new HashMap<>();
|
||||
for(Long factor : factors.get(cnt + 2)){
|
||||
factors2.merge(factor, 1L, Long::sum);
|
||||
}
|
||||
HashMap<Long, Long> factors3 = new HashMap<>();
|
||||
for(Long factor : factors.get(cnt + 3)){
|
||||
factors3.merge(factor, 1L, Long::sum);
|
||||
}
|
||||
|
||||
//Make sure all of the maps have the correct number of elements
|
||||
if((factors0.keySet().size() != 4) || (factors1.keySet().size() != 4) || (factors2.keySet().size() != 4) || (factors3.keySet().size() != 4)){
|
||||
continue;
|
||||
}
|
||||
|
||||
//Make sure none of the elements match
|
||||
for(Long key : factors0.keySet()){
|
||||
if(factors0.get(key).equals(factors1.get(key)) || factors0.get(key).equals(factors1.get(key)) || factors0.get(key).equals(factors3.get(key))){
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
for(Long key : factors1.keySet()){
|
||||
if(factors1.get(key).equals(factors2.get(key)) || factors1.get(key).equals(factors2.get(key))){
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
for(Long key : factors2.keySet()){
|
||||
if(factors2.get(key).equals(factors3.get(key))){
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found){
|
||||
num = cnt;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Set a flag to show the problem is solved
|
||||
solved = true;
|
||||
}
|
||||
//Reset the problem so it can be run again
|
||||
@Override
|
||||
public void reset(){
|
||||
super.reset();
|
||||
num = 0;
|
||||
}
|
||||
|
||||
//Gets
|
||||
//Returns a string with the solution to the problem
|
||||
public String getResult(){
|
||||
solvedCheck("results");
|
||||
return String.format("The first number is %d", num);
|
||||
}
|
||||
//Returns the number
|
||||
public long getFirstNum(){
|
||||
solvedCheck("first number");
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
||||
/* Results
|
||||
The first number is 134043
|
||||
It took 50.878 seconds to solve this problem.
|
||||
*/
|
||||
@@ -0,0 +1,95 @@
|
||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem48.java
|
||||
//Mattrixwv
|
||||
// Created: 08-23-22
|
||||
//Modified: 08-23-22
|
||||
//Find the last ten digits of the series 1^1 + 2^2 + 3^3 + ... + 1000^1000
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||
/*
|
||||
Copyright (C) 2022 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/>.
|
||||
*/
|
||||
package com.mattrixwv.project_euler.problems;
|
||||
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
|
||||
public class Problem48 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static final BigInteger TOP_NUM = BigInteger.valueOf(1000);
|
||||
//Instance variables
|
||||
private BigInteger sum;
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem48(){
|
||||
super("Find the last ten digits of the series Σn^n for 1<=n<=1000");
|
||||
sum = BigInteger.ZERO;
|
||||
}
|
||||
//Operational functions
|
||||
//Solve the problem
|
||||
public void solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
return;
|
||||
}
|
||||
|
||||
//Start the timer
|
||||
timer.start();
|
||||
|
||||
|
||||
//Start with 1 and execute the formula for each number
|
||||
for(BigInteger cnt = BigInteger.ONE;cnt.compareTo(TOP_NUM) <= 0;cnt = cnt.add(BigInteger.ONE)){
|
||||
sum = sum.add(cnt.pow(cnt.intValue()));
|
||||
}
|
||||
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Set a flag to show the problem is solved
|
||||
solved = true;
|
||||
}
|
||||
//Reset the rpobelm so it can be run again
|
||||
@Override
|
||||
public void reset(){
|
||||
super.reset();
|
||||
sum = BigInteger.ZERO;
|
||||
}
|
||||
|
||||
//Gets
|
||||
//Returns a string with the solution to the problem
|
||||
public String getResult(){
|
||||
solvedCheck("results");
|
||||
return String.format("The last ten digits of the series Σn^n for 1<=n<=1000 is %s", getSumDigits());
|
||||
}
|
||||
//Returns the sum of the series
|
||||
public BigInteger getSum(){
|
||||
solvedCheck("sum");
|
||||
return sum;
|
||||
}
|
||||
//Returns the last 10 digits of the sum
|
||||
public String getSumDigits(){
|
||||
solvedCheck("last 10 digits of the sum");
|
||||
String sumString = sum.toString();
|
||||
return sumString.substring(sumString.length() - 10);
|
||||
}
|
||||
}
|
||||
|
||||
/* Results
|
||||
The last ten digits of the series Σn^n for 1<=n<=1000 is 9110846700
|
||||
It took an average of 3.831 milliseconds to run this problem through 100 iterations
|
||||
*/
|
||||
@@ -0,0 +1,158 @@
|
||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem49.java
|
||||
//Mattrixwv
|
||||
// Created: 08-23-22
|
||||
//Modified: 08-23-22
|
||||
//What is the 12-digit number formed by concatenating the three terms in the sequence?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||
/*
|
||||
Copyright (C) 2022 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/>.
|
||||
*/
|
||||
package com.mattrixwv.project_euler.problems;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
import com.mattrixwv.NumberAlgorithms;
|
||||
import com.mattrixwv.StringAlgorithms;
|
||||
|
||||
|
||||
public class Problem49 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static final int MIN_NUMBER = 1000;
|
||||
private static final int MAX_NUMBER = 9999;
|
||||
//Instance variables
|
||||
private String concatenationOfNumbers;
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem49(){
|
||||
super("What is the 12-digit number formed by concatenating the three terms in the sequence?");
|
||||
concatenationOfNumbers = "";
|
||||
}
|
||||
//Operational functions
|
||||
//Solve the problem
|
||||
public void solve(){
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
return;
|
||||
}
|
||||
|
||||
//Start the timer
|
||||
timer.start();
|
||||
|
||||
|
||||
//Get all of the prime numbers <= MAX_NUMBER
|
||||
List<Integer> primes = NumberAlgorithms.getPrimes(MAX_NUMBER);
|
||||
//Loop through every prime from min to max and check if its permutations match the sequence
|
||||
for(Integer prime : primes){
|
||||
//Skip all primes < MIN
|
||||
if(prime < MIN_NUMBER){
|
||||
continue;
|
||||
}
|
||||
//Get all permutations of the number
|
||||
List<String> primeStrsRaw = StringAlgorithms.getPermutations(prime.toString());
|
||||
HashSet<String> primeStrs = new HashSet<>();
|
||||
for(String str : primeStrsRaw){
|
||||
primeStrs.add(str);
|
||||
}
|
||||
//Skip the provided example
|
||||
if(primeStrs.contains("1487")){
|
||||
continue;
|
||||
}
|
||||
//Remove any even numbers and numbers smaller than the current number
|
||||
ArrayList<Integer> remaining = new ArrayList<>();
|
||||
for(String primeStr : primeStrs){
|
||||
Integer num = Integer.valueOf(primeStr);
|
||||
if(((num % 2) != 0) && (num >= prime)){
|
||||
remaining.add(num);
|
||||
}
|
||||
}
|
||||
//Check if there are at least 3 elements remaining
|
||||
ArrayList<Integer> matches = new ArrayList<>();
|
||||
if(remaining.size() >= 3){
|
||||
//Check if 3 elements are prime
|
||||
for(Integer num : remaining){
|
||||
if(NumberAlgorithms.isPrime(num)){
|
||||
matches.add(num);
|
||||
}
|
||||
}
|
||||
}
|
||||
Collections.sort(matches);
|
||||
//If there are at least 3 matches see if there are any that are equidistant
|
||||
if(matches.size() >= 3){
|
||||
HashSet<Integer> distances = new HashSet<>();
|
||||
for(int cnt1 = 0;cnt1 < matches.size();++cnt1){
|
||||
for(int cnt2 = cnt1 + 1;cnt2 < matches.size();++cnt2){
|
||||
int num = matches.get(cnt2) - matches.get(cnt1);
|
||||
if((MIN_NUMBER + num + num) <= MAX_NUMBER){
|
||||
distances.add(num);
|
||||
}
|
||||
}
|
||||
}
|
||||
for(Integer distance : distances){
|
||||
for(Integer match : matches){
|
||||
if(matches.contains(match + distance) && (matches.contains(match + distance + distance))){
|
||||
concatenationOfNumbers = match.toString() + Integer.toString(match + distance) + Integer.toString(match + distance + distance);
|
||||
}
|
||||
if(!concatenationOfNumbers.isBlank()){
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!concatenationOfNumbers.isBlank()){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!concatenationOfNumbers.isBlank()){
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Set a flag to show the problem is solved
|
||||
solved = true;
|
||||
}
|
||||
//Reset the problem so it can be run again
|
||||
@Override
|
||||
public void reset(){
|
||||
super.reset();
|
||||
concatenationOfNumbers = "";
|
||||
}
|
||||
|
||||
//Gets
|
||||
//Returns a string with the solution to the problem
|
||||
public String getResult(){
|
||||
solvedCheck("results");
|
||||
return String.format("The 12-digit number formed by concatenation the three terms in the sequence is %s", concatenationOfNumbers);
|
||||
}
|
||||
//Returns the concatenation of numbers
|
||||
public String getConcatNums(){
|
||||
solvedCheck("concatenation of numbers");
|
||||
return concatenationOfNumbers;
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The 12-digit number formed by concatenation the three terms in the sequence is 296962999629
|
||||
It took an average of 4.261 milliseconds to run this problem through 100 iterations
|
||||
*/
|
||||
@@ -0,0 +1,144 @@
|
||||
//ProjectEulerJava/src/main/java/com/mattrixwv/project_euler/problems/Problem50.java
|
||||
//Mattrixwv
|
||||
// Created: 08-23-22
|
||||
//Modified: 08-23-22
|
||||
//Which prime, below one-million, can be written as the sum of the most consecutive primes?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||
/*
|
||||
Copyright (C) 2022 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/>.
|
||||
*/
|
||||
package com.mattrixwv.project_euler.problems;
|
||||
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.mattrixwv.ArrayAlgorithms;
|
||||
import com.mattrixwv.NumberAlgorithms;
|
||||
|
||||
|
||||
public class Problem50 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static final long MAX = 999999;
|
||||
//Instance variables
|
||||
private ArrayList<Long> consecutivePrimes;
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem50(){
|
||||
super("Which prime, below one-million, can be written as the sum of the most consecutive primes?");
|
||||
consecutivePrimes = new ArrayList<>();
|
||||
}
|
||||
//Operational functions
|
||||
//Solve the problem
|
||||
public void solve(){
|
||||
//If the porblem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
return;
|
||||
}
|
||||
|
||||
//Start the timer
|
||||
timer.start();
|
||||
|
||||
|
||||
//Get all primes < MAX
|
||||
List<Long> primes = NumberAlgorithms.getPrimes(MAX);
|
||||
//Check every length of consecutive primes for a prime sum. Stop when the length becomes too long
|
||||
boolean tooLong = false;
|
||||
for(long length = 1;!tooLong;++length){
|
||||
//If the length is even you only need to check offset 0 because of 2
|
||||
if((length % 2) == 0){
|
||||
long sum = 0;
|
||||
//Get the sum of consecutive primes
|
||||
for(int cnt = 0;cnt < length;++cnt){
|
||||
sum += primes.get(cnt);
|
||||
}
|
||||
//If the new sum is prime save it
|
||||
if(primes.contains(sum)){
|
||||
consecutivePrimes = new ArrayList<>();
|
||||
for(int cnt = 0;cnt < length;++cnt){
|
||||
consecutivePrimes.add(primes.get(cnt));
|
||||
}
|
||||
}
|
||||
}
|
||||
//If the length is odd you need to check every offset until the sum becomes too large
|
||||
else{
|
||||
//Set the offset (always skipping 2 because this is an odd length sum)
|
||||
for(int start = 1;(start + length) < primes.size();++start){
|
||||
long sum = 0;
|
||||
//Get the sum of consecutive primes
|
||||
for(int cnt = 0;cnt < length;++cnt){
|
||||
sum += primes.get(start + cnt);
|
||||
}
|
||||
//If the sum is too large the offset has gotten too high, so break the loop
|
||||
if(sum > MAX){
|
||||
//If the offset is minimum break the length loop because all subsequent sums will be too large
|
||||
if(start == 1){
|
||||
tooLong = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
//If the new sum is prime save it
|
||||
else if(primes.contains(sum)){
|
||||
consecutivePrimes = new ArrayList<>();
|
||||
for(int cnt = 0;cnt < length;++cnt){
|
||||
consecutivePrimes.add(primes.get(start + cnt));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Set a flag to show the problem is solved
|
||||
solved = true;
|
||||
}
|
||||
//Reset the problem so it can be run again
|
||||
@Override
|
||||
public void reset(){
|
||||
super.reset();
|
||||
consecutivePrimes = new ArrayList<>();
|
||||
}
|
||||
|
||||
//Gets
|
||||
//Returns a string with the solution to the problem
|
||||
public String getResult(){
|
||||
solvedCheck("results");
|
||||
return String.format("The prime below one-million that can be written as the sum of the most consecutive primes is %d", ArrayAlgorithms.getLongSum(consecutivePrimes));
|
||||
}
|
||||
//Returns the list of consecutive primes
|
||||
public ArrayList<Long> getConsecutivePrimes(){
|
||||
solvedCheck("list of consecutive primes");
|
||||
@SuppressWarnings("unchecked")
|
||||
ArrayList<Long> clone = (ArrayList<Long>)consecutivePrimes.clone();
|
||||
return clone;
|
||||
}
|
||||
//Returns the sum of the primes
|
||||
public long getPrime(){
|
||||
solvedCheck("prime");
|
||||
return ArrayAlgorithms.getLongSum(consecutivePrimes);
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The prime below one-million that can be written as the sum of the most consecutive primes is 997651
|
||||
It took an average of 77.425 milliseconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user