From 2c42098ea77e0a27b7574640e3c34a87da71529d Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Sun, 6 Jun 2021 12:19:12 -0400 Subject: [PATCH] Added solution for problem 35 --- .../mattrixwv/ProjectEuler/Benchmark.java | 2 +- .../ProjectEuler/ProblemSelection.java | 3 +- .../ProjectEuler/Problems/Problem34.java | 2 +- .../ProjectEuler/Problems/Problem35.java | 135 ++++++++++++++++++ 4 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java diff --git a/src/main/java/mattrixwv/ProjectEuler/Benchmark.java b/src/main/java/mattrixwv/ProjectEuler/Benchmark.java index f89849b..697a2ba 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Benchmark.java +++ b/src/main/java/mattrixwv/ProjectEuler/Benchmark.java @@ -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 tooLong = new ArrayList(Arrays.asList(15, 23, 24)); + private static final ArrayList tooLong = new ArrayList(Arrays.asList(15, 23, 24, 35)); //The driver function for the benchmark selection public static void benchmarkMenu() throws InvalidResult{ BenchmarkOptions selection = BenchmarkOptions.size; diff --git a/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java b/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java index 96f8fe7..815f73c 100644 --- a/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java +++ b/src/main/java/mattrixwv/ProjectEuler/ProblemSelection.java @@ -36,7 +36,7 @@ public class ProblemSelection{ public static final ArrayList 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, 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; diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem34.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem34.java index 36ea37f..41152d0 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem34.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem34.java @@ -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 diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java new file mode 100644 index 0000000..f52309a --- /dev/null +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java @@ -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 . +*/ +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 primes; //The primes below MAX_NUM + private ArrayList circularPrimes; //The circular primes below MAX_NUM + //Functions + //Returns a list of all rotations of a string passed to it + private ArrayList getRotations(String str){ + ArrayList rotations = new ArrayList(); + 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(); + circularPrimes = new ArrayList(); + } + //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 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 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 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 +*/