From 8b3c6c8cd5fc1da2efab13407e3ce9076ce23669 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Fri, 28 May 2021 16:13:40 -0400 Subject: [PATCH] Updated Algorithms --- .../ProjectEuler/Problems/Problem11.java | 2 +- .../ProjectEuler/Problems/Problem13.java | 2 +- .../ProjectEuler/Problems/Problem14.java | 2 +- .../ProjectEuler/Problems/Problem16.java | 4 +++- .../ProjectEuler/Problems/Problem18.java | 11 +++++---- .../ProjectEuler/Problems/Problem22.java | 1 + .../ProjectEuler/Problems/Problem26.java | 7 +++--- .../ProjectEuler/Problems/Problem27.java | 18 +++++++------- .../ProjectEuler/Problems/Problem28.java | 2 +- .../ProjectEuler/Problems/Problem29.java | 24 ++++--------------- .../ProjectEuler/Problems/Problem30.java | 10 ++++++-- .../ProjectEuler/Problems/Problem31.java | 4 ++-- .../ProjectEuler/Problems/Problem33.java | 6 ++--- 13 files changed, 43 insertions(+), 50 deletions(-) diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem11.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem11.java index 0c06fb8..4ee1c63 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem11.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem11.java @@ -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 getNumbers(){ + public ArrayList getNumbers(){ //If the problem hasn't been solved throw an exception if(!solved){ throw new Unsolved(); diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem13.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem13.java index eed5c36..9af17b2 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem13.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem13.java @@ -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 getNumbers(){ //If the problem hasn't been solved throw an exception if(!solved){ diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem14.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem14.java index 69cf87a..b03a855 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem14.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem14.java @@ -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; } diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem16.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem16.java index e3e8995..47bf806 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem16.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem16.java @@ -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 diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java index 0fb2f88..7a4d3df 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java @@ -89,7 +89,7 @@ public class Problem18 extends Problem{ )); } //Instance variables - protected ArrayList foundPoints; //For the points that I have already found the shortest distance to + protected ArrayList foundPoints; //For the points that you have already found the shortest distance to protected ArrayList 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 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)); } } } diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java index febec16..2ccc557 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java @@ -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(); prod = new ArrayList(); + sum = 0; } //Operational functions //Solve the problem diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem26.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem26.java index 171664b..666eb7b 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem26.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem26.java @@ -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; } diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem27.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem27.java index 16ac4f0..07852f0 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem27.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem27.java @@ -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 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(); } //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 */ diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem28.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem28.java index 85eb784..cdee5cd 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem28.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem28.java @@ -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 diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem29.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem29.java index 714a39b..2f1e2a3 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem29.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem29.java @@ -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 diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem30.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem30.java index 4bd74b6..ecfb3ca 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem30.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem30.java @@ -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 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(); + 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; } } diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem31.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem31.java index 722dd5d..1ec11f8 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem31.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem31.java @@ -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){ diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java index 8ca51dd..3faf202 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java @@ -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 getNumerators(){ + public ArrayList 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 getDenominators(){ + public ArrayList 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();