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()); 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 //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 the problem hasn't been solved throw an exception
if(!solved){ if(!solved){
throw new Unsolved(); 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)); 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(){ public ArrayList<BigInteger> getNumbers(){
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
if(!solved){ if(!solved){

View File

@@ -42,7 +42,7 @@ public class Problem14 extends Problem{
//Functions //Functions
//Constructor //Constructor
public Problem14(){ 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; maxLength = 0;
maxNum = 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 private static final int POWER = 1000; //The power that the number is going to be raised to
//Instance variables //Instance variables
private BigInteger num; //The number to be calculated 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 //Functions
//Constructor //Constructor
public Problem16(){ public Problem16(){
super(String.format("What is the sum of the digits of the number %d^%d?", NUM_TO_POWER, POWER)); 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 //Operational functions
//Solve the problem //Solve the problem

View File

@@ -89,7 +89,7 @@ public class Problem18 extends Problem{
)); ));
} }
//Instance variables //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 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 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){ protected void removeHelper(ArrayList<location> possiblePoints, final location minLoc){
possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation))); possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation)));
} }
//Operational functions
//Solve the problem //Solve the problem
public void solve(){ public void solve(){
//If the problem has already been solved do nothing and end the function //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 the list
invert(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 //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(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)); 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{ else{
possiblePoints.add(new location(xLoc, yLoc, minLoc.total + list.get(yLoc).get(xLoc), true)); 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 //Check if x is out of bounds
if(xLoc < list.get(yLoc).size()){ 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?"); super("What is the total of all the name scores in this file?");
sums = new ArrayList<Long>(); sums = new ArrayList<Long>();
prod = new ArrayList<Long>(); prod = new ArrayList<Long>();
sum = 0;
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem

View File

@@ -23,7 +23,6 @@
package mattrixwv.ProjectEuler.Problems; package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved; import mattrixwv.ProjectEuler.Unsolved;
import java.util.ArrayList; import java.util.ArrayList;
@@ -32,7 +31,7 @@ import java.util.ArrayList;
public class Problem26 extends Problem{ public class Problem26 extends Problem{
//Variables //Variables
//Static 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 //Instance variables
private int longestCycle; //The length of the longest cycle private int longestCycle; //The length of the longest cycle
private int longestNumber; //The starting denominator 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(){ 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)); 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; longestCycle = 0;
longestNumber = 0; longestNumber = 1;
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
@@ -70,7 +69,7 @@ public class Problem26 extends Problem{
endFound = true; endFound = true;
} }
//Check if the remainder is in the list and set the appropriate flags //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; endFound = true;
cycleFound = true; cycleFound = true;
} }

View File

@@ -26,8 +26,6 @@ package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms; import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved; import mattrixwv.ProjectEuler.Unsolved;
import java.util.ArrayList;
public class Problem27 extends Problem{ public class Problem27 extends Problem{
//Variables //Variables
@@ -38,7 +36,6 @@ public class Problem27 extends Problem{
private int topA; //The A for the most n's generated private int topA; //The A for the most n's generated
private int topB; //The B 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 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 //Functions
//Constructor //Constructor
@@ -47,7 +44,6 @@ public class Problem27 extends Problem{
topA = 0; topA = 0;
topB = 0; topB = 0;
topN = 0; topN = 0;
primes = new ArrayList<Integer>();
} }
//Operational functions //Operational functions
//Solve the problem //Solve the problem
@@ -61,9 +57,6 @@ public class Problem27 extends Problem{
//Start the timer //Start the timer
timer.start(); timer.start();
//Get the primes
//primes = Algorithms.getPrimes(12000);
//Start with the lowest possible A and check all possibilities after that //Start with the lowest possible A and check all possibilities after that
for(int a = -LARGEST_POSSIBLE_A;a <= LARGEST_POSSIBLE_A;++a){ for(int a = -LARGEST_POSSIBLE_A;a <= LARGEST_POSSIBLE_A;++a){
//Start with the lowest possible B and check all possibilities after that //Start with the lowest possible B and check all possibilities after that
@@ -99,7 +92,6 @@ public class Problem27 extends Problem{
topA = 0; topA = 0;
topB = 0; topB = 0;
topN = 0; topN = 0;
primes.clear();
} }
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
@@ -134,11 +126,19 @@ public class Problem27 extends Problem{
} }
return topN; 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: /* Results:
The greatest number of primes found is 70 The greatest number of primes found is 70
It was found with A = -61, B = 971 It was found with A = -61, B = 971
The product of A and B is -59231 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 //Functions
//Constructor //Constructor
public Problem28(){ 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; sumOfDiagonals = 0;
} }
//Operational functions //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()); 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 //Returns the lowest possible value for a
public int getBottomA(){ public static int getBottomA(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
return BOTTOM_A; return BOTTOM_A;
} }
//Returns the highest possible value for a //Returns the highest possible value for a
public int getTopA(){ public static int getTopA(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
return TOP_A; return TOP_A;
} }
//Returns the lowest possible value for b //Returns the lowest possible value for b
public int getBottomB(){ public static int getBottomB(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
return BOTTOM_B; return BOTTOM_B;
} }
//Returns the highest possible value for b //Returns the highest possible value for b
public int getTopB(){ public static int getTopB(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
return TOP_B; return TOP_B;
} }
//Returns a vector of all the unique values for a^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 private static final long POWER_RAISED = 5; //This is the power that the digits are raised to
//Instance variables //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 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 //Functions
//Operational functions //Operational functions
public Problem30(){ public Problem30(){
super("Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits."); 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>(); sumOfFifthNumbers = new ArrayList<Long>();
sum = 0;
} }
//Operational functions //Operational functions
//Returns an ArrayList with the individual digits of the number passed to it //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 //Stop the timer
timer.stop(); timer.stop();
@@ -95,6 +100,7 @@ public class Problem30 extends Problem{
public void reset(){ public void reset(){
super.reset(); super.reset();
sumOfFifthNumbers.clear(); sumOfFifthNumbers.clear();
sum = 0;
} }
//Gets //Gets
//Returns the result of solving the problem //Returns the result of solving the problem
@@ -103,7 +109,7 @@ public class Problem30 extends Problem{
if(!solved){ if(!solved){
throw new Unsolved(); 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 //This returns the top number to be checked
public long getTopNum(){ public long getTopNum(){
@@ -127,7 +133,7 @@ public class Problem30 extends Problem{
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
} }
return Algorithms.getLongSum(sumOfFifthNumbers); return sum;
} }
} }

View File

@@ -29,7 +29,7 @@ import mattrixwv.ProjectEuler.Unsolved;
public class Problem31 extends Problem{ public class Problem31 extends Problem{
//Variables //Variables
//Static 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 //Instance variables
private int permutations; //The number of permutations that are found private int permutations; //The number of permutations that are found
@@ -51,7 +51,7 @@ public class Problem31 extends Problem{
timer.start(); timer.start();
//Start with 200p and remove the necessary coins with each loop //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 pound1 = pound2;pound1 >= 0;pound1 -= 100){
for(int pence50 = pound1;pence50 >= 0;pence50 -= 50){ for(int pence50 = pound1;pence50 >= 0;pence50 -= 50){
for(int pence20 = pence50;pence20 >= 0;pence20 -= 20){ 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); return String.format("The denominator of the product is %d", prodDenominator);
} }
//Returns the list of numerators //Returns the list of numerators
ArrayList<Integer> getNumerators(){ public ArrayList<Integer> getNumerators(){
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -143,7 +143,7 @@ public class Problem33 extends Problem{
return numerators; return numerators;
} }
//Returns the list of denominators //Returns the list of denominators
ArrayList<Integer> getDenominators(){ public ArrayList<Integer> getDenominators(){
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();
@@ -151,7 +151,7 @@ public class Problem33 extends Problem{
return denominators; return denominators;
} }
//Returns the answer to the question //Returns the answer to the question
int getProdDenominator(){ public int getProdDenominator(){
//If the problem hasn't been solved throw an exception //If the problem hasn't been solved throw an exception
if(!solved){ if(!solved){
throw new Unsolved(); throw new Unsolved();