Updated to work with new library layout

This commit is contained in:
2021-07-03 21:44:18 -04:00
parent 85012be5e8
commit 290e43173a
39 changed files with 374 additions and 595 deletions

View File

@@ -1,10 +1,10 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 06-17-20
//Modified: 07-03-21
//This is a base class for problems to use as a template
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -21,10 +21,12 @@
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.exceptions.InvalidResult;
public abstract class Problem{
//Variables
//Instance variables
@@ -59,6 +61,11 @@ public abstract class Problem{
}
return timer;
}
void solvedCheck(String str){
if(!solved){
throw new Unsolved("You must solve the problem before you can see the " + str);
}
}
//Solve the problem
public abstract void solve() throws InvalidResult;
//Reset the problem so it can be run again

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem1.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 10-26-20
//Modified: 07-03-21
//What is the sum of all the multiples of 3 or 5 that are less than 1000
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,9 +23,6 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem1 extends Problem{
//Variables
//Static variables
@@ -51,9 +48,11 @@ public class Problem1 extends Problem{
//Start the timer
timer.start();
//Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap
fullSum = sumOfProgression(3) + sumOfProgression(5) - sumOfProgression(3 * 5);
//Stop the timer
timer.stop();
@@ -76,21 +75,17 @@ public class Problem1 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The sum of all numbers < %d is %d", (TOP_NUM + 1), fullSum);
}
//Returns the requested sum
public int getSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum");
return fullSum;
}
}
/* Results:
The sum of all numbers < 1000 is 233168
It took an average of 298.000 nanoseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem10.java
//Matthew Ellison
// Created: 03-03-19
//Modified: 08-27-20
//Modified: 07-03-21
//Find the sum of all the primes below two million
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,8 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.ArrayAlgorithms;
import mattrixwv.NumberAlgorithms;
public class Problem10 extends Problem{
@@ -52,8 +52,10 @@ public class Problem10 extends Problem{
//Start the timer
timer.start();
//Get the sum of all prime numbers < GOAL_NUMBER
sum = Algorithms.getLongSum(Algorithms.getPrimes(GOAL_NUMBER)); //Subtract 1 from the number so that it is < the number
sum = ArrayAlgorithms.getLongSum(NumberAlgorithms.getPrimes(GOAL_NUMBER)); //Subtract 1 from the number so that it is < the number
//Stop the timer
timer.stop();
@@ -71,21 +73,17 @@ public class Problem10 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The sum of all the primes < %d is %d", GOAL_NUMBER + 1, sum);
}
//Returns the sum that was requested
public long getSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum");
return sum;
}
}
/* Results:
The sum of all the primes < 2000000 is 142913828922
It took an average of 186.751 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem11.java
//Matthew Ellison
// Created: 03-03-19
//Modified: 08-27-20
//Modified: 07-03-21
//What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
/*
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
@@ -27,7 +27,7 @@
*/
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -47,8 +47,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.ArrayAlgorithms;
public class Problem11 extends Problem{
@@ -105,6 +104,7 @@ public class Problem11 extends Problem{
//Start the timer
timer.start();
//Loop through every row and column
for(int row = 0;row < grid.length;++row){
for(int col = 0;col < grid[row].length;++col){
@@ -134,7 +134,7 @@ public class Problem11 extends Problem{
currentProduct.set(3, grid[row][col + 3]);
//If the current number's product is greater than the greatest product replace it
if(Algorithms.getProd(currentProduct) > Algorithms.getProd(greatestProduct)){
if(ArrayAlgorithms.getProd(currentProduct) > ArrayAlgorithms.getProd(greatestProduct)){
greatestProduct.set(0, currentProduct.get(0));
greatestProduct.set(1, currentProduct.get(1));
greatestProduct.set(2, currentProduct.get(2));
@@ -150,7 +150,7 @@ public class Problem11 extends Problem{
currentProduct.set(3, grid[row + 3][col]);
//If the current number's product is greater than the greatest product replace it
if(Algorithms.getProd(currentProduct) > Algorithms.getProd(greatestProduct)){
if(ArrayAlgorithms.getProd(currentProduct) > ArrayAlgorithms.getProd(greatestProduct)){
greatestProduct.set(0, currentProduct.get(0));
greatestProduct.set(1, currentProduct.get(1));
greatestProduct.set(2, currentProduct.get(2));
@@ -166,7 +166,7 @@ public class Problem11 extends Problem{
currentProduct.set(3, grid[row + 3][col - 3]);
//If the current number's product is greater than the greatest product replace it
if(Algorithms.getProd(currentProduct) > Algorithms.getProd(greatestProduct)){
if(ArrayAlgorithms.getProd(currentProduct) > ArrayAlgorithms.getProd(greatestProduct)){
greatestProduct.set(0, currentProduct.get(0));
greatestProduct.set(1, currentProduct.get(1));
greatestProduct.set(2, currentProduct.get(2));
@@ -182,7 +182,7 @@ public class Problem11 extends Problem{
currentProduct.set(3, grid[row + 3][col + 3]);
//If the current number's product is greater than the greatest product replace it
if(Algorithms.getProd(currentProduct) > Algorithms.getProd(greatestProduct)){
if(ArrayAlgorithms.getProd(currentProduct) > ArrayAlgorithms.getProd(greatestProduct)){
greatestProduct.set(0, currentProduct.get(0));
greatestProduct.set(1, currentProduct.get(1));
greatestProduct.set(2, currentProduct.get(2));
@@ -192,6 +192,7 @@ public class Problem11 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -208,29 +209,22 @@ public class Problem11 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
return String.format("The greatest product of 4 numbers in a line is %d\nThe numbers are %s", Algorithms.getProd(greatestProduct), greatestProduct.toString());
solvedCheck("result");
return String.format("The greatest product of 4 numbers in a line is %d\nThe numbers are %s", ArrayAlgorithms.getProd(greatestProduct), greatestProduct.toString());
}
//Returns the numbers that were being searched
public ArrayList<Integer> getNumbers(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("numbers");
return greatestProduct;
}
//Returns the product that was requested
public int getProduct(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
return Algorithms.getProd(greatestProduct);
solvedCheck("product of the numbers");
return ArrayAlgorithms.getProd(greatestProduct);
}
}
/* Results:
The greatest product of 4 numbers in a line is 70600674
The numbers are [89, 94, 97, 87]

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 08-27-20
//Modified: 07-03-21
//What is the value of the first triangle number to have over five hundred divisors?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -25,8 +25,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.NumberAlgorithms;
public class Problem12 extends Problem{
@@ -60,9 +59,10 @@ public class Problem12 extends Problem{
//Start the timer
timer.start();
//Loop until you find the appropriate number
while((!foundNumber) && (sum > 0)){
divisors = Algorithms.getDivisors(sum);
divisors = NumberAlgorithms.getDivisors(sum);
//If the number of divisors is correct set the flag
if(divisors.size() > GOAL_DIVISORS.intValue()){
foundNumber = true;
@@ -74,6 +74,7 @@ public class Problem12 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -91,45 +92,32 @@ public class Problem12 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The triangular number %d is the sum of all numbers >= %d and has %d divisors", sum, counter - 1, divisors.size());
}
//Returns the triangular number
public long getTriangularNumber(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("triangular number");
return sum;
}
//Get the final number that was added to the triangular number
public long getLastNumberAdded(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("last number added to get the triangular number");
return counter - 1;
}
//Returns the list of divisors of the requested number
public ArrayList<Long> getDivisorsOfTriangularNumber(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("divisors of the triangular number");
return divisors;
}
//Returns the number of divisors of the requested number
public int getNumberOfDivisors(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("number of divisors of the triangular number");
return divisors.size();
}
}
/* Results:
The triangular number 76576500 is the sum of all numbers >= 12375 and has 576 divisors
It took an average of 344.173 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem13.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 08-27-20
//Modified: 07-03-21
//Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
/*
37107287533902102798797998220837590246510135740250
@@ -107,7 +107,7 @@
*/
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -128,8 +128,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.math.BigInteger;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.ArrayAlgorithms;
public class Problem13 extends Problem{
@@ -269,8 +268,10 @@ public class Problem13 extends Problem{
//Setup the array
setNums();
//Get the sum of all the numbers
sum = Algorithms.getBigSum(nums);
sum = ArrayAlgorithms.getBigSum(nums);
//Stop the timer
timer.stop();
@@ -290,29 +291,22 @@ public class Problem13 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
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 of 50-digit numbers
public ArrayList<BigInteger> getNumbers(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("numbers");
return nums;
}
//Returns the sum of the 50-digit numbers
public BigInteger getSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum");
return sum;
}
}
/* Results:
The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672
The first 10 digits of the sum of the numbers is 5537376230

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem14.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 08-27-20
//Modified: 07-03-21
/*
The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
@@ -10,7 +10,7 @@ Which starting number, under one million, produces the longest chain?
*/
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -28,9 +28,6 @@ Which starting number, under one million, produces the longest chain?
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem14 extends Problem{
//Variables
//Static variables
@@ -58,6 +55,7 @@ public class Problem14 extends Problem{
//Start the timer
timer.start();
//Loop through all numbers <= MAX_NUM and check them against the series
for(long currentNum = 1L;currentNum <= MAX_NUM;++currentNum){
long currentLength = checkSeries(currentNum);
@@ -68,6 +66,7 @@ public class Problem14 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -103,29 +102,22 @@ public class Problem14 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The number %d produced a chain of %d steps", maxNum, maxLength);
}
//Returns the length of the requested chain
public long getLength(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("length of the longest chain");
return maxLength;
}
//Returns the starting number of the requested chain
public long getStartingNumber(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("starting number of the longest chain");
return maxNum;
}
}
/* Results:
The number 837799 produced a chain of 525 steps
It took an average of 286.004 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem15.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 08-27-20
//Modified: 07-03-21
//How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,9 +23,6 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem15 extends Problem{
//Variables
//Static vaiables
@@ -52,10 +49,12 @@ public class Problem15 extends Problem{
//Start the timer
timer.start();
//We write this as a recursive function
//When in a location it always moves right first, then down
move(0, 0);
//Stop the timer
timer.stop();
@@ -91,21 +90,17 @@ public class Problem15 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The number of routes is %d", numOfRoutes);
}
//Returns the number of routes found
public long getNumberOfRoutes(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("number of routes");
return numOfRoutes;
}
}
/* Results:
The number of routes is 137846528820
It took an average of 20.702 minutes to run this problem through 10 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem16.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 08-27-20
//Modified: 07-03-21
//What is the sum of the digits of the number 2^1000?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -25,8 +25,6 @@ package mattrixwv.ProjectEuler.Problems;
import java.math.BigInteger;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem16 extends Problem{
//Variables
@@ -56,6 +54,7 @@ public class Problem16 extends Problem{
//Start the timer
timer.start();
//Get the number
num = BigInteger.valueOf(NUM_TO_POWER).pow(POWER);
@@ -67,6 +66,7 @@ public class Problem16 extends Problem{
sumOfElements += Integer.parseInt(numString.substring(cnt, cnt + 1));
}
//Stop the timer
timer.stop();
@@ -84,29 +84,22 @@ public class Problem16 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("%d^%d = %s\nThe sum of the elements is %d", NUM_TO_POWER, POWER, num.toString(), sumOfElements);
}
//Returns the number that was calculated
public BigInteger getNumber(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("number");
return num;
}
//Return the sum of the digits of the number
public int getSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum");
return sumOfElements;
}
}
/* Results:
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
The sum of the elements is 1366

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem17.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 08-27-20
//Modified: 07-03-21
//If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -52,6 +52,7 @@ public class Problem17 extends Problem{
//Start the timer
timer.start();
//Start with 1 and increment
for(int num = START_NUM;num <= STOP_NUM;++num){
//Pass the number to a function that will create a string for the number
@@ -62,6 +63,7 @@ public class Problem17 extends Problem{
letterCount += getNumberChars(currentNumString);
}
//Stop the timer
timer.stop();
@@ -189,21 +191,17 @@ public class Problem17 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The sum of all the letters in all the numbers %d-%d is %d", START_NUM, STOP_NUM, letterCount);
}
//Returns the number of letters asked for
public long getLetterCount(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("letter count");
return letterCount;
}
}
/* Results:
The sum of all the letters in all the numbers 1-1000 is 21124
It took an average of 216.210 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java
//Matthew Ellison
// Created: 03-11-19
//Modified: 08-27-20
//Modified: 07-03-21
//Find the maximum total from top to bottom
/*
75
@@ -23,7 +23,7 @@
//This is done using a breadth first search
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -45,8 +45,6 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem18 extends Problem{
//Structures
@@ -127,6 +125,7 @@ public class Problem18 extends Problem{
//Start the timer
timer.start();
//Invert the list
invert(list);
@@ -178,6 +177,7 @@ public class Problem18 extends Problem{
//Get the correct total which will be the inversion of the current one
actualTotal = ((100 * list.size()) - foundPoints.get(foundPoints.size() - 1).total);
//Stop the timer
timer.stop();
@@ -195,13 +195,12 @@ public class Problem18 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The value of the longest path is %d", actualTotal);
}
//Returns the pyramid that was traversed as a string
public String getPyramid(){
solvedCheck("pyramid of numbers");
StringBuilder results = new StringBuilder();
//Loop through all elements of the list and print them
for(ArrayList<Integer> row : list){
@@ -214,10 +213,7 @@ public class Problem18 extends Problem{
}
//Returns the trail the algorithm took as a string
public String getTrail(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("trail of the shortest path");
StringBuilder results = new StringBuilder();
//Save the trail the algorithm took
@@ -276,14 +272,12 @@ public class Problem18 extends Problem{
}
//Returns the total that was asked for
public int getTotal(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("total");
return actualTotal;
}
}
/* Results:
The value of the longest path is 1074
It took an average of 198.418 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem19.java
//Matthew Ellison
// Created: 03-13-19
//Modified: 08-27-20
//Modified: 07-03-21
//How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
/*
You are given the following information, but you may prefer to do some research for yourself.
@@ -16,7 +16,7 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
*/
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -33,7 +33,6 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem19 extends Problem{
//Variables
@@ -63,6 +62,7 @@ public class Problem19 extends Problem{
//Start the timer
timer.start();
//Run for all years 1901-2000
for(int year = START_YEAR;year <= END_YEAR;++year){
//Run for all months in the year
@@ -77,6 +77,7 @@ public class Problem19 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -195,21 +196,17 @@ public class Problem19 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("There are %d Sundays that landed on the first of the months from %d to %d", totalSundays, START_YEAR, END_YEAR);
}
//Returns the total sundays that were asked for
public long getTotalSundays(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("total number of sundays");
return totalSundays;
}
}
/* Results:
There are 171 Sundays that landed on the first of the months from 1901 to 2000
It took an average of 4.656 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem2.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 08-27-20
//Modified: 07-03-21
//The sum of the even Fibonacci numbers less than 4,000,000
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -25,8 +25,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.NumberAlgorithms;
public class Problem2 extends Problem{
@@ -54,8 +53,9 @@ public class Problem2 extends Problem{
//Start the timer
timer.start();
//Get a list of all fibonacci numbers <= TOP_NUM
ArrayList<Integer> fibNums = Algorithms.getAllFib(TOP_NUM);
ArrayList<Integer> fibNums = NumberAlgorithms.getAllFib(TOP_NUM);
//Step through every element in the list checking if it is even
for(int num : fibNums){
//If the number is even add it to the running tally
@@ -64,6 +64,7 @@ public class Problem2 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -80,21 +81,17 @@ public class Problem2 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The sum of all even fibonacci numbers <= %d is %d", TOP_NUM, fullSum);
}
//Returns the requested sum
public int getSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum");
return fullSum;
}
}
/* Results:
The sum of all even fibonacci numbers <= 3999999 is 4613732
It took an average of 29.713 microseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem20.java
//Matthew Ellison
// Created: 03-14-19
//Modified: 08-27-20
//Modified: 07-03-21
//What is the sum of the digits of 100!?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -25,8 +25,6 @@ package mattrixwv.ProjectEuler.Problems;
import java.math.BigInteger;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem20 extends Problem{
//Variables
@@ -55,6 +53,7 @@ public class Problem20 extends Problem{
//Start the timer
timer.start();
//Run through every number from 1 to 100 and multiply it by the current num to generate 100!
for(int cnt = TOP_NUM;cnt > 1;--cnt){
num = num.multiply(BigInteger.valueOf(cnt));
@@ -68,6 +67,7 @@ public class Problem20 extends Problem{
sum += Integer.valueOf(digit.toString());
}
//Stop the timer
timer.stop();
@@ -85,37 +85,28 @@ public class Problem20 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("%d! = %s\nThe sum of the digits is: %d", TOP_NUM, num.toString(), sum);
}
//Returns the number 100!
public BigInteger getNumber(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("number");
return num;
}
//Returns the number 100! in a string
public String getNumberString(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("number as a string");
return num.toString(10);
}
//Returns the sum of the digits of 100!
public long getSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum of the digits");
return sum;
}
}
/* Restuls:
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
The sum of the digits is: 648

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem21.java
//Matthew Ellison
// Created: 03-18-19
//Modified: 08-27-20
//Modified: 07-03-21
//Evaluate the sum of all the amicable numbers under 10000
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,8 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.ArrayAlgorithms;
import mattrixwv.NumberAlgorithms;
import java.util.ArrayList;
import java.util.Collections;
@@ -66,13 +66,14 @@ public class Problem21 extends Problem{
//Start the timer
timer.start();
//Generate the divisors of all numbers < 10000, get their sum, and add it to the list
for(int cnt = 1;cnt < LIMIT;++cnt){
ArrayList<Integer> divisors = Algorithms.getDivisors(cnt); //Get all the divisors of a number
ArrayList<Integer> divisors = NumberAlgorithms.getDivisors(cnt); //Get all the divisors of a number
if(divisors.size() > 1){
divisors.remove(divisors.get(divisors.size() - 1)); //Remove the last entry because it will be the number itself
}
divisorSum.set(cnt, Algorithms.getSum(divisors)); //Add the sum of the divisors of the vector
divisorSum.set(cnt, ArrayAlgorithms.getSum(divisors)); //Add the sum of the divisors of the vector
}
//Check every sum of divisors in the list for a matching sum
for(int cnt = 1;cnt < divisorSum.size();++cnt){
@@ -95,6 +96,7 @@ public class Problem21 extends Problem{
//Sort the arraylist for neatness
Collections.sort(amicable);
//Stop the timer
timer.stop();
@@ -113,35 +115,28 @@ public class Problem21 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
StringBuilder result = new StringBuilder(String.format("All amicable numbers less than %d are\n", LIMIT));
for(int cnt = 0;cnt < amicable.size();++cnt){
result.append(String.format("%d\n", amicable.get(cnt)));
}
result.append(String.format("The sum of all of these amicable numbers is %d", Algorithms.getSum(amicable)));
result.append(String.format("The sum of all of these amicable numbers is %d", ArrayAlgorithms.getSum(amicable)));
return result.toString();
}
//Returns a vector with all of the amicable numbers calculated
public ArrayList<Integer> getAmicable(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("amicable numbers");
return amicable;
}
//Returns the sum of all of the amicable numbers
public int getSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
return Algorithms.getSum(amicable);
solvedCheck("sum of the amicable numbers");
return ArrayAlgorithms.getSum(amicable);
}
}
/* Results:
All amicable numbers less than 10000 are
220

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java
//Matthew Ellison
// Created: 03-20-19
//Modified: 08-27-20
//Modified: 07-03-21
//What is the total of all the name scores in this file?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,7 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.ArrayAlgorithms;
import java.util.ArrayList;
import java.util.Arrays;
@@ -445,7 +444,7 @@ public class Problem22 extends Problem{
}
//Get the sum of all the numbers
sum = Algorithms.getLongSum(prod);
sum = ArrayAlgorithms.getLongSum(prod);
//Stop the timer
timer.stop();
@@ -465,9 +464,7 @@ public class Problem22 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The answer to the question is %d", sum);
}
//Returns the vector of the names being scored
@@ -476,14 +473,12 @@ public class Problem22 extends Problem{
}
//Returns the sum of the names scores
public long getNameScoreSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("score of the names");
return sum;
}
}
/* Results:
The answer to the question is 871198282
It took an average of 2.609 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java
//Matthew Ellison
// Created: 03-22-19
//Modified: 08-27-20
//Modified: 07-03-21
//Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,8 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.ArrayAlgorithms;
import mattrixwv.NumberAlgorithms;
import java.util.ArrayList;
@@ -65,14 +65,15 @@ public class Problem23 extends Problem{
//Start the timer
timer.start();
//Get the sum of the divisors of all numbers < MAX_NUM
for(int cnt = 1;cnt < MAX_NUM;++cnt){
ArrayList<Integer> div = Algorithms.getDivisors(cnt);
ArrayList<Integer> div = NumberAlgorithms.getDivisors(cnt);
//Remove the last element, which is the number itself. This gives us the propper divisors
if(div.size() > 1){
div.remove(div.size() - 1);
}
divisorSums.set(cnt, Algorithms.getSum(div));
divisorSums.set(cnt, ArrayAlgorithms.getSum(div));
}
//Get the abundant numbers
@@ -90,6 +91,7 @@ public class Problem23 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -127,21 +129,17 @@ public class Problem23 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The answer is %d", sum);
}
//Returns the sum of the numbers asked for
public long getSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum");
return sum;
}
}
/* Results:
The answer is 4179871
It took an average of 15.048 seconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem24.java
//Matthew Ellison
// Created: 03-24-19
//Modified: 08-27-20
//Modified: 07-03-21
//What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,7 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.StringAlgorithms;
import java.util.ArrayList;
@@ -55,8 +54,10 @@ public class Problem24 extends Problem{
//Start the timer
timer.start();
//Get all the permutations of the string
permutations = Algorithms.getPermutations(nums);
permutations = StringAlgorithms.getPermutations(nums);
//Stop the timer
timer.stop();
@@ -74,29 +75,22 @@ public class Problem24 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The 1 millionth permutation is %s", permutations.get(NEEDED_PERM - 1));
}
//Returns an ArrayList with all of the permutations
public ArrayList<String> getPermutationsList(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("permutations");
return permutations;
}
//Returns the requested permutation
public String getPermutation(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("1,000,000th permutation");
return permutations.get(NEEDED_PERM - 1);
}
}
/* Results
The 1 millionth permutation is 2783915460
It took an average of 1.140 seconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem25.java
//Matthew Ellison
// Created: 03-25-19
//Modified: 08-27-20
//Modified: 07-03-21
//What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,7 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.NumberAlgorithms;
import java.math.BigInteger;
@@ -56,12 +55,14 @@ public class Problem25 extends Problem{
//Start the timer
timer.start();
//Move through all Fibonacci numbers until you reach the one with at least NUM_DIGITS digits
while(number.toString().length() < NUM_DIGITS){
index = index.add(BigInteger.ONE); //Increase the index number. Doing this at the beginning keeps the index correct at the end of the loop
number = Algorithms.getFib(index); //Calculate the number
number = NumberAlgorithms.getFib(index); //Calculate the number
}
//Stop the timer
timer.stop();
@@ -79,53 +80,37 @@ public class Problem25 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The first Fibonacci number with %d digits is %s\nIts index is %d", NUM_DIGITS, number.toString(), index);
}
//Returns the Fibonacci number asked for
public BigInteger getNumber(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("fibonacci number");
return number;
}
//Returns the Fibonacci number asked for as a string
public String getNumberString(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("fibonacci number as a string");
return number.toString(10);
}
//Returns the index of the requested Fibonacci number
public BigInteger getIndex(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("index of the fibonacci number");
return index;
}
//Returns the index of the requested Fibonacci number as a string
public String getIndexString(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("index of the fibonacci number as a string");
return index.toString(10);
}
//Returns the index of the requested Fibonacci number as a long
public long getIndexInt(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("index of the fibonacci number as an int");
return index.longValue();
}
}
/* Results:
The first Fibonacci number with 1000 digits is 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816
Its index is 4782

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem26.java
//Matthew Ellison
// Created: 07-28-19
//Modified: 08-27-20
//Modified: 07-03-21
//Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,6 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
import java.util.ArrayList;
@@ -54,6 +52,7 @@ public class Problem26 extends Problem{
//Start the timer
timer.start();
//Start with 1/2 and find out how long the longest cycle is by checking the remainders
//Loop through every number from 2-999 and use it for the denominator
for(int denominator = 2;denominator <= TOP_NUM;++denominator){
@@ -90,6 +89,7 @@ public class Problem26 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -106,29 +106,22 @@ public class Problem26 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The longest cycle is %d digits long\nIt started with the number %d", longestCycle, longestNumber);
}
//Returns the length of the longest cycle
public int getLongestCycle(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("length of the longest cycle");
return longestCycle;
}
//Returns the denominator that starts the longest cycle
public int getLongestNumber(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("denominator that starts the longest cycle");
return longestNumber;
}
}
/* Results:
The longest cycle is 982 digits long
It started with the number 983

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem27.java
//Matthew Ellison
// Created: 09-15-19
//Modified: 08-27-20
//Modified: 07-03-21
//Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,8 +23,7 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.NumberAlgorithms;
public class Problem27 extends Problem{
@@ -57,6 +56,7 @@ public class Problem27 extends Problem{
//Start the timer
timer.start();
//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
@@ -64,7 +64,7 @@ public class Problem27 extends Problem{
//Start with n=0 and check the formula to see how many primes you can get with concecutive n's
int n = 0;
int quadratic = (n * n) + (a * n) + b;
while(Algorithms.isPrime(quadratic)){
while(NumberAlgorithms.isPrime(quadratic)){
++n;
quadratic = (n * n) + (a * n) + b;
}
@@ -79,6 +79,7 @@ public class Problem27 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -97,45 +98,32 @@ public class Problem27 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The greatest number of primes found is %d\nIt was found with A = %d, B = %d\nThe product of A and B is %d", topN, topA, topB, topA * topB);
}
//Returns the top A that was generated
public int getTopA(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("largest A");
return topA;
}
//Returns the top B that was generated
public int getTopB(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("largest B");
return topB;
}
//Returns the top N that was generated
public int getTopN(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("largest N");
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();
}
solvedCheck("product of A and B");
return topA * topB;
}
}
/* Results:
The greatest number of primes found is 70
It was found with A = -61, B = 971

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem28.java
//Matthew Ellison
// Created: 09-22-19
//Modified: 08-27-20
//Modified: 07-03-21
//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
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -25,8 +25,6 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem28 extends Problem{
//Variables
@@ -126,11 +124,13 @@ public class Problem28 extends Problem{
//Start the timer
timer.start();
//Setup the grid
setupGrid();
//Find the sum of the diagonals in the grid
findSum();
//Stop the timer
timer.stop();
@@ -147,30 +147,23 @@ public class Problem28 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The sum of the diagonals in the given grid is %d", sumOfDiagonals);
}
//Returns the grid
public ArrayList<ArrayList<Integer>> getGrid(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("grid");
return grid;
}
//Returns the sum of the diagonals
public int getSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum");
return sumOfDiagonals;
}
}
/* Results:
The sum of the diagonals in the given grid is 669171001
It took an average of 17.307 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem29.java
//Matthew Ellison
// Created: 10-09-19
//Modified: 08-27-20
//Modified: 07-03-21
//How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -26,8 +26,6 @@ package mattrixwv.ProjectEuler.Problems;
import java.math.BigInteger;
import java.util.ArrayList;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem29 extends Problem{
//Variables
@@ -57,6 +55,7 @@ public class Problem29 extends Problem{
//Start the timer
timer.start();
//Start with the first A and move towards the top
for(int currentA = BOTTOM_A;currentA <= TOP_A;++currentA){
//Start with the first B and move towards the top
@@ -70,6 +69,7 @@ public class Problem29 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -86,9 +86,7 @@ public class Problem29 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
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
@@ -109,14 +107,17 @@ public class Problem29 extends Problem{
}
//Returns a vector of all the unique values for a^b
public ArrayList<BigInteger> getUnique(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("the unique values for a^b");
return unique;
}
//Returns the number of unique values for a^b
public int getNumUnique(){
solvedCheck("the number of unique values for a^b");
return unique.size();
}
}
/* Results:
The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183
It took an average of 118.773 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem3.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 08-27-20
//Modified: 07-03-21
//The largest prime factor of 600851475143
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -25,8 +25,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.NumberAlgorithms;
import mattrixwv.exceptions.InvalidResult;
@@ -55,10 +54,12 @@ public class Problem3 extends Problem{
//Start the timer
timer.start();
//Get all the factors of the number
factors = Algorithms.getFactors(GOAL_NUMBER);
factors = NumberAlgorithms.getFactors(GOAL_NUMBER);
//The last element should be the largest factor
//Stop the timer
timer.stop();
@@ -75,25 +76,17 @@ public class Problem3 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The largest factor of the number %d is %d", GOAL_NUMBER, factors.get(factors.size() - 1));
}
//Returns the list of factors of the number
public ArrayList<Long> getFactors(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("factors");
return factors;
}
//Returns the largest factor of the number
public long getLargestFactor(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("largest factor");
return factors.get(factors.size() - 1);
}
//Returns the number for which we are getting the factor
@@ -102,6 +95,7 @@ public class Problem3 extends Problem{
}
}
/* Results:
The largest factor of the number 600851475143 is 6857
It took an average of 48.209 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem30.java
//Matthew Ellison
// Created: 10-27-19
//Modified: 08-27-20
//Modified: 07-03-21
//Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -25,8 +25,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.ArrayAlgorithms;
public class Problem30 extends Problem{
@@ -70,6 +69,7 @@ public class Problem30 extends Problem{
//Start the timer
timer.start();
//Start with the lowest number and increment until you reach the largest number
for(long currentNum = BOTTOM_NUM;currentNum <= TOP_NUM;++currentNum){
//Get the digits of the number
@@ -87,7 +87,8 @@ public class Problem30 extends Problem{
}
//Get the sum of the numbers
sum = Algorithms.getLongSum(sumOfFifthNumbers);
sum = ArrayAlgorithms.getLongSum(sumOfFifthNumbers);
//Stop the timer
timer.stop();
@@ -106,37 +107,27 @@ public class Problem30 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
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(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("largest number checked");
return TOP_NUM;
}
//This returns a copy of the vector holding all the numbers that are the sum of the fifth power of their digits
public ArrayList<Long> getListOfSumOfFifths(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("list of all numbers that are the sum of the 5th power of their digits");
return sumOfFifthNumbers;
}
//This returns the sum of all entries in sumOfFifthNumbers
public long getSumOfList(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum of all numbers that are the sum of the 5th power of their digits");
return sum;
}
}
/* Results:
The sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839
It took an average of 240.500 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem31.java
//Matthew Ellison
// Created: 06-19-20
//Modified: 08-27-20
//Modified: 07-03-21
//How many different ways can £2 be made using any number of coins?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,9 +23,6 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem31 extends Problem{
//Variables
//Static variables
@@ -50,6 +47,7 @@ public class Problem31 extends Problem{
//Start the timer
timer.start();
//Start with 200p and remove the necessary coins with each loop
for(int pound2 = DESIRED_VALUE; pound2 >= 0;pound2 -= 200){
for(int pound1 = pound2;pound1 >= 0;pound1 -= 100){
@@ -67,6 +65,7 @@ public class Problem31 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -83,21 +82,17 @@ public class Problem31 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("There are %d ways to make 2 pounds with the given denominations of coins", permutations);
}
//Returns the number of correct permutations of the coins
public int getPermutations(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("number of correct permutations of the coins");
return permutations;
}
}
/* Results:
There are 73682 ways to make 2 pounds with the given denominations of coins
It took an average of 19.175 microseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem32.java
//Matthew Ellison
// Created: 07-27-20
//Modified: 08-27-20
//Modified: 07-03-21
//Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -25,8 +25,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.StringAlgorithms;
public class Problem32 extends Problem{
@@ -97,6 +96,7 @@ public class Problem32 extends Problem{
//Start the timer
timer.start();
//Create the multiplicand and start working your way up
for(int multiplicand = 1;multiplicand <= TOP_MULTIPLICAND;++multiplicand){
//Run through all possible multipliers
@@ -120,6 +120,7 @@ public class Problem32 extends Problem{
sumOfPandigitals += prod.getProduct();
}
//Stop the timer
timer.stop();
@@ -138,7 +139,7 @@ public class Problem32 extends Problem{
for(int panNumber = 1;panNumber <= 9;++panNumber){
//Make sure there is exactly one of this number contained in the string
final int tempNum = panNumber; //This is here because forDigit() wanted a final variable
if(Algorithms.findNumOccurrence(numberString, Character.forDigit(tempNum, 10)) != 1){
if(StringAlgorithms.findNumOccurrence(numberString, Character.forDigit(tempNum, 10)) != 1){
return false;
}
}
@@ -155,21 +156,17 @@ public class Problem32 extends Problem{
//Gets
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("There are %d unique 1-9 pandigitals\nThe sum of the products of these pandigitals is %d", listOfProducts.size(), sumOfPandigitals);
}
//Returns the sum of the pandigitals
public long getSumOfPandigitals(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum of the pandigitals");
return sumOfPandigitals;
}
}
/* Results:
There are 7 unique 1-9 pandigitals
The sum of the products of these pandigitals is 45228

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem33.java
//Matthew Ellison
// Created: 02-05-21
//Modified: 02-07-21
//Modified: 07-03-21
/*
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s
We shall consider fractions like, 30/50 = 3/5, to be trivial examples
@@ -30,8 +30,8 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.ArrayAlgorithms;
import mattrixwv.NumberAlgorithms;
public class Problem33 extends Problem{
@@ -66,6 +66,7 @@ public class Problem33 extends Problem{
//Start the timer
timer.start();
//Search every possible numerator/denominator pair
for(int denominator = MIN_DENOMINATOR;denominator <= MAX_DENOMINATOR;++denominator){
for(int numerator = MIN_NUMERATOR;(numerator < denominator) && (numerator <= MAX_NUMERATOR);++numerator){
@@ -105,13 +106,14 @@ public class Problem33 extends Problem{
}
//Get the product of the numbers
int numProd = Algorithms.getProd(numerators);
int denomProd = Algorithms.getProd(denominators);
int numProd = ArrayAlgorithms.getProd(numerators);
int denomProd = ArrayAlgorithms.getProd(denominators);
//Get the gcd to reduce to lowest terms
int gcd = Algorithms.gcd(numProd, denomProd);
int gcd = NumberAlgorithms.gcd(numProd, denomProd);
//Save the denominator
prodDenominator = denomProd / gcd;
//Stop the timer
timer.stop();
@@ -129,33 +131,22 @@ public class Problem33 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The denominator of the product is %d", prodDenominator);
}
//Returns the list of numerators
public ArrayList<Integer> getNumerators(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("list of numerators");
return numerators;
}
//Returns the list of denominators
public ArrayList<Integer> getDenominators(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("list of denominators");
return denominators;
}
//Returns the answer to the question
public int getProdDenominator(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("denominator");
return prodDenominator;
}
}

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem34.java
//Matthew Ellison
// Created: 02-05-21
//Modified: 02-07-21
//Modified: 07-03-21
//Find the sum of all numbers which are equal to the sum of the factorial of their digits
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
@@ -22,10 +22,11 @@
*/
package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.NumberAlgorithms;
public class Problem34 extends Problem{
//Variables
@@ -56,9 +57,10 @@ public class Problem34 extends Problem{
//Start the timer
timer.start();
//Pre-compute the possible factorials from 0! to 9!
for(int cnt = 0;cnt <= 9;++cnt){
factorials.set(cnt, Algorithms.factorial(cnt));
factorials.set(cnt, NumberAlgorithms.factorial(cnt));
}
//Run through all possible numbers from 3-MAX_NUM and see if they equal the sum of their digit's factorials
for(int cnt = 3;cnt < MAX_NUM;++cnt){
@@ -76,6 +78,7 @@ public class Problem34 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -95,30 +98,22 @@ public class Problem34 extends Problem{
//Gets
//Returns a string witht he solution to the problem
public String getResult(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The sum of all numbers that are the sum of their digit's factorials is %d", sum);
}
//Returns the list of factorials from 0-9
public ArrayList<Integer> getFactorials(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("list of factorials");
return factorials;
}
//Returns the sum of all numbers equal to the sum of their digit's factorials
public int getSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum");
return sum;
}
}
/* Results:
The sum of all numbers that are the sum of their digit's factorials is 40730
It took an average of 78.889 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem35.java
//Matthew Ellison
// Created: 06-05-21
//Modified: 06-05-21
//Modified: 07-03-21
//How many circular primes are there below one million?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
@@ -22,10 +22,11 @@
*/
package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.NumberAlgorithms;
public class Problem35 extends Problem{
//Variables
@@ -62,8 +63,9 @@ public class Problem35 extends Problem{
//Start the timer
timer.start();
//Get all primes under 1,000,000
primes = Algorithms.getPrimes(MAX_NUM);
primes = NumberAlgorithms.getPrimes(MAX_NUM);
//Go through all primes, get all their rotations, and check if those numbers are also primes
for(int prime : primes){
boolean allRotationsPrime = true;
@@ -82,6 +84,7 @@ public class Problem35 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -97,38 +100,27 @@ public class Problem35 extends Problem{
//Gets
//Returns a string with the solution to the problem
public String getResult(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The number of all circular prime numbers under %d is %d", MAX_NUM, circularPrimes.size());
}
//Returns the ArrayList of primes < MAX_NUM
public ArrayList<Integer> getPrimes(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("list of primes");
return primes;
}
//Returns the ArrayList of circular primes < MAX_NUM
public ArrayList<Integer> getCircularPrimes(){
//If the problem hasn't been solved throw an excpetion
if(!solved){
throw new Unsolved();
}
solvedCheck("list of circular primes");
return circularPrimes;
}
//Returns the number of circular primes
public int getNumCircularPrimes(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("number of circular primes");
return circularPrimes.size();
}
}
/* Results:
The number of all circular prime numbers under 999999 is 55
It took an average of 5.255 seconds to run this problem through 100 iterations

View File

@@ -25,8 +25,9 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.ArrayAlgorithms;
import mattrixwv.NumberAlgorithms;
import mattrixwv.StringAlgorithms;
public class Problem36 extends Problem{
@@ -55,20 +56,22 @@ public class Problem36 extends Problem{
//Start the timer
timer.start();
//Start with 1, check if it is a palindrome in base 10 and 2, and continue to MAX_NUM
for(int num = 1;num < MAX_NUM;++num){
//Check if num is a palindrome
if(Algorithms.isPalindrome(Integer.toString(num))){
if(StringAlgorithms.isPalindrome(Integer.toString(num))){
//Convert num to base 2 and see if that is a palindrome
String binNum = Algorithms.toBin(num);
if(Algorithms.isPalindrome(binNum)){
String binNum = NumberAlgorithms.toBin(num);
if(StringAlgorithms.isPalindrome(binNum)){
//Add num to the list of palindromes
palindromes.add(num);
}
}
}
//Get the sum of all palindromes in the list
sum = Algorithms.getSum(palindromes);
sum = ArrayAlgorithms.getSum(palindromes);
//Stop the timer
timer.stop();
@@ -85,30 +88,22 @@ public class Problem36 extends Problem{
//Gets
//Returns a string with the solution to the problem
public String getResult(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The sum of all base 10 and base 2 palindromic numbers < %d is %d", MAX_NUM, sum);
}
//Return the List of palindromes < MAX_NUM
public ArrayList<Integer> getPalindromes(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("list of palindromes");
return palindromes;
}
//Return the sum of all elements in the List of palindromes
public int getSumOfPalindromes(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum of all palindromes");
return sum;
}
}
/* Results:
The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187
It took an average of 39.606 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem37.java
//Matthew Ellison
// Created: 07-01-21
//Modified: 07-01-21
//Modified: 07-03-21
//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).
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
@@ -24,9 +24,9 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ArrayAlgorithms;
import mattrixwv.NumberAlgorithms;
import mattrixwv.SieveOfEratosthenes;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem37 extends Problem{
//Variables
@@ -54,6 +54,7 @@ public class Problem37 extends Problem{
//Start the timer
timer.start();
//Create the sieve and get the first prime number
SieveOfEratosthenes sieve = new SieveOfEratosthenes();
long currentPrime = sieve.next();
@@ -89,7 +90,7 @@ public class Problem37 extends Problem{
String primeSubstring = primeString.substring(truncLoc);
//Convert the string to an int and see if the number is still prime
long newPrime = Long.valueOf(primeSubstring);
if(!Algorithms.isPrime(newPrime)){
if(!NumberAlgorithms.isPrime(newPrime)){
isTruncPrime = false;
break;
}
@@ -102,7 +103,7 @@ public class Problem37 extends Problem{
String primeSubstring = primeString.substring(0, primeString.length() - truncLoc);
//Convert the string to an int and see if the number is still prime
long newPrime = Long.valueOf(primeSubstring);
if(!Algorithms.isPrime(newPrime)){
if(!NumberAlgorithms.isPrime(newPrime)){
isTruncPrime = false;
break;
}
@@ -114,7 +115,8 @@ public class Problem37 extends Problem{
}
}
//Get the sum of all elements in the truncPrimes vector
sum = Algorithms.getLongSum(truncPrimes);
sum = ArrayAlgorithms.getLongSum(truncPrimes);
//Stop the timer
timer.stop();
@@ -132,28 +134,22 @@ public class Problem37 extends Problem{
//Gets
//Returns a string with the solution to the problem
public String getResult(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The sum of all left and right truncatable primes is %d", sum);
}
//Returns the list of primes that can be truncated
public ArrayList<Long> getTruncatablePrimes(){
if(!solved){
throw new Unsolved();
}
solvedCheck("list of truncatable primes");
return truncPrimes;
}
//Return the sum of all primes in truncPrimes
public long getSumOfPrimes(){
if(!solved){
throw new Unsolved();
}
solvedCheck("sum of truncatable primes");
return sum;
}
}
/* Results:
The sum of all left and right truncatable primes is 748317
It took an average of 103.829 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem4.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 08-27-20
//Modified: 07-03-21
//Find the largest palindrome made from the product of two 3-digit numbers
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -26,8 +26,6 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import java.util.Collections;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem4 extends Problem{
//Variables
@@ -54,6 +52,7 @@ public class Problem4 extends Problem{
//Start the timer
timer.start();
//Start at the first 3-digit number and check every one up to the last 3-digit number
for(int firstNum = START_NUM;firstNum <= END_NUM;++firstNum){
//You can start at the location of the first number because everything before that has already been tested. (100 * 101 == 101 * 100)
@@ -76,6 +75,7 @@ public class Problem4 extends Problem{
//Sort the palindromes so that the last one is the largest
Collections.sort(palindromes);
//Stop the timer
timer.stop();
@@ -92,29 +92,22 @@ public class Problem4 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The largest palindrome is %d", palindromes.get(palindromes.size() - 1));
}
//Returns the list of all palindromes
public ArrayList<Integer> getPalindromes(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("palindromes");
return palindromes;
}
//Returns the largest palindrome
public int getLargestPalindrome(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("largest palindrome");
return palindromes.get(palindromes.size() - 1);
}
}
/* Results:
The largest palindrome is 906609
It took an average of 16.926 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem5.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 08-27-20
//Modified: 07-03-21
//What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -23,9 +23,6 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem5 extends Problem{
//Variables
//Instance variables
@@ -49,6 +46,7 @@ public class Problem5 extends Problem{
//Start the timer
timer.start();
//Start at 20 because it must at least be divisible by 20. Increment by 2 because it must be an even number to be divisible by 2
boolean numFound = false; //A flag for finding the divisible number
int currentNum = 20; //The number that it is currently checking against
@@ -68,9 +66,10 @@ public class Problem5 extends Problem{
currentNum += 2;
}
}
//Save the current number as the smallest
smallestNum = currentNum;
//Stop the timer
timer.stop();
@@ -87,21 +86,17 @@ public class Problem5 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The smallest positive number evenly divisible by all numbers 1-20 is %d", smallestNum);
}
//Returns the requested number
public int getNumber(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("number");
return smallestNum;
}
}
/* Results:
The smallest positive number evenly divisible by all numbers 1-20 is 232792560
It took an average of 213.942 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem6.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 08-27-20
//Modified: 07-03-21
//Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -22,7 +22,6 @@
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem6 extends Problem{
//Variables
@@ -52,6 +51,7 @@ public class Problem6 extends Problem{
//Start the timer
timer.start();
//Run through all numbers and add them to the appropriate sums
for(int currentNum = START_NUM;currentNum <= END_NUM;++currentNum){
sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable
@@ -60,6 +60,7 @@ public class Problem6 extends Problem{
//Squaring the sum that needs it
squareOfSum *= squareOfSum;
//Stop the timer
timer.stop();
@@ -77,37 +78,27 @@ public class Problem6 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is %d", Math.abs(sumOfSquares - squareOfSum));
}
//Returns the sum of all the squares
public long getSumOfSquares(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("sum of the squares");
return sumOfSquares;
}
//Returns the square of all of the sums
public long getSquareOfSum(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("square of the sums");
return squareOfSum;
}
//Returns the requested difference
public long getDifference(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("difference between the two numbers");
return Math.abs(sumOfSquares - squareOfSum);
}
}
/* Results:
The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150
It took an average of 2.669 microseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem67.java
//Matthew Ellison
// Created: 03-26-19
//Modified: 07-19-20
//Modified: 07-03-21
//Find the maximum total from top to bottom
/*
59
@@ -108,7 +108,7 @@
//This is done using a breadth first search
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -129,8 +129,6 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import java.util.Arrays;
//import mattrixwv.ProjectEuler.Unsolved;
public class Problem67 extends Problem18{
//Setup the list of numbers to check
@@ -241,6 +239,7 @@ public class Problem67 extends Problem18{
}
}
/* Results:
The value of the longest path is 7273
It took an average of 187.077 milliseconds to run this problem through 100 iterations

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem7.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 08-27-20
//Modified: 07-03-21
//What is the 10001th prime number?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -25,8 +25,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import mattrixwv.NumberAlgorithms;
public class Problem7 extends Problem{
@@ -54,8 +53,10 @@ public class Problem7 extends Problem{
//Start the timer
timer.start();
//Setup the variables
primes = Algorithms.getNumPrimes(NUMBER_OF_PRIMES); //Holds the prime numbers
primes = NumberAlgorithms.getNumPrimes(NUMBER_OF_PRIMES); //Holds the prime numbers
//Stop the timer
timer.stop();
@@ -73,21 +74,17 @@ public class Problem7 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The %dth prime number is %d", NUMBER_OF_PRIMES, primes.get(primes.size() - 1));
}
//Returns the requested prime number
public long getPrime(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("prime");
return primes.get(primes.size() - 1);
}
}
/* Results:
The 10001th prime number is 104743
It took an average of 27.343 milliseconds to run this problem through 100 iterations

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem8.java
//Matthew Ellison
// Created: 03-28-19
//Modified: 08-27-20
//Modified: 07-03-21
//Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
/*
73167176531330624919225119674426574742355349194934
@@ -27,7 +27,7 @@
*/
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -44,7 +44,6 @@
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem8 extends Problem{
//Variables
@@ -74,6 +73,7 @@ public class Problem8 extends Problem{
//Start the timer
timer.start();
//Cycle through the string of numbers looking for the maximum product
for(int cnt = 12;cnt < NUMBER.length();++cnt){
long currentProduct = Long.parseLong(NUMBER.substring(cnt - 12, cnt - 11)) * Long.parseLong(NUMBER.substring(cnt - 11, cnt - 10)) * Long.parseLong(NUMBER.substring(cnt - 10, cnt - 9)) * Long.parseLong(NUMBER.substring(cnt - 9, cnt - 8)) * Long.parseLong(NUMBER.substring(cnt - 8, cnt - 7)) * Long.parseLong(NUMBER.substring(cnt - 7, cnt - 6)) * Long.parseLong(NUMBER.substring(cnt - 6, cnt - 5)) * Long.parseLong(NUMBER.substring(cnt - 5, cnt - 4)) * Long.parseLong(NUMBER.substring(cnt - 4, cnt - 3)) * Long.parseLong(NUMBER.substring(cnt - 3, cnt - 2)) * Long.parseLong(NUMBER.substring(cnt - 2, cnt - 1)) * Long.parseLong(NUMBER.substring(cnt - 1, cnt)) * Long.parseLong(NUMBER.substring(cnt, cnt + 1));
@@ -85,6 +85,7 @@ public class Problem8 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -102,29 +103,22 @@ public class Problem8 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The greatest product is %d\nThe numbers are %s", maxProduct, maxNums);
}
//Returns the string of numbers that produces the largest product
public String getLargestNums(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("numbers that make the largest product");
return maxNums;
}
//Returns the requested product
public long getLargestProduct(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("product of the numbers");
return maxProduct;
}
}
/* Results:
The greatest product is 23514624000
The numbers are 5576689664895

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem9.java
//Matthew Ellison
// Created: 03-02-19
//Modified: 08-27-20
//Modified: 07-03-21
//There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
Copyright (C) 2020 Matthew Ellison
Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
@@ -57,6 +57,7 @@ public class Problem9 extends Problem{
//Start the timer
timer.start();
//Loop through all possible a's
while((a < GOAL_SUM) && !found){
b = a + 1; //b must be larger than a
@@ -77,6 +78,7 @@ public class Problem9 extends Problem{
}
}
//Stop the timer
timer.stop();
@@ -101,45 +103,32 @@ public class Problem9 extends Problem{
//Returns the result of solving the problem
@Override
public String getResult(){
if(!solved){
throw new Unsolved();
}
solvedCheck("result");
return String.format("The Pythagorean triplet is %d + %d + %d\nThe numbers' product is %d", a, b, Math.round(c), a * b * Math.round(c));
}
//Returns the length of the first side
public int getSideA(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("first side");
return a;
}
//Returns the length of the second side
public int getSideB(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("second side");
return b;
}
//Returns the length of the hyp
public int getSideC(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("third side");
return (int)c;
}
//Returns the product of the 3 sides
public int getProduct(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
solvedCheck("product of all three sides");
return a * b * (int)c;
}
}
/* Results:
The Pythagorean triplet is 200 + 375 + 425
The numbers' product is 31875000