mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Added solution to problem 38
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, 36, 37, 67));
|
||||
31, 32, 33, 34, 35, 36, 37, 38, 67));
|
||||
|
||||
//Returns the problem corresponding to the given problem number
|
||||
public static Problem getProblem(Integer problemNumber){
|
||||
@@ -79,6 +79,7 @@ public class ProblemSelection{
|
||||
case 35 : problem = new Problem35(); break;
|
||||
case 36 : problem = new Problem36(); break;
|
||||
case 37 : problem = new Problem37(); break;
|
||||
case 38 : problem = new Problem38(); break;
|
||||
case 67 : problem = new Problem67(); break;
|
||||
}
|
||||
return problem;
|
||||
|
||||
@@ -121,7 +121,7 @@ public class Problem37 extends Problem{
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Throw a flag to show the porblem is solved
|
||||
//Throw a flag to show the problem is solved
|
||||
solved = true;
|
||||
}
|
||||
//Reset the problem so it can be run again
|
||||
|
||||
116
src/main/java/mattrixwv/ProjectEuler/Problems/Problem38.java
Normal file
116
src/main/java/mattrixwv/ProjectEuler/Problems/Problem38.java
Normal file
@@ -0,0 +1,116 @@
|
||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem37.java
|
||||
//Matthew Ellison
|
||||
// Created: 10-11-21
|
||||
//Modified: 10-11-21
|
||||
//What is the largest 1-9 pandigital number that can be formed as the concatenated product of an integer with 1, 2, ... n where n > 1
|
||||
//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 mattrixwv.StringAlgorithms;
|
||||
|
||||
|
||||
public class Problem38 extends Problem{
|
||||
//Variables
|
||||
//Static variables
|
||||
private static final long HIGHEST_POSSIBLE_NUM = 9999; //The highest number that needs to be checked for a 1-9 pandigital
|
||||
//Instance variables
|
||||
private long largestNum; //The number passed to the executeFormula function that returns the largest pandigital
|
||||
private long pandigital; //The largest pandigital number found
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem38(){
|
||||
super("What is the largest 1-9 pandigital number that can be formed as the concatenated product of an integer with 1, 2, ... n where n > 1");
|
||||
largestNum = 0;
|
||||
pandigital = 0;
|
||||
}
|
||||
//Operational functions
|
||||
//Take the number and add its multiples to a string to return
|
||||
private String executeFormula(int num){
|
||||
//Turn the current number into a string
|
||||
String numStr = Integer.toString(num);
|
||||
int cnt = 2;
|
||||
//Multiply the number and append the product to the string until you have one long enough
|
||||
do{
|
||||
numStr += Integer.toString(num * cnt);
|
||||
++cnt;
|
||||
}while(numStr.length() < 9);
|
||||
|
||||
return numStr;
|
||||
}
|
||||
//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();
|
||||
|
||||
|
||||
//Loop from 1 -> HIGHEST_POSSIBLE_NUM checking for pandigitals
|
||||
for(int cnt = 1;cnt <= HIGHEST_POSSIBLE_NUM;++cnt){
|
||||
//Get the string from the formula
|
||||
String numStr = executeFormula(cnt);
|
||||
long panNum = Long.parseLong(numStr);
|
||||
//If the number is pandigital save it as the highest number
|
||||
if(StringAlgorithms.isPandigital(numStr) && (panNum > pandigital)){
|
||||
largestNum = cnt;
|
||||
pandigital = panNum;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Throw a flag to show the problem is solved
|
||||
solved = true;
|
||||
}
|
||||
//Reset the prblem so it can be run again
|
||||
public void reset(){
|
||||
super.reset();
|
||||
largestNum = 0;
|
||||
pandigital = 0;
|
||||
}
|
||||
|
||||
//Gets
|
||||
//Returns a string with the solution to the problem
|
||||
public String getResult(){
|
||||
solvedCheck("results");
|
||||
return String.format("The largest appended product pandigital is %d", pandigital);
|
||||
}
|
||||
//Returns the largest number
|
||||
public long getLargestNum(){
|
||||
solvedCheck("largest number");
|
||||
return largestNum;
|
||||
}
|
||||
//Returns the pandigital of the number
|
||||
public long getPandigital(){
|
||||
solvedCheck("pandigital");
|
||||
return pandigital;
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
The largest appended product pandigital is 932718654
|
||||
It took 16.947 milliseconds to solve this problem.
|
||||
*/
|
||||
Reference in New Issue
Block a user