Updated long running problem to test smaller dataset

This commit is contained in:
2022-12-06 18:41:20 -05:00
parent 5bdb9f8110
commit d48d245110
6 changed files with 50 additions and 37 deletions

View File

@@ -26,15 +26,15 @@ package com.mattrixwv.project_euler.problems;
public class Problem15 extends Problem{
//Variables
//Static vaiables
private static final int WIDTH = 20; //The width of the box to traverse
private static final int LENGTH = 20; //The height of the box to traverse
protected static int gridWidth = 20; //The width of the box to traverse
protected static int gridLength = 20; //The height of the box to traverse
//Instance variables
private long numOfRoutes; //The number of routes from 0, 0 to 20, 20
//Functions
//Constructor
public Problem15(){
super(String.format("How many routes from the top left corner to the bottom right corner are there through a %dx%d grid if you can only move right and down?", WIDTH, LENGTH));
super(String.format("How many routes from the top left corner to the bottom right corner are there through a %dx%d grid if you can only move right and down?", gridWidth, gridLength));
numOfRoutes = 0;
}
//Operational functions
@@ -65,18 +65,18 @@ public class Problem15 extends Problem{
//It moves right first, then down
private void move(int currentX, int currentY){
//Check if you are at the end and act accordingly
if((currentX == WIDTH) && (currentY == LENGTH)){
if((currentX == gridWidth) && (currentY == gridLength)){
++numOfRoutes;
return;
}
//Move right if possible
if(currentX < WIDTH){
if(currentX < gridWidth){
move(currentX + 1, currentY);
}
//Move down if possible
if(currentY < LENGTH){
if(currentY < gridLength){
move(currentX, currentY + 1);
}
}

View File

@@ -33,7 +33,7 @@ import java.util.List;
public class Problem23 extends Problem{
//Variables
//Static variables
private static final int MAX_NUM = 28123; //The largest number to be checked
protected static int maxNum = 28123; //The largest number to be checked
//Instance variables
private ArrayList<Integer> divisorSums; //This gives the sum of the divisors at subscripts
private long sum; //The sum of all the numbers we are looking for
@@ -49,9 +49,9 @@ public class Problem23 extends Problem{
//Operational functions
//Reserve the size of the array to speed up insertion
private void reserveArray(){
divisorSums.ensureCapacity(MAX_NUM); //It is faster to reserve the appropriate amount of ram now
divisorSums.ensureCapacity(maxNum); //It is faster to reserve the appropriate amount of ram now
//Make sure every element has a 0 in it's location
while(divisorSums.size() <= MAX_NUM){
while(divisorSums.size() <= maxNum){
divisorSums.add(0);
}
}
@@ -68,7 +68,7 @@ public class Problem23 extends Problem{
//Get the sum of the divisors of all numbers < MAX_NUM
for(int cnt = 1;cnt < MAX_NUM;++cnt){
for(int cnt = 1;cnt < maxNum;++cnt){
List<Integer> div = NumberAlgorithms.getDivisors(cnt);
//Remove the last element, which is the number itself. This gives us the propper divisors
if(div.size() > 1){
@@ -86,7 +86,7 @@ public class Problem23 extends Problem{
}
//Check if each number can be the sum of 2 abundant numbers and add to the sum if no
for(int cnt = 1;cnt < MAX_NUM;++cnt){
for(int cnt = 1;cnt < maxNum;++cnt){
if(!isSum(abund, cnt)){
sum += cnt;
}

View File

@@ -32,15 +32,15 @@ import java.util.List;
public class Problem24 extends Problem{
//Variables
//Static variables
private static final int NEEDED_PERM = 1000000; //The number of the permutation that you need
private static String nums = "0123456789"; //All of the characters that we need to get the permutations of
protected static int neededPerm = 1000000; //The number of the permutation that you need
protected static String nums = "0123456789"; //All of the characters that we need to get the permutations of
//Instance variables
private List<String> permutations; //Holds all of the permutations of the string nums
//Functions
//Constructor
public Problem24(){
super(String.format("What is the millionth lexicographic permutation of the digits %s?", nums));
super(String.format("What is the %dth lexicographic permutation of the digits %s?", neededPerm, nums));
permutations = new ArrayList<>();
}
//Operational functions
@@ -77,7 +77,7 @@ public class Problem24 extends Problem{
@Override
public String getResult(){
solvedCheck("result");
return String.format("The 1 millionth permutation is %s", permutations.get(NEEDED_PERM - 1));
return String.format("The %dth permutation is %s", neededPerm, permutations.get(neededPerm - 1));
}
//Returns an ArrayList with all of the permutations
public List<String> getPermutationsList(){
@@ -87,7 +87,7 @@ public class Problem24 extends Problem{
//Returns the requested permutation
public String getPermutation(){
solvedCheck("1,000,000th permutation");
return permutations.get(NEEDED_PERM - 1);
return permutations.get(neededPerm - 1);
}
}

View File

@@ -4,7 +4,6 @@ package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -17,27 +16,26 @@ import com.mattrixwv.project_euler.exceptions.Unsolved;
@ExtendWith(MockitoExtension.class)
public class TestProblem15 extends BaseTest{
@InjectMocks
private Problem15 problem;
private Problem15Override problem;
@Test
@Override
public void testDescription(){
assertEquals("How many routes from the top left corner to the bottom right corner are there through a 20x20 grid if you can only move right and down?", problem.getDescription());
assertEquals("How many routes from the top left corner to the bottom right corner are there through a 5x5 grid if you can only move right and down?", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assumeTrue(Boolean.valueOf(System.getProperty("longTests")));
assertThrows(Unsolved.class, () -> { problem.getNumberOfRoutes(); });
problem.solve();
//Verify result
assertEquals(137846528820L, problem.getNumberOfRoutes());
assertEquals("The number of routes is 137846528820", problem.getResult());
assertEquals(252, problem.getNumberOfRoutes());
assertEquals("The number of routes is 252", problem.getResult());
//Verify the problem variables and functions were called
verifyProblem(problem);
@@ -48,7 +46,6 @@ public class TestProblem15 extends BaseTest{
@Test
@Override
public void testReset(){
assumeTrue(Boolean.valueOf(System.getProperty("longTests")));
problem.reset();
//Verify solved was reset
@@ -58,4 +55,11 @@ public class TestProblem15 extends BaseTest{
//Verify the problem can be solved again
testSolve();
}
public static class Problem15Override extends Problem15{
static{
gridWidth = 5;
gridLength = 5;
}
}
}

View File

@@ -4,7 +4,6 @@ package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -17,7 +16,7 @@ import com.mattrixwv.project_euler.exceptions.Unsolved;
@ExtendWith(MockitoExtension.class)
public class TestProblem23 extends BaseTest{
@InjectMocks
private Problem23 problem;
private Problem23Override problem;
@Test
@@ -30,14 +29,13 @@ public class TestProblem23 extends BaseTest{
@Test
@Override
public void testSolve(){
assumeTrue(Boolean.valueOf(System.getProperty("longTests")));
assertThrows(Unsolved.class, () -> { problem.getSum(); });
problem.solve();
//Verify result
assertEquals(4179871, problem.getSum());
assertEquals("The answer is 4179871", problem.getResult());
assertEquals(2035227, problem.getSum());
assertEquals("The answer is 2035227", problem.getResult());
//Verify the problem variables and functions were called
verifyProblem(problem);
@@ -48,7 +46,6 @@ public class TestProblem23 extends BaseTest{
@Test
@Override
public void testReset(){
assumeTrue(Boolean.valueOf(System.getProperty("longTests")));
problem.reset();
//Verify solved was reset
@@ -58,4 +55,11 @@ public class TestProblem23 extends BaseTest{
//Verify the problem can be solved again
testSolve();
}
public static class Problem23Override extends Problem23{
static{
maxNum = 5000;
}
}
}

View File

@@ -4,7 +4,6 @@ package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -17,29 +16,28 @@ import com.mattrixwv.project_euler.exceptions.Unsolved;
@ExtendWith(MockitoExtension.class)
public class TestProblem24 extends BaseTest{
@InjectMocks
private Problem24 problem;
private Problem24Override problem;
@Test
@Override
public void testDescription(){
assertEquals("What is the millionth lexicographic permutation of the digits 0123456789?", problem.getDescription());
assertEquals("What is the 100th lexicographic permutation of the digits 01234?", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assumeTrue(Boolean.valueOf(System.getProperty("longTests")));
assertThrows(Unsolved.class, () -> { problem.getPermutationsList(); });
assertThrows(Unsolved.class, () -> { problem.getPermutation(); });
problem.solve();
//Verify result
assertEquals(3628800, problem.getPermutationsList().size());
assertEquals("2783915460", problem.getPermutation());
assertEquals("The 1 millionth permutation is 2783915460", problem.getResult());
assertEquals(120, problem.getPermutationsList().size());
assertEquals("40231", problem.getPermutation());
assertEquals("The 100th permutation is 40231", problem.getResult());
//Verify the problem variables and functions were called
verifyProblem(problem);
@@ -50,7 +48,6 @@ public class TestProblem24 extends BaseTest{
@Test
@Override
public void testReset(){
assumeTrue(Boolean.valueOf(System.getProperty("longTests")));
problem.reset();
//Verify solved was reset
@@ -60,4 +57,12 @@ public class TestProblem24 extends BaseTest{
//Verify the problem can be solved again
testSolve();
}
public static class Problem24Override extends Problem24{
static{
neededPerm = 100;
nums = "01234";
}
}
}