Finished adding tests

This commit is contained in:
2022-12-06 23:59:16 -05:00
parent 26a38fb9ea
commit cf45461ecb
14 changed files with 562 additions and 4 deletions

View File

@@ -33,6 +33,8 @@ import com.mattrixwv.StringAlgorithms;
public class Problem43 extends Problem{
//Variables
//Static variables
protected static String nums = "0123456789";
//Instance variables
private ArrayList<Long> subPrimePandigitals;
@@ -55,7 +57,7 @@ public class Problem43 extends Problem{
//Get all of the 0-9 pandigitals
List<String> pandigitals = StringAlgorithms.getPermutations("0123456789");
List<String> pandigitals = StringAlgorithms.getPermutations(nums);
//Get all of the primes we need
List<Long> primes = NumberAlgorithms.getNumPrimes(7L);
//Break them into their component parts and see if they are divisible by the prime numbers

View File

@@ -111,6 +111,11 @@ public class Problem44 extends Problem{
solvedCheck("second pentagonal number");
return pentagonalNumber2;
}
//Returns the difference between the 2 pentagonal numbers
public long getDifference(){
solvedCheck("difference between the pentagonal numbers");
return Math.abs(pentagonalNumber1 - pentagonalNumber2);
}
}
/* Results:

View File

@@ -40,7 +40,7 @@ public class Problem50 extends Problem{
//Functions
//Constructor
public Problem50(){
super("Which prime, below one-million, can be written as the sum of the most consecutive primes?");
super(String.format("Which prime, below %d, can be written as the sum of the most consecutive primes?", MAX + 1));
consecutivePrimes = new ArrayList<>();
}
//Operational functions
@@ -130,7 +130,7 @@ public class Problem50 extends Problem{
//Returns a string with the solution to the problem
public String getResult(){
solvedCheck("results");
return String.format("The prime below one-million that can be written as the sum of the most consecutive primes is %d", ArrayAlgorithms.getLongSum(consecutivePrimes));
return String.format("The prime below %d that can be written as the sum of the most consecutive primes is %d", MAX + 1, ArrayAlgorithms.getLongSum(consecutivePrimes));
}
//Returns the list of consecutive primes
public List<Long> getConsecutivePrimes(){
@@ -147,6 +147,6 @@ public class Problem50 extends Problem{
}
/* Results:
The prime below one-million that can be written as the sum of the most consecutive primes is 997651
The prime below 1000000 that can be written as the sum of the most consecutive primes is 997651
It took an average of 77.425 milliseconds to run this problem through 100 iterations
*/

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 TestProblem41 extends BaseTest{
@InjectMocks
private Problem41 problem;
static{
description = "What is the largest n-digit pandigital prime?";
result = "The largest n-digit pandigital prime is 7652413";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getLargestPrimePandigital(); });
super.testSolve(problem);
//Verify result
assertEquals(7652413L, problem.getLargestPrimePandigital());
}
@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 TestProblem42 extends BaseTest{
@InjectMocks
private Problem42 problem;
static{
description = "Triangular number t(n) - (n * (n + 1)) / 2. For A = 1, B = 2, ... find the number of trinagular words in the file";
result = "The number of triangular numbers is 162";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getTriangularWords(); });
assertThrows(Unsolved.class, () -> { problem.getNumberTriangularNumbers(); });
super.testSolve(problem);
//Verify result
assertEquals(162, problem.getTriangularWords().size());
assertEquals(162, problem.getNumberTriangularNumbers());
}
@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 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 TestProblem43 extends BaseTest{
@InjectMocks
private Problem43Override problem;
static{
description = "Find the sum of all 0-9 pandigital numbers with this property";
result = "The sum of all pandigitals with the property is 1113342912";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getSubPrimePandigitals(); });
assertThrows(Unsolved.class, () -> { problem.getSumSubPrimePandigitals(); });
super.testSolve(problem);
//Verify result
assertEquals(List.of(140635728L, 146035728L, 410635728L, 416035728L), problem.getSubPrimePandigitals());
assertEquals(1113342912L, problem.getSumSubPrimePandigitals());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
public static class Problem43Override extends Problem43{
static{
nums = "012345678";
}
}
}

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 org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import com.mattrixwv.project_euler.exceptions.Unsolved;
public class TestProblem44 extends BaseTest{
@InjectMocks
private Problem44 problem;
static{
description = "Find 2 pentagonal numbers whos sum and difference are also pentagonal numbers";
result = "The difference of the pentagonal numbers is 5482660";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getPentagonalList(); });
assertThrows(Unsolved.class, () -> { problem.getPentagonalNumber1(); });
assertThrows(Unsolved.class, () -> { problem.getPentagonalNumber2(); });
assertThrows(Unsolved.class, () -> { problem.getDifference(); });
super.testSolve(problem);
//Verify result
assertEquals(2167, problem.getPentagonalList().size());
assertEquals(1560090L, problem.getPentagonalNumber1());
assertEquals(7042750L, problem.getPentagonalNumber2());
assertEquals(5482660L, problem.getDifference());
}
@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 TestProblem45 extends BaseTest{
@InjectMocks
private Problem45 problem;
static{
description = "Find the next triangle number after 40755 that is also pentagonal and hexagonal";
result = "The next triangular/pentagonal/hexagonal number is 1533776805";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getNum(); });
super.testSolve(problem);
//Verify result
assertEquals(1533776805L, problem.getNum());
}
@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 TestProblem46 extends BaseTest{
@InjectMocks
private Problem46 problem;
static{
description = "What is the smallest odd composite number that connot be written as the sum of a prime and twice a square?";
result = "The smallest odd composite that cannot be written as the sum of a prime and twice a square is 5777";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getCompositeNum(); });
super.testSolve(problem);
//Verify result
assertEquals(5777, problem.getCompositeNum());
}
@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 TestProblem47 extends BaseTest{
@InjectMocks
private Problem47 problem;
static{
description = "What is the first of four consecutive integers to have four distinct prime factors each?";
result = "The first number is 134043";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getFirstNum(); });
super.testSolve(problem);
//Verify result
assertEquals(134043, problem.getFirstNum());
}
@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.math.BigInteger;
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 TestProblem48 extends BaseTest{
@InjectMocks
private Problem48 problem;
static{
description = "Find the last ten digits of the series Σn^n for 1<=n<=1000";
result = "The last ten digits of the series Σn^n for 1<=n<=1000 is 9110846700";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getSum(); });
assertThrows(Unsolved.class, () -> { problem.getSumDigits(); });
super.testSolve(problem);
//Verify result
assertEquals(new BigInteger("1000368199144695177095375011227646795567793680622934654583760988100234910747716194381428659099527845945869942643191290894720342979906407679647259860434238468038326040809691037615370376237713648510063115732951461774246705584266865759601815843666442832284556880313114548151539190975398485496645576513465858582712336401166221956188173449531674102688908321764663020306699770408625340766091595022791379368098369306375602813856646358773751558775213460225796579846583334007349358624342339332981334571237888809283103348760261360175950815609179464026871005243652109980863552142014242903434068560936573231079342194031864413918101238151056509267393515760392842472501391594073463001521843811073767021711026307504695733467897821866906648469828346607412967395801797791683609834722432241952845352564681868240369569566192825555323558078061997527689983848863374786789331581565252059172614339424600986143259233167583371070362625554531852054166117148858229508581589614337594463277554380518380921301218836327102231407332201109740102580216469298331766920619646083790732807627360614428085171565006289728508688964226799647192582924058589530750674578385365561878559589685756225692348914746922810913915619834754117648358035814128670294158565669942087736286390942241547226015004471330630113072042704288905042142628193771918594574302202147201188486345913190833752307476966010547423928871063118783026036381319039052008252072057933666712918946233312793697094074224187872045970976444309242782187738320257490080824330074991698698239561125811127607863900355221737846690567707344074494145266662103839812840216303448476913957072355732716627098372245223046792919747259113157425824064858331415400943278213042954635053574045209984512221264241903550178416824551412548637590007779082539288247751653566899882749594405895102587985539527709493510049546445427265617478399107188238681771215904234119392247489751079085948055945098805617963722928469554263782217625160428008228845552540344494860195267115187092227766195753907211126646150140614744233974765273475619964311852858614167819668340124730487710162006793529985758820653677274379563313495454526632718723482339494825759821076401694316043456512117937935456463521463021197726694983558929132357576188594977516630734212863869456164205525536767311298137182511494649463663073759219213056823561667776093739425742883930712609962163464088038826569132032160692637206183085942987973684584276491784843115472077900401692595694119273553511025991265446039366288921743581333200083717105241171504606883543418862024047552177055263424469501298905901938158245938633694105024815166679813689156668341197713475094389904887126794468901893850475050011205225742455555625750560213230387910337983950333245020653238989115507013882956277763880795687210857196493893142656713105966275422144605988058939600603604226921401402096519294250488670297983396353279460453142375542267881989197481789780678955093763193658603690898474826976906544473978017455720367929981796023041785852626797271283465789498383642350667978127819110846700"), problem.getSum());
assertEquals("9110846700", problem.getSumDigits());
}
@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 TestProblem49 extends BaseTest{
@InjectMocks
private Problem49 problem;
static{
description = "What is the 12-digit number formed by concatenating the three terms in the sequence?";
result = "The 12-digit number formed by concatenation the three terms in the sequence is 296962999629";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getConcatNums(); });
super.testSolve(problem);
//Verify result
assertEquals("296962999629", problem.getConcatNums());
}
@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 TestProblem50 extends BaseTest{
@InjectMocks
private Problem50 problem;
static{
description = "Which prime, below 1000000, can be written as the sum of the most consecutive primes?";
result = "The prime below 1000000 that can be written as the sum of the most consecutive primes is 997651";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getConsecutivePrimes(); });
assertThrows(Unsolved.class, () -> { problem.getPrime(); });
super.testSolve(problem);
//Verify result
assertEquals(543, problem.getConsecutivePrimes().size());
assertEquals(997651L, problem.getPrime());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
}

View File

@@ -0,0 +1,46 @@
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 TestProblem67 extends BaseTest{
@InjectMocks
private Problem67 problem;
static{
description = "Find the maximum total from top to bottom";
result = "The value of the longest path is 7273";
}
@Test
@Order(1)
@Override
public void testDescription(){
super.testDescription(problem);
}
@Test
@Order(2)
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getTotal(); });
super.testSolve(problem);
//Verify result
assertEquals(7273, problem.getTotal());
}
@Test
@Order(3)
@Override
public void testReset(){
super.testReset(problem);
}
}