Updated Algorithms

This commit is contained in:
2021-05-28 16:13:40 -04:00
parent 4fc455bff0
commit 8b3c6c8cd5
13 changed files with 43 additions and 50 deletions

View File

@@ -214,7 +214,7 @@ public class Problem11 extends Problem{
return String.format("The greatest product of 4 numbers in a line is %d\nThe numbers are %s", Algorithms.getProd(greatestProduct), greatestProduct.toString());
}
//Returns the numbers that were being searched
ArrayList<Integer> getNumbers(){
public ArrayList<Integer> getNumbers(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();

View File

@@ -295,7 +295,7 @@ public class Problem13 extends Problem{
}
return String.format("The sum of all %d numbers is %d\nThe first 10 digits of the sum of the numbers is %s", nums.size(), sum, (sum.toString()).substring(0, 10));
}
//Returns the list 50-digit numbers
//Returns the list of 50-digit numbers
public ArrayList<BigInteger> getNumbers(){
//If the problem hasn't been solved throw an exception
if(!solved){

View File

@@ -42,7 +42,7 @@ public class Problem14 extends Problem{
//Functions
//Constructor
public Problem14(){
super(String.format("Which starting number, under %d, produces the longest chain using the itterative sequence?", MAX_NUM));
super(String.format("Which starting number, under %d, produces the longest chain using the iterative sequence?", MAX_NUM + 1));
maxLength = 0;
maxNum = 0;
}

View File

@@ -35,12 +35,14 @@ public class Problem16 extends Problem{
private static final int POWER = 1000; //The power that the number is going to be raised to
//Instance variables
private BigInteger num; //The number to be calculated
private int sumOfElements; //THe sum of all digits in the number
private int sumOfElements; //The sum of all digits in the number
//Functions
//Constructor
public Problem16(){
super(String.format("What is the sum of the digits of the number %d^%d?", NUM_TO_POWER, POWER));
num = BigInteger.ZERO;
sumOfElements = 0;
}
//Operational functions
//Solve the problem

View File

@@ -89,7 +89,7 @@ public class Problem18 extends Problem{
));
}
//Instance variables
protected ArrayList<location> foundPoints; //For the points that I have already found the shortest distance to
protected ArrayList<location> foundPoints; //For the points that you have already found the shortest distance to
protected ArrayList<location> possiblePoints; //For the locations you are checking this round
protected int actualTotal; //The true total of the path from the top to the bottom
@@ -117,7 +117,6 @@ public class Problem18 extends Problem{
protected void removeHelper(ArrayList<location> possiblePoints, final location minLoc){
possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation)));
}
//Operational functions
//Solve the problem
public void solve(){
//If the problem has already been solved do nothing and end the function
@@ -131,7 +130,8 @@ public class Problem18 extends Problem{
//Invert the list
invert(list);
foundPoints.add(new location(0, 0, list.get(0).get(0), false)); //Add the first row as a found point because you have to go through the tip
//Add the first row as a found point because you have to go through the top element
foundPoints.add(new location(0, 0, list.get(0).get(0), false));
//Add the second row as possible points
possiblePoints.add(new location(0, 1, (list.get(0).get(0) + list.get(1).get(0)), true));
possiblePoints.add(new location(1, 1, (list.get(0).get(0) + list.get(1).get(1)), false));
@@ -162,10 +162,11 @@ public class Problem18 extends Problem{
}
else{
possiblePoints.add(new location(xLoc, yLoc, minLoc.total + list.get(yLoc).get(xLoc), true));
++xLoc; //Advance the x location to simulate going right
//Advance the x location to simulate going right
++xLoc;
//Check if x is out of bounds
if(xLoc < list.get(yLoc).size()){
possiblePoints.add( new location(xLoc, yLoc, minLoc.total + list.get(yLoc).get(xLoc), false));
possiblePoints.add(new location(xLoc, yLoc, minLoc.total + list.get(yLoc).get(xLoc), false));
}
}
}

View File

@@ -414,6 +414,7 @@ public class Problem22 extends Problem{
super("What is the total of all the name scores in this file?");
sums = new ArrayList<Long>();
prod = new ArrayList<Long>();
sum = 0;
}
//Operational functions
//Solve the problem

View File

@@ -23,7 +23,6 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import java.util.ArrayList;
@@ -32,7 +31,7 @@ import java.util.ArrayList;
public class Problem26 extends Problem{
//Variables
//Static variables
private static final int TOP_NUM = 999; //The number of digits needed in the number
private static final int TOP_NUM = 999; //The largest denominator to test
//Instance variables
private int longestCycle; //The length of the longest cycle
private int longestNumber; //The starting denominator of the longest cycle
@@ -42,7 +41,7 @@ public class Problem26 extends Problem{
public Problem26(){
super(String.format("Find the value of d <= %d for which 1/d contains the longest recurring cycle in its decimal fraction part.", TOP_NUM));
longestCycle = 0;
longestNumber = 0;
longestNumber = 1;
}
//Operational functions
//Solve the problem
@@ -70,7 +69,7 @@ public class Problem26 extends Problem{
endFound = true;
}
//Check if the remainder is in the list and set the appropriate flags
else if(Algorithms.isFound(denomList, remainder)){
else if(denomList.contains(remainder)){
endFound = true;
cycleFound = true;
}

View File

@@ -26,8 +26,6 @@ package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import java.util.ArrayList;
public class Problem27 extends Problem{
//Variables
@@ -38,7 +36,6 @@ public class Problem27 extends Problem{
private int topA; //The A for the most n's generated
private int topB; //The B for the most n's generated
private int topN; //The most n's generated
private ArrayList<Integer> primes; //A list of all primes that could possibly be generated with this formula
//Functions
//Constructor
@@ -47,7 +44,6 @@ public class Problem27 extends Problem{
topA = 0;
topB = 0;
topN = 0;
primes = new ArrayList<Integer>();
}
//Operational functions
//Solve the problem
@@ -61,9 +57,6 @@ public class Problem27 extends Problem{
//Start the timer
timer.start();
//Get the primes
//primes = Algorithms.getPrimes(12000);
//Start with the lowest possible A and check all possibilities after that
for(int a = -LARGEST_POSSIBLE_A;a <= LARGEST_POSSIBLE_A;++a){
//Start with the lowest possible B and check all possibilities after that
@@ -99,7 +92,6 @@ public class Problem27 extends Problem{
topA = 0;
topB = 0;
topN = 0;
primes.clear();
}
//Gets
//Returns the result of solving the problem
@@ -134,11 +126,19 @@ public class Problem27 extends Problem{
}
return topN;
}
//Resuts the product of A and B for the answer
public int getProduct(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
return topA * topB;
}
}
/* Results:
The greatest number of primes found is 70
It was found with A = -61, B = 971
The product of A and B is -59231
It took an average of 3.764 seconds to run this problem through 100 iterations
It took an average of 18.835 milliseconds to run this problem through 100 iterations
*/

View File

@@ -38,7 +38,7 @@ public class Problem28 extends Problem{
//Functions
//Constructor
public Problem28(){
super("What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral");
super("What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction?");
sumOfDiagonals = 0;
}
//Operational functions

View File

@@ -92,35 +92,19 @@ public class Problem29 extends Problem{
return String.format("The number of unique values generated by a^b for %d <= a <= %d and %d <= b <= %d is %d", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B, unique.size());
}
//Returns the lowest possible value for a
public int getBottomA(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
public static int getBottomA(){
return BOTTOM_A;
}
//Returns the highest possible value for a
public int getTopA(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
public static int getTopA(){
return TOP_A;
}
//Returns the lowest possible value for b
public int getBottomB(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
public static int getBottomB(){
return BOTTOM_B;
}
//Returns the highest possible value for b
public int getTopB(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
public static int getTopB(){
return TOP_B;
}
//Returns a vector of all the unique values for a^b

View File

@@ -37,12 +37,14 @@ public class Problem30 extends Problem{
private static final long POWER_RAISED = 5; //This is the power that the digits are raised to
//Instance variables
private ArrayList<Long> sumOfFifthNumbers; //This is an ArrayList of the numbers that are the sum of the fifth power of their digits
private long sum; //This is the sum of the sumOfFifthNumbers array
//Functions
//Operational functions
public Problem30(){
super("Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.");
sumOfFifthNumbers = new ArrayList<Long>();
sum = 0;
}
//Operational functions
//Returns an ArrayList with the individual digits of the number passed to it
@@ -84,6 +86,9 @@ public class Problem30 extends Problem{
}
}
//Get the sum of the numbers
sum = Algorithms.getLongSum(sumOfFifthNumbers);
//Stop the timer
timer.stop();
@@ -95,6 +100,7 @@ public class Problem30 extends Problem{
public void reset(){
super.reset();
sumOfFifthNumbers.clear();
sum = 0;
}
//Gets
//Returns the result of solving the problem
@@ -103,7 +109,7 @@ public class Problem30 extends Problem{
if(!solved){
throw new Unsolved();
}
return String.format("The sum of all the numbers that can be written as the sum of the fifth powers of their digits is %d", Algorithms.getLongSum(sumOfFifthNumbers));
return String.format("The sum of all the numbers that can be written as the sum of the fifth powers of their digits is %d", sum);
}
//This returns the top number to be checked
public long getTopNum(){
@@ -127,7 +133,7 @@ public class Problem30 extends Problem{
if(!solved){
throw new Unsolved();
}
return Algorithms.getLongSum(sumOfFifthNumbers);
return sum;
}
}

View File

@@ -29,7 +29,7 @@ import mattrixwv.ProjectEuler.Unsolved;
public class Problem31 extends Problem{
//Variables
//Static variables
private static final int desiredValue = 200; //The value of coins we want
private static final int DESIRED_VALUE = 200; //The value of coins we want
//Instance variables
private int permutations; //The number of permutations that are found
@@ -51,7 +51,7 @@ public class Problem31 extends Problem{
timer.start();
//Start with 200p and remove the necessary coins with each loop
for(int pound2 = desiredValue; pound2 >= 0;pound2 -= 200){
for(int pound2 = DESIRED_VALUE; pound2 >= 0;pound2 -= 200){
for(int pound1 = pound2;pound1 >= 0;pound1 -= 100){
for(int pence50 = pound1;pence50 >= 0;pence50 -= 50){
for(int pence20 = pence50;pence20 >= 0;pence20 -= 20){

View File

@@ -135,7 +135,7 @@ public class Problem33 extends Problem{
return String.format("The denominator of the product is %d", prodDenominator);
}
//Returns the list of numerators
ArrayList<Integer> getNumerators(){
public ArrayList<Integer> getNumerators(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
@@ -143,7 +143,7 @@ public class Problem33 extends Problem{
return numerators;
}
//Returns the list of denominators
ArrayList<Integer> getDenominators(){
public ArrayList<Integer> getDenominators(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
@@ -151,7 +151,7 @@ public class Problem33 extends Problem{
return denominators;
}
//Returns the answer to the question
int getProdDenominator(){
public int getProdDenominator(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();