mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Added solution for problem 35
This commit is contained in:
@@ -34,7 +34,7 @@ import mattrixwv.exceptions.InvalidResult;
|
||||
public class Benchmark{
|
||||
private static final Scanner input = new Scanner(System.in);
|
||||
private static enum BenchmarkOptions{runSpecific, runAllShort, runAll, exit, size};
|
||||
private static final ArrayList<Integer> tooLong = new ArrayList<Integer>(Arrays.asList(15, 23, 24));
|
||||
private static final ArrayList<Integer> tooLong = new ArrayList<Integer>(Arrays.asList(15, 23, 24, 35));
|
||||
//The driver function for the benchmark selection
|
||||
public static void benchmarkMenu() throws InvalidResult{
|
||||
BenchmarkOptions selection = BenchmarkOptions.size;
|
||||
|
||||
@@ -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, 67));
|
||||
31, 32, 33, 34, 35, 67));
|
||||
|
||||
//Returns the problem corresponding to the given problem number
|
||||
public static Problem getProblem(Integer problemNumber){
|
||||
@@ -76,6 +76,7 @@ public class ProblemSelection{
|
||||
case 32 : problem = new Problem32(); break;
|
||||
case 33 : problem = new Problem33(); break;
|
||||
case 34 : problem = new Problem34(); break;
|
||||
case 35 : problem = new Problem35(); break;
|
||||
case 67 : problem = new Problem67(); break;
|
||||
}
|
||||
return problem;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java
|
||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem34.java
|
||||
//Matthew Ellison
|
||||
// Created: 02-05-21
|
||||
//Modified: 02-07-21
|
||||
|
||||
135
src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java
Normal file
135
src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java
Normal file
@@ -0,0 +1,135 @@
|
||||
//ProjectEulerJava/src/main/java/matrixwv/ProjectEuler/Problems/Problem35.java
|
||||
//Matthew Ellison
|
||||
// Createe: 06-05-21
|
||||
//Modified: 06-05-21
|
||||
//How many circular primes are there below one million?
|
||||
//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.ProjectEuler.Unsolved;
|
||||
|
||||
public class Problem35 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static final int MAX_NUM = 999999; //The largest number that we are checking for primes
|
||||
//Instance variables
|
||||
private ArrayList<Integer> primes; //The primes below MAX_NUM
|
||||
private ArrayList<Integer> circularPrimes; //The circular primes below MAX_NUM
|
||||
//Functions
|
||||
//Returns a list of all rotations of a string passed to it
|
||||
private ArrayList<String> getRotations(String str){
|
||||
ArrayList<String> rotations = new ArrayList<String>();
|
||||
rotations.add(str);
|
||||
for(int cnt = 1;cnt < str.length();++cnt){
|
||||
str = str.substring(1) + str.substring(0, 1);
|
||||
rotations.add(str);
|
||||
}
|
||||
return rotations;
|
||||
}
|
||||
//Constructor
|
||||
public Problem35(){
|
||||
super("How many circular primes are there below one million?");
|
||||
primes = new ArrayList<Integer>();
|
||||
circularPrimes = new ArrayList<Integer>();
|
||||
}
|
||||
//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 primes under 1,000,000
|
||||
primes = Algorithms.getPrimes(MAX_NUM);
|
||||
//Go through all primes, get all their rotations, and check if those numbers are also primes
|
||||
for(int prime : primes){
|
||||
boolean allRotationsPrime = true;
|
||||
//Get all of the rotations of the prime and see if they are also prime
|
||||
ArrayList<String> rotations = getRotations(Integer.toString(prime));
|
||||
for(String rotation : rotations){
|
||||
int p = Integer.valueOf(rotation);
|
||||
if(!primes.contains(p)){
|
||||
allRotationsPrime = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
//If all rotations are prime add it to the list of circular primes
|
||||
if(allRotationsPrime){
|
||||
circularPrimes.add(prime);
|
||||
}
|
||||
}
|
||||
|
||||
//Stop 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 void reset(){
|
||||
super.reset();
|
||||
primes.clear();
|
||||
circularPrimes.clear();
|
||||
}
|
||||
//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 number of all circular prime numbers under %d is %d", MAX_NUM, circularPrimes.size());
|
||||
}
|
||||
//Returns the ArrayList of primes < MAX_NUM
|
||||
public ArrayList<Integer> getPrimes(){
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
//Returns the ArrayList of circular primes < MAX_NUM
|
||||
public ArrayList<Integer> getCircularPrimes(){
|
||||
//If the problem hasn't been solved throw an excpetion
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return circularPrimes;
|
||||
}
|
||||
//Returns the number of circular primes
|
||||
public int getNumCircularPrimes(){
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return circularPrimes.size();
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The number of all circular prime numbers under 999999 is 55
|
||||
It took an average of 5.255 seconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user