mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Added solution to problem 36
This commit is contained in:
@@ -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, 67));
|
||||
31, 32, 33, 34, 35, 36, 67));
|
||||
|
||||
//Returns the problem corresponding to the given problem number
|
||||
public static Problem getProblem(Integer problemNumber){
|
||||
@@ -77,6 +77,7 @@ public class ProblemSelection{
|
||||
case 33 : problem = new Problem33(); break;
|
||||
case 34 : problem = new Problem34(); break;
|
||||
case 35 : problem = new Problem35(); break;
|
||||
case 36 : problem = new Problem36(); break;
|
||||
case 67 : problem = new Problem67(); break;
|
||||
}
|
||||
return problem;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//ProjectEulerJava/src/main/java/matrixwv/ProjectEuler/Problems/Problem35.java
|
||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java
|
||||
//Matthew Ellison
|
||||
// Createe: 06-05-21
|
||||
// Created: 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
|
||||
|
||||
115
src/main/java/mattrixwv/ProjectEuler/Problems/Problem36.java
Normal file
115
src/main/java/mattrixwv/ProjectEuler/Problems/Problem36.java
Normal file
@@ -0,0 +1,115 @@
|
||||
//ProjectEulerJava/str/main/java/mattrixwv/ProjectEuler/Problems/Problem36.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-29-21
|
||||
//Modified: 06-29-21
|
||||
//Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
|
||||
//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 Problem36 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static final int MAX_NUM = 999999; //The largest number that will be checked
|
||||
//Instance variables
|
||||
private ArrayList<Integer> palindromes; //All numbers that are palindromes in base 10 and 2
|
||||
private int sum; //The sum of all elements in the list of palindromes
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem36(){
|
||||
super("Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.");
|
||||
palindromes = new ArrayList<Integer>();
|
||||
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();
|
||||
|
||||
//Start with 1, check if it is a palindrome in base 10 and 2, and continue to MAX_NUM
|
||||
for(int num = 1;num < MAX_NUM;++num){
|
||||
//Check if num is a palindrome
|
||||
if(Algorithms.isPalindrome(Integer.toString(num))){
|
||||
//Convert num to base 2 and see if that is a palindrome
|
||||
String binNum = Algorithms.toBin(num);
|
||||
if(Algorithms.isPalindrome(binNum)){
|
||||
//Add num to the list of palindromes
|
||||
palindromes.add(num);
|
||||
}
|
||||
}
|
||||
}
|
||||
//Get the sum of all palindromes in the list
|
||||
sum = Algorithms.getSum(palindromes);
|
||||
|
||||
//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();
|
||||
palindromes.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 base 10 and base 2 palindromic numbers < %d is %d", MAX_NUM, sum);
|
||||
}
|
||||
//Return the List of palindromes < MAX_NUM
|
||||
public ArrayList<Integer> getPalindromes(){
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return palindromes;
|
||||
}
|
||||
//Return the sum of all elements in the List of palindromes
|
||||
public int getSumOfPalindromes(){
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187
|
||||
It took an average of 39.606 milliseconds to run this problem through 100 iterations
|
||||
*/
|
||||
Reference in New Issue
Block a user