mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Created solution to problem32
This commit is contained in:
@@ -35,7 +35,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, 67));
|
||||
31, 32, 67));
|
||||
|
||||
//Returns the problem corresponding to the given problem number
|
||||
public static Problem getProblem(Integer problemNumber){
|
||||
@@ -72,6 +72,7 @@ public class ProblemSelection{
|
||||
case 29 : problem = new Problem29(); break;
|
||||
case 30 : problem = new Problem30(); break;
|
||||
case 31 : problem = new Problem31(); break;
|
||||
case 32 : problem = new Problem32(); break;
|
||||
case 67 : problem = new Problem67(); break;
|
||||
}
|
||||
return problem;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem31.java
|
||||
//Matthew Ellison
|
||||
// Created: 06-19-20
|
||||
//Modified: 07-1920
|
||||
//Modified: 07-19-20
|
||||
//How many different ways can £2 be made using any number of coins?
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||
/*
|
||||
|
||||
@@ -1,17 +1,171 @@
|
||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem32.java
|
||||
//Matthew Ellison
|
||||
// Created: 07-27-20
|
||||
//Modified: 07-27-20
|
||||
//Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
|
||||
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||
/*
|
||||
Copyright (C) 2020 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 Problem32 extends Problem{
|
||||
Problem32(){
|
||||
super("");
|
||||
//Structures
|
||||
//Holds the set of numbers that make a product
|
||||
private class ProductSet{
|
||||
private int multiplicand;
|
||||
private int multiplier;
|
||||
public ProductSet(int multiplicand, int multiplier){
|
||||
this.multiplicand = multiplicand;
|
||||
this.multiplier = multiplier;
|
||||
}
|
||||
@SuppressWarnings("unused")
|
||||
public int getMultiplicand(){
|
||||
return multiplicand;
|
||||
}
|
||||
@SuppressWarnings("unused")
|
||||
public int getMultiplier(){
|
||||
return multiplier;
|
||||
}
|
||||
public int getProduct(){
|
||||
return (multiplicand * multiplier);
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object o){
|
||||
//If an object is compared to itself return true
|
||||
if(o == this){
|
||||
return true;
|
||||
}
|
||||
//Check that the object is the correct type
|
||||
if(!(o instanceof ProductSet)){
|
||||
return false;
|
||||
}
|
||||
ProductSet secondSet = (ProductSet)o;
|
||||
//Return true if the products are the same
|
||||
return (getProduct() == secondSet.getProduct());
|
||||
}
|
||||
@Override
|
||||
public String toString(){
|
||||
return String.format("%d%d%d", multiplicand, multiplier, getProduct());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
//Variables
|
||||
//Static variables
|
||||
private static final int TOP_MULTIPLICAND = 99; //The largest multiplicand to check
|
||||
private static final int TOP_MULTIPLIER = 4999; //The largest multiplier to check
|
||||
//Instance variables
|
||||
ArrayList<ProductSet> listOfProducts; //The list of unique products that are 1-9 pandigital
|
||||
long sumOfPandigitals; //The sum of the products of the pandigital numbers
|
||||
|
||||
//Functions
|
||||
//Constructor
|
||||
public Problem32(){
|
||||
super("Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.");
|
||||
listOfProducts = new ArrayList<ProductSet>();
|
||||
sumOfPandigitals = 0;
|
||||
}
|
||||
//Operational functions
|
||||
//Solve the problem
|
||||
public void solve(){
|
||||
//1X1-4
|
||||
//2X1-3
|
||||
//If the problem has already been solved do nothing and end the function
|
||||
if(solved){
|
||||
return;
|
||||
}
|
||||
|
||||
//Start the timer
|
||||
timer.start();
|
||||
|
||||
//Create the multiplicand and start working your way up
|
||||
for(int multiplicand = 1;multiplicand <= TOP_MULTIPLICAND;++multiplicand){
|
||||
//Run through all possible multipliers
|
||||
for(int multiplier = multiplicand;multiplier <= TOP_MULTIPLIER;++multiplier){
|
||||
ProductSet currentProductSet = new ProductSet(multiplicand, multiplier);
|
||||
//If the product is too long move on to the next possible number
|
||||
if(currentProductSet.toString().length() > 9){
|
||||
break;
|
||||
}
|
||||
//If the current number is a pandigital that doesn't already exist in the list add it to the list
|
||||
if(isPandigital(currentProductSet)){
|
||||
if(!listOfProducts.contains(currentProductSet)){
|
||||
listOfProducts.add(currentProductSet);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Get the sum of the products of the pandigitals
|
||||
for(ProductSet prod : listOfProducts){
|
||||
sumOfPandigitals += prod.getProduct();
|
||||
}
|
||||
|
||||
//Stop the timer
|
||||
timer.stop();
|
||||
|
||||
//Save the results
|
||||
result = String.format("There are %d unique 1-9 pandigitals\nThe sum of the products of these pandigitals is %d", listOfProducts.size(), sumOfPandigitals);
|
||||
|
||||
//Throw a flag to show the problem is solved
|
||||
solved = true;
|
||||
}
|
||||
//Returns true if the passed productset is 1-9 pandigital
|
||||
private boolean isPandigital(ProductSet currentSet){
|
||||
//Get the numbers out of the object and put them into a string
|
||||
String numberString = currentSet.toString();
|
||||
//Make sure the string is the correct length
|
||||
if(numberString.length() != 9){
|
||||
return false;
|
||||
}
|
||||
//Make sure every number from 1-9 is contained exactly once
|
||||
for(int panNumber = 1;panNumber <= 9;++panNumber){
|
||||
//Make sure there is exactly one of this number contained in the string
|
||||
final int tempNum = panNumber; //This is here because forDigit() wanted a final variable
|
||||
if(Algorithms.findNumOccurrence(numberString, Character.forDigit(tempNum, 10)) != 1){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//If all numbers were found in the string return true
|
||||
return true;
|
||||
}
|
||||
//Reset the problem so it can be run again
|
||||
public void reset(){
|
||||
super.reset();
|
||||
listOfProducts.clear();
|
||||
sumOfPandigitals = 0;
|
||||
}
|
||||
//Gets
|
||||
//Returns the sum of the pandigitals
|
||||
public long getSumOfPandigitals(){
|
||||
//If the problem hasn't been solved throw an exception
|
||||
if(!solved){
|
||||
throw new Unsolved();
|
||||
}
|
||||
return sumOfPandigitals;
|
||||
}
|
||||
}
|
||||
|
||||
/* Results:
|
||||
|
||||
There are 7 unique 1-9 pandigitals
|
||||
The sum of the products of these pandigitals is 45228
|
||||
It took an average of 63.456 milliseconds to run this problem through 100 iterations
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user