Added new tests

This commit is contained in:
2022-12-06 23:02:54 -05:00
parent b4dc0aed37
commit 26a38fb9ea
14 changed files with 529 additions and 9 deletions

View File

@@ -32,7 +32,7 @@ import com.mattrixwv.NumberAlgorithms;
public class Problem35 extends Problem{ public class Problem35 extends Problem{
//Variables //Variables
//Static variables //Static variables
private static final int MAX_NUM = 999999; //The largest number that we are checking for primes protected static int maxNum = 999999; //The largest number that we are checking for primes
//Instance variables //Instance variables
private List<Integer> primes; //The primes below MAX_NUM private List<Integer> primes; //The primes below MAX_NUM
private ArrayList<Integer> circularPrimes; //The circular primes below MAX_NUM private ArrayList<Integer> circularPrimes; //The circular primes below MAX_NUM
@@ -49,7 +49,7 @@ public class Problem35 extends Problem{
} }
//Constructor //Constructor
public Problem35(){ public Problem35(){
super("How many circular primes are there below one million?"); super(String.format("How many circular primes are there below %d?", maxNum + 1));
primes = new ArrayList<>(); primes = new ArrayList<>();
circularPrimes = new ArrayList<>(); circularPrimes = new ArrayList<>();
} }
@@ -66,7 +66,7 @@ public class Problem35 extends Problem{
//Get all primes under 1,000,000 //Get all primes under 1,000,000
primes = NumberAlgorithms.getPrimes(MAX_NUM); primes = NumberAlgorithms.getPrimes(maxNum);
//Go through all primes, get all their rotations, and check if those numbers are also primes //Go through all primes, get all their rotations, and check if those numbers are also primes
for(int prime : primes){ for(int prime : primes){
boolean allRotationsPrime = true; boolean allRotationsPrime = true;
@@ -103,7 +103,7 @@ public class Problem35 extends Problem{
//Returns a string with the solution to the problem //Returns a string with the solution to the problem
public String getResult(){ public String getResult(){
solvedCheck("result"); solvedCheck("result");
return String.format("The number of all circular prime numbers under %d is %d", MAX_NUM, circularPrimes.size()); return String.format("The number of all circular prime numbers under %d is %d", maxNum, circularPrimes.size());
} }
//Returns the ArrayList of primes < MAX_NUM //Returns the ArrayList of primes < MAX_NUM
public List<Integer> getPrimes(){ public List<Integer> getPrimes(){

View File

@@ -32,14 +32,14 @@ import com.mattrixwv.Triple;
public class Problem39 extends Problem{ public class Problem39 extends Problem{
//Variables //Variables
//Static variables //Static variables
private static final long MAX_PERIMETER = 1000; protected static long maxPerimeter = 1000;
//Instace variables //Instace variables
private ArrayList<Triple<Long, Long, Long>> longestSolutions; private ArrayList<Triple<Long, Long, Long>> longestSolutions;
//Functions //Functions
//Constructor //Constructor
public Problem39(){ public Problem39(){
super("If p is the perimeter of a right triangle for which value of p <= 1000 is the number of solutions for the sides {a, b, c} maximized"); super(String.format("If p is the perimeter of a right triangle for which value of p <= %d is the number of solutions for the sides {a, b, c} maximized", maxPerimeter));
longestSolutions = new ArrayList<>(); longestSolutions = new ArrayList<>();
} }
//Operational functions //Operational functions
@@ -58,7 +58,7 @@ public class Problem39 extends Problem{
//Loop for perimeter <= 1000 //Loop for perimeter <= 1000
for(long perimeter = 1;perimeter <= MAX_PERIMETER;++perimeter){ for(long perimeter = 1;perimeter <= maxPerimeter;++perimeter){
ArrayList<Triple<Long, Long, Long>> solutions = new ArrayList<>(); ArrayList<Triple<Long, Long, Long>> solutions = new ArrayList<>();
for(long a = 1;(a * 3) <= perimeter;++a){ for(long a = 1;(a * 3) <= perimeter;++a){
for(long b = a;(a + b + b) <= perimeter;++b){ for(long b = a;(a + b + b) <= perimeter;++b){

View File

@@ -92,7 +92,7 @@ public class Problem40 extends Problem{
return String.format("The product is %d", product); return String.format("The product is %d", product);
} }
//Returns the string of the decimal //Returns the string of the decimal
public String irrationalDecimal(){ public String getIrrationalDecimal(){
solvedCheck("decimal string"); solvedCheck("decimal string");
return "0." + irrationalDecimal.toString(); return "0." + irrationalDecimal.toString();
} }

View File

@@ -37,7 +37,6 @@ public abstract class BaseTest{
} }
public abstract void testSolve(); public abstract void testSolve();
public void testSolve(Problem problem){ public void testSolve(Problem problem){
//Verify the results cannot be seen until the problem has been run //Verify the results cannot be seen until the problem has been run
assertThrows(Unsolved.class, () -> { problem.getResult(); }); assertThrows(Unsolved.class, () -> { problem.getResult(); });

View File

@@ -0,0 +1,48 @@
package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.mattrixwv.project_euler.exceptions.Unsolved;
public class TestProblem31 extends BaseTest{
@InjectMocks
private Problem31 problem;
static{
description = "How many different ways can 2 pounds be made using any number of coins?";
result = "There are 73682 ways to make 2 pounds with the given denominations of coins";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getPermutations(); });
super.testSolve(problem);
//Verify result
assertEquals(73682, problem.getPermutations());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
}

View File

@@ -0,0 +1,48 @@
package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.mattrixwv.project_euler.exceptions.Unsolved;
public class TestProblem32 extends BaseTest{
@InjectMocks
private Problem32 problem;
static{
description = "Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.";
result = String.format("There are 7 unique 1-9 pandigitals%nThe sum of the products of these pandigitals is 45228");
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getSumOfPandigitals(); });
super.testSolve(problem);
//Verify result
assertEquals(45228, problem.getSumOfPandigitals());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
}

View File

@@ -0,0 +1,54 @@
package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.List;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.mattrixwv.project_euler.exceptions.Unsolved;
public class TestProblem33 extends BaseTest{
@InjectMocks
private Problem33 problem;
static{
description = "If the product of these four fractions is given in its lowest common terms, find the value of the denominator";
result = "The denominator of the product is 100";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getNumerators(); });
assertThrows(Unsolved.class, () -> { problem.getDenominators(); });
assertThrows(Unsolved.class, () -> { problem.getProdDenominator(); });
super.testSolve(problem);
//Verify result
assertEquals(List.of(16, 26, 19, 49), problem.getNumerators());
assertEquals(List.of(64, 65, 95, 98), problem.getDenominators());
assertEquals(100, problem.getProdDenominator());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
}

View File

@@ -0,0 +1,52 @@
package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.List;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.mattrixwv.project_euler.exceptions.Unsolved;
public class TestProblem34 extends BaseTest{
@InjectMocks
private Problem34 problem;
static{
description = "Find the sum of all numbers which are equal to the sum of the factorial of their digits";
result = "The sum of all numbers that are the sum of their digit's factorials is 40730";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getFactorials(); });
assertThrows(Unsolved.class, () -> { problem.getSum(); });
super.testSolve(problem);
//Verify result
assertEquals(List.of(1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880), problem.getFactorials());
assertEquals(40730, problem.getSum());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
}

View File

@@ -0,0 +1,59 @@
package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.mattrixwv.project_euler.exceptions.Unsolved;
public class TestProblem35 extends BaseTest{
@InjectMocks
private Problem35Override problem;
static{
description = "How many circular primes are there below 10000?";
result = "The number of all circular prime numbers under 9999 is 33";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getPrimes(); });
assertThrows(Unsolved.class, () -> { problem.getCircularPrimes(); });
assertThrows(Unsolved.class, () -> { problem.getNumCircularPrimes(); });
super.testSolve(problem);
//Verify result
assertEquals(1229, problem.getPrimes().size());
assertEquals(33, problem.getCircularPrimes().size());
assertEquals(33, problem.getNumCircularPrimes());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
public static class Problem35Override extends Problem35{
static{
maxNum = 9999;
}
}
}

View File

@@ -0,0 +1,50 @@
package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.mattrixwv.project_euler.exceptions.Unsolved;
public class TestProblem36 extends BaseTest{
@InjectMocks
private Problem36 problem;
static{
description = "Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.";
result = "The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getPalindromes(); });
assertThrows(Unsolved.class, () -> { problem.getSumOfPalindromes(); });
super.testSolve(problem);
//Verify result
assertEquals(19, problem.getPalindromes().size());
assertEquals(872187, problem.getSumOfPalindromes());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
}

View File

@@ -0,0 +1,50 @@
package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.mattrixwv.project_euler.exceptions.Unsolved;
public class TestProblem37 extends BaseTest{
@InjectMocks
private Problem37 problem;
static{
description = "Find the sum of the only eleven primes that are both truncatable from left to right and right to left (2, 3, 5, and 7 are not counted).";
result = "The sum of all left and right truncatable primes is 748317";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getTruncatablePrimes(); });
assertThrows(Unsolved.class, () -> { problem.getSumOfPrimes(); });
super.testSolve(problem);
//Verify result
assertEquals(11, problem.getTruncatablePrimes().size());
assertEquals(748317L, problem.getSumOfPrimes());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
}

View File

@@ -0,0 +1,50 @@
package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.mattrixwv.project_euler.exceptions.Unsolved;
public class TestProblem38 extends BaseTest{
@InjectMocks
private Problem38 problem;
static{
description = "What is the largest 1-9 pandigital number that can be formed as the concatenated product of an integer with 1, 2, ... n where n > 1";
result = "The largest appended product pandigital is 932718654";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getLargestNum(); });
assertThrows(Unsolved.class, () -> { problem.getPandigital(); });
super.testSolve(problem);
//Verify result
assertEquals(9327L, problem.getLargestNum());
assertEquals(932718654L, problem.getPandigital());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
}

View File

@@ -0,0 +1,60 @@
package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.List;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.mattrixwv.Triple;
import com.mattrixwv.project_euler.exceptions.Unsolved;
public class TestProblem39 extends BaseTest{
@InjectMocks
private Problem39Override problem;
static{
description = "If p is the perimeter of a right triangle for which value of p <= 100 is the number of solutions for the sides {a, b, c} maximized";
result = "The perimeter with the largest number of solutions is 90";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getLongestSolutions(); });
assertThrows(Unsolved.class, () -> { problem.getLongestPerimeter(); });
super.testSolve(problem);
//Verify result
assertEquals(List.of(new Triple<>(9L, 40L, 41L), new Triple<>(15L, 36L, 39L)), problem.getLongestSolutions());
assertEquals(90, problem.getLongestPerimeter());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
public static class Problem39Override extends Problem39{
static{
maxPerimeter = 100;
}
}
}

View File

@@ -0,0 +1,50 @@
package com.mattrixwv.project_euler.problems;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.mattrixwv.project_euler.exceptions.Unsolved;
public class TestProblem40 extends BaseTest{
@InjectMocks
private Problem40 problem;
static{
description = "An irrational decimal fraction is created by concatenating the positive integers. Find the value of the following expression: d1 * d10 * d100 * d1000 * d10000 * d100000 * d1000000";
result = "The product is 210";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getIrrationalDecimal(); });
assertThrows(Unsolved.class, () -> { problem.getProduct(); });
super.testSolve(problem);
//Verify result
assertEquals(1000007, problem.getIrrationalDecimal().length());
assertEquals(210, problem.getProduct());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
}