Added solution to problem 37

This commit is contained in:
2021-07-02 00:47:16 -04:00
parent 727699a960
commit 85012be5e8
3 changed files with 163 additions and 2 deletions

View File

@@ -36,7 +36,7 @@ public class ProblemSelection{
public static final ArrayList<Integer> PROBLEM_NUMBERS = new ArrayList<Integer>(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, 67));
31, 32, 33, 34, 35, 36, 37, 67));
//Returns the problem corresponding to the given problem number
public static Problem getProblem(Integer problemNumber){
@@ -78,6 +78,7 @@ public class ProblemSelection{
case 34 : problem = new Problem34(); break;
case 35 : problem = new Problem35(); break;
case 36 : problem = new Problem36(); break;
case 37 : problem = new Problem37(); break;
case 67 : problem = new Problem67(); break;
}
return problem;

View File

@@ -1,4 +1,4 @@
//ProjectEulerJava/str/main/java/mattrixwv/ProjectEuler/Problems/Problem36.java
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem36.java
//Matthew Ellison
// Created: 06-29-21
//Modified: 06-29-21

View File

@@ -0,0 +1,160 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem37.java
//Matthew Ellison
// Created: 07-01-21
//Modified: 07-01-21
//Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted).
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2021 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 mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.SieveOfEratosthenes;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem37 extends Problem{
//Variables
//Static variables
private static final long LAST_PRIME_BEFORE_CHECK = 7; //The last prime before 11 since single digit primes aren't checked
//Instance variables
private ArrayList<Long> truncPrimes; //All numbers that are truncatable primes
private long sum; //The sum of all elements in truncPrimes
//Functions
//Constructor
public Problem37(){
super("Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted).");
truncPrimes = new ArrayList<Long>();
sum = 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();
//Create the sieve and get the first prime number
SieveOfEratosthenes sieve = new SieveOfEratosthenes();
long currentPrime = sieve.next();
//Loop through the sieve until you get to LAST_PRIME_BEFORE_CHECK
while(currentPrime < LAST_PRIME_BEFORE_CHECK){
currentPrime = sieve.next();
}
//Loop until truncPrimes contains 11 elements
while(truncPrimes.size() < 11){
boolean isTruncPrime = true;
//Get the next prime
currentPrime = sieve.next();
//Convert the prime to a string
String primeString = Long.toString(currentPrime);
//If the string contains an even digit move to the next prime
for(int strLoc = 0;(strLoc < primeString.length()) && (isTruncPrime);++strLoc){
//Allow 2 to be the first digit
if((strLoc == 0) && (primeString.charAt(strLoc) == '2')){
continue;
}
switch(primeString.charAt(strLoc)){
case '0' :
case '2' :
case '4' :
case '6' :
case '8' : isTruncPrime = false; break;
}
}
//Start removing digits from the left and see if the number stays prime
if(isTruncPrime){
for(int truncLoc = 1;truncLoc < primeString.length();++truncLoc){
//Create a substring of the prime, removing the needed digits from the left
String primeSubstring = primeString.substring(truncLoc);
//Convert the string to an int and see if the number is still prime
long newPrime = Long.valueOf(primeSubstring);
if(!Algorithms.isPrime(newPrime)){
isTruncPrime = false;
break;
}
}
}
//Start removing digits from the right and see if the number stays prime
if(isTruncPrime){
for(int truncLoc = 1;truncLoc < primeString.length();++truncLoc){
//Create a substring of the prime, removing the needed digits from the right
String primeSubstring = primeString.substring(0, primeString.length() - truncLoc);
//Convert the string to an int and see if the number is still prime
long newPrime = Long.valueOf(primeSubstring);
if(!Algorithms.isPrime(newPrime)){
isTruncPrime = false;
break;
}
}
}
//If the number remained prime through all operations add it to the vector
if(isTruncPrime){
truncPrimes.add(currentPrime);
}
}
//Get the sum of all elements in the truncPrimes vector
sum = Algorithms.getLongSum(truncPrimes);
//Stop the timer
timer.stop();
//Throw a flag to show the porblem is solved
solved = true;
}
//Reset the problem so it can be run again
public void reset(){
super.reset();
truncPrimes.clear();
sum = 0;
}
//Gets
//Returns a string with the solution to the problem
public String getResult(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
return String.format("The sum of all left and right truncatable primes is %d", sum);
}
//Returns the list of primes that can be truncated
public ArrayList<Long> getTruncatablePrimes(){
if(!solved){
throw new Unsolved();
}
return truncPrimes;
}
//Return the sum of all primes in truncPrimes
public long getSumOfPrimes(){
if(!solved){
throw new Unsolved();
}
return sum;
}
}
/* Results:
The sum of all left and right truncatable primes is 748317
It took an average of 103.829 milliseconds to run this problem through 100 iterations
*/