Changed to maven build and added a wrapper program

This commit is contained in:
2020-06-07 17:01:09 -04:00
parent 32a395a2b3
commit 00890ec55d
35 changed files with 722 additions and 357 deletions

7
.gitignore vendored
View File

@@ -1,5 +1,8 @@
#Visual Studio Code
.vscode
.classpath
.project
.settings
#Java class files
*.class
#Ignore all bin files
target/

93
pom.xml Normal file
View File

@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mattrixwv.ProjectEuler</groupId>
<artifactId>ProjectEulerJava</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ProjectEulerJava</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>mattrixwv</groupId>
<artifactId>myClasses</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>mattrixwv.ProjectEuler.Driver</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,191 @@
//ProjectEuler/ProjectEulerJava/src/man/java/mattrixwv/ProjectEuler/Driver.java
//Matthew Ellison
// Created: 06-07-20
//Modified: 06-07-20
//This is the driver function for the Java version of the ProjectEuler project
package mattrixwv.ProjectEuler;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import mattrixwv.ProjectEuler.Problems.*;
public class Driver{
private static final ArrayList<Integer> PROBLEM_NUMBERS = new ArrayList<Integer>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
67));
private static enum SELECTIONS{SOLVE, DESCRIPTION, LIST, EXIT, SIZE};
private static final Scanner input = new Scanner(System.in);
public static void main(String[] args){
SELECTIONS selection = SELECTIONS.SIZE; //Holds the menu selection of the user
do{
//Print the menu and prompt the user to select an action
printMenu();
selection = getMenuSelection();
switch(selection){
case SOLVE : solveMenu(); break;
case DESCRIPTION : descriptionMenu(); break;
case LIST : listProblems(); break;
case EXIT : break;
case SIZE :
default : printErrorMessage();
}
}while(!selection.equals(SELECTIONS.EXIT));
}
private static void printMenu(){
System.out.println("1. Solve a problem");
System.out.println("2. Print a problem description");
System.out.println("3. List valid problem numbers");
System.out.println("4. Exit");
System.out.println();
}
private static SELECTIONS getMenuSelection(){
Integer selection = input.nextInt();
while(!isValidMenu(selection)){
System.out.println("That is an invalid option!Press Enter to continue");
printMenu();
selection = input.nextInt();
}
return getSelection(selection);
}
private static Boolean isValidMenu(Integer selection){
//Ordinal + 1 because enum starts at 0
if((selection > 0) && (selection < (SELECTIONS.SIZE.ordinal() + 1))){
return true;
}
else{
return false;
}
}
private static SELECTIONS getSelection(Integer selection){
SELECTIONS sel = null;
switch(selection){
case 1 : sel = SELECTIONS.SOLVE; break;
case 2 : sel = SELECTIONS.DESCRIPTION; break;
case 3 : sel = SELECTIONS.LIST; break;
case 4 : sel = SELECTIONS.EXIT; break;
default : sel = SELECTIONS.SIZE;
}
return sel;
}
private static void printErrorMessage(){
System.out.println("That is an invalid selection!");
}
private static void solveMenu(){
Integer problemNumber = getProblemNumber();
//This selection solves all problems in order
if(problemNumber.equals(0)){
//Solve to every valid problem number, skipping over 0
for(Integer problemLocation = 1;problemLocation < PROBLEM_NUMBERS.size();++problemLocation){
//Solve the problem
System.out.print(PROBLEM_NUMBERS.get(problemLocation).toString() + ". ");
solveProblem(PROBLEM_NUMBERS.get(problemLocation));
}
}
//This is if a single problem number was chosen
else{
//Solve the problem
solveProblem(PROBLEM_NUMBERS.get(problemNumber));
}
}
private static void solveProblem(Integer problemNumber){
//Get the problem
Problem problem = getProblem(problemNumber);
//Print the problem description
String description = problem.getDescription();
System.out.println(description);
//Solve the problem
problem.solve();
//Print the results
System.out.println(problem.getResult() + "\nIt took " + problem.getTime() + " to solve this problem.\n\n");
}
private static Problem getProblem(Integer problemNumber){
Problem problem = null;
switch(problemNumber){
case 1 : problem = new Problem1(); break;
case 2 : problem = new Problem2(); break;
case 3 : problem = new Problem3(); break;
case 4 : problem = new Problem4(); break;
case 5 : problem = new Problem5(); break;
case 6 : problem = new Problem6(); break;
case 7 : problem = new Problem7(); break;
case 8 : problem = new Problem8(); break;
case 9 : problem = new Problem9(); break;
case 10 : problem = new Problem10(); break;
case 11 : problem = new Problem11(); break;
case 12 : problem = new Problem12(); break;
case 13 : problem = new Problem13(); break;
case 14 : problem = new Problem14(); break;
case 15 : problem = new Problem15(); break;
case 16 : problem = new Problem16(); break;
case 17 : problem = new Problem17(); break;
case 18 : problem = new Problem18(); break;
case 19 : problem = new Problem19(); break;
case 20 : problem = new Problem20(); break;
case 21 : problem = new Problem21(); break;
case 22 : problem = new Problem22(); break;
case 23 : problem = new Problem23(); break;
case 24 : problem = new Problem24(); break;
case 25 : problem = new Problem25(); break;
case 26 : problem = new Problem26(); break;
case 27 : problem = new Problem27(); break;
case 28 : problem = new Problem28(); break;
case 29 : problem = new Problem29(); break;
case 30 : problem = new Problem30(); break;
case 67 : problem = new Problem67(); break;
}
return problem;
}
private static void descriptionMenu(){
//Give some extra space to print the description
System.out.println("\n");
//Get the problem number
Integer problemNumber = getProblemNumber();
//If the problem number is 0 print out all the descriptions
if(problemNumber.equals(0)){
//Print description for every valid problem number, skipping over 0
for(Integer problemLocation = PROBLEM_NUMBERS.get(1);problemLocation < PROBLEM_NUMBERS.size();++problemLocation){
//Print the problem's description
System.out.print(PROBLEM_NUMBERS.get(problemLocation) + ". ");
printDescription(problemLocation);
System.out.println();
}
}
//Otherwise print out a single problem's description
else{
printDescription(problemNumber);
}
}
private static void printDescription(Integer problemNumber){
//Get the problem
Problem problem = getProblem(problemNumber);
//Print the problem's description
System.out.println(problem.getDescription());
}
private static Integer getProblemNumber(){
Integer problemNumber = 0;
System.out.print("Enter a problem number: ");
problemNumber = input.nextInt();
while(!PROBLEM_NUMBERS.contains(problemNumber)){
System.out.print("That is an invalid problem number!\nEnter a problem number: ");
problemNumber = input.nextInt();
}
return problemNumber;
}
private static void listProblems(){
System.out.print(PROBLEM_NUMBERS.get(1));
for(Integer problemNumber = 2;problemNumber < PROBLEM_NUMBERS.size();++problemNumber){
System.out.print(", " + PROBLEM_NUMBERS.get(problemNumber).toString());
}
System.out.println();
}
}

View File

@@ -0,0 +1,23 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
public abstract class Problem{
protected final Stopwatch timer = new Stopwatch(); //To time how long it takes to run the algorithm
protected String result = null; //Holds the results of the problem
private final String description;
Problem(String description){
this.description = description;
}
public String getDescription(){
return description;
}
public String getTime(){
return timer.getStr();
}
public String getResult(){
return result;
}
public abstract void solve();
}

View File

@@ -20,19 +20,21 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
public class Problem1 extends Problem{
//The largest number to be checked
private static final Integer TOP_NUM = 999;
public class Problem1{
private static final Integer TOP_NUM = 1000;
public static void main(String[] argv){
public Problem1(){
super("What is the sum of all the multiples of 3 or 5 that are less than 1000");
}
public void solve(){
Integer sum = 0; //Holds the sum of all the correct elements
Stopwatch timer = new Stopwatch();
//Check every number < 1000 to see if it is a multiple of 3 or 5. If it is add it to the running sum
timer.start();
for(int cnt = 1;cnt < TOP_NUM;++cnt){
for(int cnt = 1;cnt <= TOP_NUM;++cnt){
if((cnt % 3) == 0){
sum += cnt;
}
@@ -41,9 +43,8 @@ public class Problem1{
}
}
timer.stop();
//Print the results
System.out.println("The sum of all numbers < " + TOP_NUM.toString() + " is " + sum.toString());
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the results
result = "The sum of all numbers < " + TOP_NUM.toString() + " is " + sum.toString();
}
}

View File

@@ -20,19 +20,20 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
public class Problem10{
public class Problem10 extends Problem{
//The largest number to check for primes
private static final Long GOAL_NUMBER = 2000000L;
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //To time the algorithm's run time
public Problem10(){
super("Find the sum of all the primes below two million");
}
public void solve(){
//Start the timer
timer.start();
@@ -42,9 +43,8 @@ public class Problem10{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The sum of all the primes < %d is %d\n", GOAL_NUMBER, sum);
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the results
result = String.format("The sum of all the primes < %d is %d\n", GOAL_NUMBER, sum);
}
}

View File

@@ -42,15 +42,15 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
import mattrixwv.Algorithms;
public class Problem11{
public class Problem11 extends Problem{
//This is the grid of numbers that we will be working with
private static final Integer[][] grid = {{8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00},
@@ -72,10 +72,15 @@ public class Problem11{
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54},
{01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}};;
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //Allows the program to the timed
ArrayList<Integer> greatestProduct = new ArrayList<Integer>(); //Holds the largest product we have found so far
ArrayList<Integer> currentProduct = new ArrayList<Integer>(); //Holds the numbers we are currently working on
public Problem11(){
super("What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?");
}
public void solve(){
//Holds the largest product we have found so far
ArrayList<Integer> greatestProduct = new ArrayList<Integer>();
//Holds the numbers we are currently working on
ArrayList<Integer> currentProduct = new ArrayList<Integer>();
//Make sure all elements have at least 4 elements
for(int cnt = 0;cnt < 4;++cnt){
@@ -176,10 +181,8 @@ public class Problem11{
//Stop the timer
timer.stop();
//Print the resutls
System.out.printf("The greatest product of 4 numbers in a line is %d\n", Algorithms.getProd(greatestProduct));
System.out.println("The numbers are " + greatestProduct.toString());
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the resutls
result = String.format("The greatest product of 4 numbers in a line is %d\nThe numbers are %s", Algorithms.getProd(greatestProduct), greatestProduct.toString());
}
}

View File

@@ -20,19 +20,22 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
import mattrixwv.Algorithms;
public class Problem12{
private static final Long GOAL_DIVISORS = 500L; //The minimum number of divisors that you want
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //Allows timing of the algorithm
public class Problem12 extends Problem{
//The minimum number of divisors that you want
private static final Long GOAL_DIVISORS = 500L;
public Problem12(){
super("What is the value of the first triangle number to have over five hundred divisors?");
}
public void solve(){
//Setup the other variables
Boolean foundNumber = false; //TO flag whether the number has been found
Long sum = 1L;
@@ -59,9 +62,8 @@ public class Problem12{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The triangulare number %d is the sum of all numbers >= %d and has %d divisors\n", sum, counter - 1, divisors.size());
System.out.println("It took " + timer.getStr() + " to run this algorithms");
//Save the results
result = String.format("The triangulare number %d is the sum of all numbers >= %d and has %d divisors\n", sum, counter - 1, divisors.size());
}
}

View File

@@ -122,19 +122,20 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import mattrixwv.Algorithms;
public class Problem13{
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //Allows the program to be timed
public class Problem13 extends Problem{
public Problem13(){
super("Work out the first ten digits of the sum of the one-hundred 50-digit numbers");
}
public void solve(){
ArrayList<BigInteger> nums = new ArrayList<BigInteger>(); //Holds the numbers that are being summed
//Start the timer
@@ -248,10 +249,8 @@ public class Problem13{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The sum of all %d numbers is %d\n", nums.size(), sum);
System.out.printf("The first 10 digits of the sum of the numbers is %s\n", (sum.toString()).substring(0, 10));
System.out.println("It took " + timer.getStr() + " to run this algorithms");
//Save the results
result = String.format("The sum of all %d numbers is %d\nThe first 10 digits of the sum of the numbers is %s\n", nums.size(), sum, (sum.toString()).substring(0, 10));
}
}

View File

@@ -25,18 +25,21 @@ Which starting number, under one million, produces the longest chain?
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
public class Problem14 extends Problem{
//This is the top number that you will be checking against the series
private static final Long MAX_NUM = 1000000L;
public class Problem14{
private static final Long MAX_NUM = 1000000L; //This is the top number that you will be checking against the series
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //This allows the run time of the algorithm to be testd
Long maxLength = 0L; //This is the length of the longest chain
Long maxNum = 0L; //This is teh starting number of the longest chain
public Problem14(){
super("Which starting number, under one million, produces the longest chain using the itterative sequence?");
}
public void solve(){
//This is the length of the longest chain
Long maxLength = 0L;
//This is teh starting number of the longest chain
Long maxNum = 0L;
//Start the timer
timer.start();
@@ -54,9 +57,8 @@ public class Problem14{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The number %d produced a chain of %d steps\n", maxNum, maxLength);
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the results
result = String.format("The number %d produced a chain of %d steps\n", maxNum, maxLength);
}
//This function follows the rules of the sequence and returns its length
private static Long checkSeries(Long num){

View File

@@ -21,17 +21,19 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
public class Problem15{
public class Problem15 extends Problem{
//The width of the box to traverse
private static final Integer WIDTH = 20;
//The height of the box to traverse
private static final Integer LENGTH = 20;
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //Used to determine the algorithm's run time
public Problem15(){
super("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?");
}
public void solve(){
//Setup the rest of the variables
Long numOfRoutes = 0L; //The number of routes from 0, 0, to 20, 20
Integer currentX = 0; //The current x location on the grid
@@ -47,9 +49,8 @@ public class Problem15{
//Stop the timer
timer.stop();
//Print the results
System.out.println("The number of routes is " + numOfRoutes.toString());
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the results
result = String.format("The number of routes is " + numOfRoutes.toString());
}
//This function acts as a handler for moving the position on the grid and counting the distance
//It moves right first, then down

View File

@@ -20,18 +20,22 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import java.math.BigInteger;
public class Problem16{
private static final Integer NUM_TO_POWER = 2; //The number that is going to be raised to a power
private static final Integer POWER = 1000; //The power that the number is going to be raised to
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //To time the runtime of the algorithm
public class Problem16 extends Problem{
//The number that is going to be raised to a power
private static final Integer NUM_TO_POWER = 2;
//The power that the number is going to be raised to
private static final Integer POWER = 1000;
public Problem16(){
super("What is the sum of the digits of the number 2^1000?");
}
public void solve(){
//Setup the other variables
BigInteger num = new BigInteger("0"); //The number to be calculated
Integer sumOfElements = 0; //The sum of all digits in the number
@@ -53,10 +57,8 @@ public class Problem16{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("%d^%d = %s\n", NUM_TO_POWER, POWER, num.toString());
System.out.println("The sum of the elements is " + sumOfElements.toString());
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the results
result = String.format("%d^%d = %s\nThe sum of the elements is %d\n", NUM_TO_POWER, POWER, num.toString(), sumOfElements.toString());
}
}

View File

@@ -20,16 +20,18 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
public class Problem17{
public class Problem17 extends Problem{
//The first number to be
private static final Integer START_NUM = 1;
private static final Integer STOP_NUM = 1000;
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //To time the algorithm's run time
public Problem17(){
super("If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?");
}
public void solve(){
//Setup the variables needed
Integer sumOfLetters = 0;
@@ -47,11 +49,10 @@ public class Problem17{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The sum of all the letters in all the numbers %d-%d is %d\n", START_NUM, STOP_NUM, sumOfLetters);
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the results
result = String.format("The sum of all the letters in all the numbers %d-%d is %d\n", START_NUM, STOP_NUM, sumOfLetters);
}
private static String getStringFromNum(Integer number){
private String getStringFromNum(Integer number){
String numberString = new String();
//Starting with the largest digit create a string based on teh number passed in
//Check for negative
@@ -190,7 +191,7 @@ public class Problem17{
//Return the string
return numberString;
}
private static Integer getNumberChars(String number){
private Integer getNumberChars(String number){
Integer sumOfLetters = 0;
//Start at location 0 and count the number of letters, ignoring punctuation and whitespace
for(Integer location = 0;location < number.length();++location){

View File

@@ -38,15 +38,14 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Predicate;
public class Problem18{
public class Problem18 extends Problem{
//The number of rows in the array
private static final Integer NUM_ROWS = 15;
//Used to keep track of where the best location came from
@@ -54,6 +53,8 @@ public class Problem18{
public Integer xLocation;
public Integer yLocation;
public Integer total;
//Used originally for debugging so I could trace the path backwards
@SuppressWarnings("unused")
public Boolean fromRight;
location(Integer x, Integer y, Integer t, Boolean r){
xLocation = x;
@@ -62,8 +63,12 @@ public class Problem18{
fromRight = r;
}
}
public Problem18(){
super("Find the maximum total from top to bottom");
}
//This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest
private static void invert(ArrayList<ArrayList<Integer>> list){
private void invert(ArrayList<ArrayList<Integer>> list){
//Loop through every row in the list
for(int rowCnt = 0;rowCnt < list.size();++rowCnt){
//Loop through every column in the list
@@ -74,12 +79,11 @@ public class Problem18{
}
}
//This function helps by removing the element that is the same as the minimum location
private static void removeHelper(ArrayList<location> possiblePoints, final location minLoc){
private void removeHelper(ArrayList<location> possiblePoints, final location minLoc){
possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation)));
}
public static void main(String[] argv){
//Setup the timer
Stopwatch timer = new Stopwatch();
public void solve(){
//Start the timer
timer.start();
//Setup the list you are trying to find a path through
@@ -147,12 +151,11 @@ public class Problem18{
//Stop the timer
timer.stop();
//Print the results
//Save the results
//Get the correct total which will be the inversion of the current one
Integer actualTotal = ((100 * NUM_ROWS) - foundPoints.get(foundPoints.size() - 1).total);
System.out.println("The value of the longest path is " + actualTotal.toString());
System.out.println("It took " + timer.getStr() + " to run this algorithm");
result = String.format("The value of the longest path is " + actualTotal.toString());
}
}

View File

@@ -31,17 +31,21 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
public class Problem19{
public class Problem19 extends Problem{
private static enum DAYS{SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, NUMBER_OF_DAYS, ERROR};
private static final Integer START_YEAR = 1901; //The first year we are going to test
private static final Integer END_YEAR = 2000; //The last year we are going to test
public static void main(String[] argv){
Stopwatch timer = new Stopwatch();
//The first year we are going to test
private static final Integer START_YEAR = 1901;
//The last year we are going to test
private static final Integer END_YEAR = 2000;
public Problem19(){
super("How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?");
}
public void solve(){
//The total number of sundays
Long totalSundays = 0L;
//Start the timer
@@ -64,12 +68,11 @@ public class Problem19{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("There are %d Sundays that landed on the first of the months from %d to %d\n", totalSundays, START_YEAR, END_YEAR);
System.out.println("It took " + timer.getStr() + " to run this algorithms");
//Save the results
result = String.format("There are %d Sundays that landed on the first of the months from %d to %d\n", totalSundays, START_YEAR, END_YEAR);
}
//Return the day of the week that the date you pass into it is on
private static DAYS getDay(Integer month, Integer day, Integer year){
private DAYS getDay(Integer month, Integer day, Integer year){
//Make sure the numbers are within propper bounds
if((month < 1) || (month > 12) || (day < 1) || (day > 31) || (year < 1)){
return DAYS.ERROR;
@@ -147,7 +150,7 @@ public class Problem19{
}
}
//Returns true if the year passed to it is a leap year
private static Boolean isLeapYear(Integer year){
private Boolean isLeapYear(Integer year){
if(year < 1){
return false;
}

View File

@@ -20,18 +20,22 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
import mattrixwv.Algorithms;
public class Problem2{
public class Problem2 extends Problem{
//The largest number that will be checked as a fibonacci number
private static final int TOP_NUM = 3999999;
public static void main(String[] argv){
Stopwatch timer = new Stopwatch();
public Problem2(){
super("The sum of the even Fibonacci numbers less than 4,000,000");
}
public void solve(){
timer.start();
//Get a list of all fibonacci numbers < 4,000,000
ArrayList<Integer> fibNums = Algorithms.getAllFib(TOP_NUM);
@@ -46,9 +50,8 @@ public class Problem2{
timer.stop();
//Print the results
System.out.printf("The sum of all even fibonacci numbers <= %d is %d\n", TOP_NUM, sum);
System.out.printf("It took %s to run this algorithm\n", timer.getStr());
//Save the results
result = String.format("The sum of all even fibonacci numbers <= %d is %d\n", TOP_NUM, sum);
}
}

View File

@@ -20,17 +20,22 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import java.math.BigInteger;
public class Problem20{
public class Problem20 extends Problem{
//The largest number that will be multiplied
private static Integer TOP_NUM = 100;
public static void main(String[] argv){
Stopwatch timer = new Stopwatch();
BigInteger num = new BigInteger("1"); //The number that is being generated
public Problem20(){
super("What is the sum of the digits of 100!?");
}
public void solve(){
//The number that is being generated
BigInteger num = new BigInteger("1");
Long sum = 0L; //The sum of the digits of num
//Start the timer
@@ -52,10 +57,8 @@ public class Problem20{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("%d! = %s\n", TOP_NUM, num.toString());
System.out.printf("The sum of the digits is: %d\n", sum);
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the results
result = String.format("%d! = %s\nThe sum of the digits is: %d\n", TOP_NUM, num.toString(), sum);
}
}

View File

@@ -20,25 +20,28 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
import java.util.Collections;
public class Problem21{
public class Problem21 extends Problem{
//The number that is > the largest number to be checked
private static final Integer LIMIT = 10000;
public static void main(String[] argv){
//Setup the timer
Stopwatch timer = new Stopwatch();
public Problem21(){
super("Evaluate the sum of all the amicable numbers under 10000");
}
public void solve(){
//Setup the variables
ArrayList<Integer> divisorSum = new ArrayList<Integer>(); //Holds the sum of the divisors of the subscript number
divisorSum.ensureCapacity(LIMIT); //Reserving it now makes it faster later
//Make sure the arraylist is filled with 0's
for(int cnt = 0;divisorSum.size() < LIMIT;++cnt){
while(divisorSum.size() < LIMIT){
divisorSum.add(0);
}
@@ -51,7 +54,6 @@ public class Problem21{
if(divisors.size() > 1){
divisors.remove(divisors.get(divisors.size() - 1)); //Remove the last entry because it will be the number itself
}
//System.out.printf("getSum(divisors) = %d\n", Algorithms.getSum(divisors));
divisorSum.set(cnt, Algorithms.getSum(divisors)); //Add the sum of the divisors of the vector
}
//Check every sum of divisors in the list for a matching sum
@@ -79,13 +81,12 @@ public class Problem21{
//Sort the arraylist for neatness
Collections.sort(amicable);
//Print the results
System.out.printf("All amicable numbers less than %d are\n", LIMIT);
//Save the results
result = String.format("All amicable numbers less than %d are\n", LIMIT);
for(int cnt = 0;cnt < amicable.size();++cnt){
System.out.println(amicable.get(cnt).toString());
result += amicable.get(cnt).toString() + "\n";
}
System.out.printf("The sum of all of these amicable numbers is %d\n", Algorithms.getSum(amicable));
System.out.println("It took " + timer.getStr() + " to run this algorithm");
result = String.format("The sum of all of these amicable numbers is %d\n", Algorithms.getSum(amicable));
}
}

View File

@@ -20,16 +20,16 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class Problem22{
public class Problem22 extends Problem{
private static ArrayList<String> names = new ArrayList<String>(Arrays.asList("MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN",
"BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON","MICHELLE","LAURA","SARAH","KIMBERLY","DEBORAH","JESSICA","SHIRLEY",
"CYNTHIA","ANGELA","MELISSA","BRENDA","AMY","ANNA","REBECCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBRA","AMANDA","STEPHANIE",
@@ -398,10 +398,11 @@ public class Problem22{
"HAYDEN","HARLAND","ARNOLDO","RUEBEN","LEANDRO","KRAIG","JERRELL","JEROMY","HOBERT","CEDRICK","ARLIE","WINFORD","WALLY","LUIGI",
"KENETH","JACINTO","GRAIG","FRANKLYN","EDMUNDO","SID","PORTER","LEIF","JERAMY","BUCK","WILLIAN","VINCENZO","SHON","LYNWOOD","JERE",
"HAI","ELDEN","DORSEY","DARELL","BRODERICK","ALONSO"));
public static void main(String[] argv){
//To allow the algorithm to be timed
Stopwatch timer = new Stopwatch();
public Problem22(){
super("What is the total of all the name scores in this file?");
}
public void solve(){
//Setup the variables
ArrayList<Long> sums = new ArrayList<Long>(); //Holds the score based on the sum of the characters in the name
ArrayList<Long> prod = new ArrayList<Long>(); //Holds the score based on the sum of characters and the location in alphabetical order
@@ -428,9 +429,8 @@ public class Problem22{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The answer to the question is %d\n", Algorithms.getLongSum(prod));
System.out.printf("It took %s to run this algorithm\n", timer.getStr());
//Save the results
result = String.format("The answer to the question is %d\n", Algorithms.getLongSum(prod));
}
}

View File

@@ -20,17 +20,22 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
public class Problem23{
public class Problem23 extends Problem{
//The largest number to be checked
public static final Integer MAX_NUM = 28123;
public static void main(String[] args){
Stopwatch timer = new Stopwatch();
public Problem23(){
super("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers");
}
public void solve(){
//Setup the variables
ArrayList<Integer> divisorSums = new ArrayList<Integer>(); //Holds the sum of all the divisors of a number
divisorSums.ensureCapacity(MAX_NUM); //It is faster to reserve the appropriate amount of ram now
@@ -71,9 +76,8 @@ public class Problem23{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The answer is %d\n", sum);
System.out.printf("It took %s to run this algorithm\n", timer.getStr());
//Save the results
result = String.format("The answer is %d\n", sum);
}
//A function that returns true if num can be created by adding two elements from abund and false if it cannot
private static Boolean isSum(final ArrayList<Integer> abund, Integer num){

View File

@@ -20,18 +20,23 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
public class Problem24{
public class Problem24 extends Problem{
//The number of the permutation I need
private static final Integer NEEDED_PERM = 1000000; //The number of the permutation that you need
public static void main(String[] args){
public Problem24(){
super("What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?");
}
public void solve(){
//Setup the variables
Stopwatch timer = new Stopwatch();
String nums = "0123456789"; //The string that you are trying to find the permutations of
//Start the timer
@@ -43,9 +48,8 @@ public class Problem24{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The 1 millionth permutation is %s\n", permutations.get(NEEDED_PERM - 1));
System.out.printf("It took %s to run this algorithm\n", timer.getStr());
//Save the results
result = String.format("The 1 millionth permutation is %s\n", permutations.get(NEEDED_PERM - 1));
}
}

View File

@@ -20,19 +20,23 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.math.BigInteger;
public class Problem25{
private static final Integer NUM_DIGITS = 1000; //The number of digits to calculate up to
public static void main(String[] args){
public class Problem25 extends Problem{
//The number of digits to calculate up to
private static final Integer NUM_DIGITS = 1000;
public Problem25(){
super("What is the index of the first term in the Fibonacci sequence to contain 1000 digits?");
}
public void solve(){
//Setup the variables
Stopwatch timer = new Stopwatch();
BigInteger number = BigInteger.ZERO; //Holds the current Fibonacci number
BigInteger index = BigInteger.valueOf(2L); //The index of the Fibonacci number just calculated
@@ -48,10 +52,8 @@ public class Problem25{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The first Fibonacci number with %d digits is %s\n", NUM_DIGITS, number.toString());
System.out.printf("Its index is %d\n", index);
System.out.printf("It took %s to run this algorithm\n", timer.getStr());
//Save the results
result = String.format("The first Fibonacci number with %d digits is %s\nIts index is %d\n", NUM_DIGITS, number.toString(), index);
}
}

View File

@@ -20,19 +20,26 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
public class Problem26{
public class Problem26 extends Problem{
//The number of digits needed in the number
private static final Integer TOP_NUM = 999;
public static void main(String[] args){
//Setup the variables
Stopwatch timer = new Stopwatch();
Integer longestCycle = 0; //The length of the longest cycle
Integer longestNumber = 0; //The starting denominator of the longest cycle
public Problem26(){
super("What is the index of the first term in the Fibonacci sequence to contain 1000 digits?");
}
public void solve(){
//The length of the longest cycle
Integer longestCycle = 0;
//The starting denominator of the longest cycle
Integer longestNumber = 0;
//Start the timer
timer.start();
@@ -78,10 +85,8 @@ public class Problem26{
//Stop the timer
timer.stop();
//Print the restuls
System.out.printf("The longest cycle is %d digits long\n", longestCycle);
System.out.printf("It started with the number %d\n", longestNumber);
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the restuls
result = String.format("The longest cycle is %d digits long\nIt started with the number %d\n", longestCycle, longestNumber);
}
}

View File

@@ -20,23 +20,28 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
public class Problem27{
private static Integer topA = 0; //The A for the most n's generated
private static Integer topB = 0; //The B for the most n's generated
private static Integer topN = 0; //The most n's generated
private static ArrayList<Integer> primes = Algorithms.getPrimes(12000); //A list of all primes that could possibly be generated with this formula
public static void main(String[] args){
//Setup the variables
Stopwatch timer = new Stopwatch();
public class Problem27 extends Problem{
//The A for the most n's generated
private static Integer topA = 0;
//The B for the most n's generated
private static Integer topB = 0;
//The most n's generated
private static Integer topN = 0;
//A list of all primes that could possibly be generated with this formula
private static ArrayList<Integer> primes = Algorithms.getPrimes(12000);
public Problem27(){
super("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");
}
public void solve(){
//Start the timer
timer.start();
@@ -65,11 +70,8 @@ public class Problem27{
//Stop the timer
timer.stop();
//Print the restuls
System.out.printf("The greatest number of primes found is %d", topN);
System.out.printf("\nIt was found with A = %d, B = %d", topA, topB);
System.out.printf("\nThe product of A and B is %d\n", topA * topB);
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the restuls
result = 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\n", topN, topA, topB, topA * topB);
}
}

View File

@@ -20,18 +20,23 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import java.util.ArrayList;
public class Problem28{
private static ArrayList<ArrayList<Integer>> grid; //Holds the grid that we will be filling and searching
private static Integer sumOfDiagonals = 0; //Holds the sum of the diagonals of the grid
public class Problem28 extends Problem{
//Holds the grid that we will be filling and searching
private static ArrayList<ArrayList<Integer>> grid;
//Holds the sum of the diagonals of the grid
private static Integer sumOfDiagonals = 0;
public Problem28(){
super("What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral");
}
//Sets up the grid
private static void setupGrid(){
private void setupGrid(){
grid = new ArrayList<ArrayList<Integer>>();
//Fill the grid with 0's
for(Integer cnt = 0;cnt < 1001;++cnt){
@@ -83,9 +88,8 @@ public class Problem28{
}
}
}
//Finds the sum of teh diagonals in the grid
private static void findSum(){
private void findSum(){
//Start at teh top corners and work your way down moving toward the opposite side
Integer leftSide = 0;
Integer rightSide = grid.size() - 1;
@@ -104,11 +108,7 @@ public class Problem28{
--rightSide;
}
}
public static void main(String args[]){
//Setup the variables
Stopwatch timer = new Stopwatch();
public void solve(){
//Start the timer
timer.start();
@@ -121,8 +121,7 @@ public class Problem28{
timer.stop();
//Print the restuls
System.out.printf("The sum of the diagonals in the given grid is %d\n", sumOfDiagonals);
System.out.println("It took " + timer.getStr() + " to run this algorithm");
result = String.format("The sum of the diagonals in the given grid is %d\n", sumOfDiagonals);
}
}

View File

@@ -20,23 +20,30 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import java.util.ArrayList;
import java.math.BigInteger;
public class Problem29{
private static final Integer BOTTOM_A = 2; //The lowest possible value for a
private static final Integer TOP_A = 100; //The highest possible value for a
private static final Integer BOTTOM_B = 2; //The lowest possible value for b
private static final Integer TOP_B = 100; //The highest possible value for b
private static ArrayList<BigInteger> unique; //Holds all unique values generated
public static void main(String[] args){
public class Problem29 extends Problem{
//The lowest possible value for a
private static final Integer BOTTOM_A = 2;
//The highest possible value for a
private static final Integer TOP_A = 100;
//The lowest possible value for b
private static final Integer BOTTOM_B = 2;
//The highest possible value for b
private static final Integer TOP_B = 100;
//Holds all unique values generated
private static ArrayList<BigInteger> unique;
public Problem29(){
super("How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?");
}
public void solve(){
//Setup the variables
Stopwatch timer = new Stopwatch();
unique = new ArrayList<BigInteger>();
//Start the time
@@ -59,8 +66,7 @@ public class Problem29{
timer.stop();
//Print the results
System.out.printf("The number of unique values generated by a^b for %d <= a <= %d and %d <= b <= %d is %d\n", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B, unique.size());
System.out.println("It took " + timer.getStr() + " to run this algorithm");
result = String.format("The number of unique values generated by a^b for %d <= a <= %d and %d <= b <= %d is %d\n", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B, unique.size());
}
}

View File

@@ -20,20 +20,23 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.math.BigInteger;
import java.util.ArrayList;
import mattrixwv.Algorithms;
public class Problem3{
public class Problem3 extends Problem{
//The number that needs factored
private static final BigInteger NUMBER = BigInteger.valueOf(600851475143L);
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //A timer to check execution time of the algorithm
public Problem3(){
super("The largest prime factor of 600851475143");
}
public void solve(){
//Start the timer
timer.start();
@@ -44,9 +47,8 @@ public class Problem3{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The largest factor of the number %d is %d\n", NUMBER, factors.get(factors.size() - 1));
System.out.printf("It took %s to run this algorithm\n", timer.getStr());
//Save the results
result = String.format("The largest factor of the number %d is %d\n", NUMBER, factors.get(factors.size() - 1));
}
}

View File

@@ -20,20 +20,27 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import java.util.ArrayList;
public class Problem30{
private static final Long TOP_NUM = 1000000L; //This is the largest number that will be checked
private static final Long BOTTOM_NUM = 2L; //Starts with 2 because 0 and 1 don't count
private static final Long POWER_RAISED = 5L; //This is the power that the digits are raised to
private static ArrayList<Long> sumOfFifthNumbers; //This is an ArrayList of the numbers that are the sum of the fifth power of their digits
public class Problem30 extends Problem{
//This is the largest number that will be checked
private static final Long TOP_NUM = 1000000L;
//Starts with 2 because 0 and 1 don't count
private static final Long BOTTOM_NUM = 2L;
//This is the power that the digits are raised to
private static final Long POWER_RAISED = 5L;
//This is an ArrayList of the numbers that are the sum of the fifth power of their digits
private static ArrayList<Long> sumOfFifthNumbers;
public Problem30(){
super("Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.");
}
//Returns an ArrayList with the individual digits of the number passed to it
private static ArrayList<Long> getDigits(Long num){
private ArrayList<Long> getDigits(Long num){
ArrayList<Long> listOfDigits = new ArrayList<Long>(); //This ArrayList holds the individual digits of num
//The easiest way to get the individual digits of a number is by converting it to a string
String digits = num.toString();
@@ -44,7 +51,7 @@ public class Problem30{
//Return the list of Digits
return listOfDigits;
}
public static Long getSumOfList(){
public Long getSumOfList(){
Long sum = 0L; //Start the sum at 0 so you can add to it
//Add every number in the ArrayList to the sum
for(Long num : sumOfFifthNumbers){
@@ -53,8 +60,8 @@ public class Problem30{
//Return the sum
return sum;
}
public static void main(String[] args){
Stopwatch timer = new Stopwatch();
public void solve(){
//Sum of the power of the fifth powers of their digits
sumOfFifthNumbers = new ArrayList<Long>();
//Start the timer
@@ -80,8 +87,7 @@ public class Problem30{
timer.stop();
//Print the results
System.out.printf("The sum of all the numbers that can be written as the sum of the fifth powers of their digits is %d\n", getSumOfList());
System.out.println("It took " + timer.getStr() + " to run this algorithm");
result = String.format("The sum of all the numbers that can be written as the sum of the fifth powers of their digits is %d\n", getSumOfList());
}
}

View File

@@ -20,21 +20,23 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
import java.util.Collections;
public class Problem4{
private static final Integer START_NUM = 100; //The first number to be multiplied
private static final Integer END_NUM = 999; //THe last number to be multiplied
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //To time the execution time of this algorithm
public class Problem4 extends Problem{
//The first number to be multiplied
private static final Integer START_NUM = 100;
//The last number to be multiplied
private static final Integer END_NUM = 999;
public Problem4(){
super("Find the largest palindrome made from the product of two 3-digit numbers");
}
public void solve(){
//Start the timer
timer.start();
@@ -55,10 +57,6 @@ public class Problem4{
if(productString.equals(reverseString)){
palindromes.add(product);
}
else{
//System.out.println("ProductString = " + productString);
//System.out.println("reverseString = " + reverseString);
}
//If it's not a palindrome ignore it and move to the next number
}
}
@@ -69,9 +67,8 @@ public class Problem4{
//Sort the palindromes so that the last one is the largest
Collections.sort(palindromes);
//Print the results
System.out.printf("The largest palindrome is %d\n", palindromes.get(palindromes.size() - 1));
System.out.printf("It took " + timer.getStr() + " to run this algorithm\n");
//Save the results
result = String.format("The largest palindrome is %d\n", palindromes.get(palindromes.size() - 1));
}
}

View File

@@ -20,17 +20,14 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import java.util.ArrayList;
public class Problem5{
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //So you can time the code
public class Problem5 extends Problem{
public Problem5(){
super("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?");
}
public void solve(){
//Start the timer
timer.start();
@@ -57,9 +54,8 @@ public class Problem5{
//Stop the timer
timer.stop();
//Print the results
System.out.println("The smallest positive number evenly divisibly by all number 1-20 is " + currentNum.toString());
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the results
result = String.format("The smallest positive number evenly divisibly by all number 1-20 is " + currentNum.toString());
}
}

View File

@@ -20,17 +20,19 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
public class Problem6{
private static final Integer START_NUM = 1; //The first number that needs to be counted
private static final Integer END_NUM = 100; //The last number that needs to be counted
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //SO you can time the algorithm's run time
public class Problem6 extends Problem{
//The first number that needs to be counted
private static final Integer START_NUM = 1;
//The last number that needs to be counted
private static final Integer END_NUM = 100;
public Problem6(){
super("Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.");
}
public void solve(){
//Start the timer
timer.start();
@@ -49,9 +51,8 @@ public class Problem6{
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is %d\n", Math.abs(sumOfSquares - squareOfSum));
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the results
result = String.format("The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is %d\n", Math.abs(sumOfSquares - squareOfSum));
}
}

View File

@@ -123,23 +123,26 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Predicate;
public class Problem67{
public class Problem67 extends Problem{
//The number of rows in the array
private static final Integer NUM_ROWS = 100;
//Setup the list you are trying to find a path through
private ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
//Used to keep track of where the best location came from
private static class location{
private class location{
public Integer xLocation;
public Integer yLocation;
public Integer total;
//Originally used in debugging
@SuppressWarnings("unused")
public Boolean fromRight;
location(Integer x, Integer y, Integer t, Boolean r){
xLocation = x;
@@ -148,28 +151,10 @@ public class Problem67{
fromRight = r;
}
}
//This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest
private static void invert(ArrayList<ArrayList<Integer>> list){
//Loop through every row in the list
for(int rowCnt = 0;rowCnt < list.size();++rowCnt){
//Loop through every column in the list
for(int colCnt = 0;colCnt < list.get(rowCnt).size();++colCnt){
//The current element gets the value of 100 - value
list.get(rowCnt).set(colCnt, 100 - list.get(rowCnt).get(colCnt));
}
}
}
//This function helps by removing the element that is the same as the minimum location
private static void removeHelper(ArrayList<location> possiblePoints, final location minLoc){
possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation)));
}
public static void main(String[] args){
//Setup the timer
Stopwatch timer = new Stopwatch();
timer.start();
//Setup the list you are trying to find a path through
ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
public Problem67(){
super("Find the maximum total from top to bottom");
list.ensureCapacity(NUM_ROWS);
list.add(new ArrayList<Integer>(Arrays.asList(59)));
list.add(new ArrayList<Integer>(Arrays.asList(73, 41)));
@@ -271,13 +256,33 @@ public class Problem67{
list.add(new ArrayList<Integer>(Arrays.asList(64, 66, 84, 24, 18, 16, 27, 48, 20, 14, 47, 69, 30, 86, 48, 40, 23, 16, 61, 21, 51, 50, 26, 47, 35, 33, 91, 28, 78, 64, 43, 68, 04, 79, 51, 8, 19, 60, 52, 95, 06, 68, 46, 86, 35, 97, 27, 58, 04, 65, 30, 58, 99, 12, 12, 75, 91, 39, 50, 31, 42, 64, 70, 04, 46, 07, 98, 73, 98, 93, 37, 89, 77, 91, 64, 71, 64, 65, 66, 21, 78, 62, 81, 74, 42, 20, 83, 70, 73, 95, 78, 45, 92, 27, 34, 53, 71, 15)));
list.add(new ArrayList<Integer>(Arrays.asList(30, 11, 85, 31, 34, 71, 13, 48, 05, 14, 44, 03, 19, 67, 23, 73, 19, 57, 06, 90, 94, 72, 57, 69, 81, 62, 59, 68, 88, 57, 55, 69, 49, 13, 07, 87, 97, 80, 89, 05, 71, 05, 05, 26, 38, 40, 16, 62, 45, 99, 18, 38, 98, 24, 21, 26, 62, 74, 69, 04, 85, 57, 77, 35, 58, 67, 91, 79, 79, 57, 86, 28, 66, 34, 72, 51, 76, 78, 36, 95, 63, 90, 8, 78, 47, 63, 45, 31, 22, 70, 52, 48, 79, 94, 15, 77, 61, 67, 68)));
list.add(new ArrayList<Integer>(Arrays.asList(23, 33, 44, 81, 80, 92, 93, 75, 94, 88, 23, 61, 39, 76, 22, 03, 28, 94, 32, 06, 49, 65, 41, 34, 18, 23, 8, 47, 62, 60, 03, 63, 33, 13, 80, 52, 31, 54, 73, 43, 70, 26, 16, 69, 57, 87, 83, 31, 03, 93, 70, 81, 47, 95, 77, 44, 29, 68, 39, 51, 56, 59, 63, 07, 25, 70, 07, 77, 43, 53, 64, 03, 94, 42, 95, 39, 18, 01, 66, 21, 16, 97, 20, 50, 90, 16, 70, 10, 95, 69, 29, 06, 25, 61, 41, 26, 15, 59, 63, 35)));
}
//This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest
private void invert(ArrayList<ArrayList<Integer>> list){
//Loop through every row in the list
for(int rowCnt = 0;rowCnt < list.size();++rowCnt){
//Loop through every column in the list
for(int colCnt = 0;colCnt < list.get(rowCnt).size();++colCnt){
//The current element gets the value of 100 - value
list.get(rowCnt).set(colCnt, 100 - list.get(rowCnt).get(colCnt));
}
}
}
//This function helps by removing the element that is the same as the minimum location
private void removeHelper(ArrayList<location> possiblePoints, final location minLoc){
possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation)));
}
public void solve(){
//Invert the list
invert(list);
ArrayList<location> foundPoints = new ArrayList<location>(); //For the points that I have already found the shortest distance to
foundPoints.add(new location(0, 0, list.get(0).get(0), false)); //Add the first row as a found point because you have to go through the tip
ArrayList<location> possiblePoints = new ArrayList<location>(); //For the locations you are checking this round
//For the points that I have already found the shortest distance to
ArrayList<location> foundPoints = new ArrayList<location>();
//Add the first row as a found point because you have to go through the tip
foundPoints.add(new location(0, 0, list.get(0).get(0), false));
//For the locations you are checking this round
ArrayList<location> possiblePoints = new ArrayList<location>();
//Add the second row as possible points
possiblePoints.add(new location(0, 1, (list.get(0).get(0) + list.get(1).get(0)), true));
possiblePoints.add(new location(1, 1, (list.get(0).get(0) + list.get(1).get(1)), false));
@@ -323,8 +328,7 @@ public class Problem67{
//Get the correct total which will be the inversion of the current one
Integer actualTotal = ((100 * NUM_ROWS) - foundPoints.get(foundPoints.size() - 1).total);
System.out.println("The value of the longest path is " + actualTotal.toString());
System.out.println("It took " + timer.getStr() + " to run this algorithm");
result = "The value of the longest path is " + actualTotal.toString();
}
}

View File

@@ -20,20 +20,23 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.Algorithms;
import java.util.ArrayList;
import java.math.BigInteger;
import mattrixwv.Algorithms;
public class Problem7{
public class Problem7 extends Problem{
//The number of primes we are trying to get
private static final BigInteger NUMBER_OF_PRIMES = BigInteger.valueOf(10001);
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //So the algorithm can be timed
public Problem7(){
super("What is the 10001th prime number?");
}
public void solve(){
//Start the timer
timer.start();
@@ -43,9 +46,8 @@ public class Problem7{
//Stop the timer
timer.stop();
//Print the results
System.out.println("The " + NUMBER_OF_PRIMES.toString() + "th prime number is " + primes.get(primes.size() - 1));
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the results
result = String.format("The " + NUMBER_OF_PRIMES.toString() + "th prime number is " + primes.get(primes.size() - 1));
}
}

View File

@@ -42,16 +42,17 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
public class Problem8{
public class Problem8 extends Problem{
//The 1000 digit number to check
private static final String NUMBER = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
public static void main(String[] argv){
Stopwatch timer = new Stopwatch();
public Problem8(){
super("Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?");
}
public void solve(){
//Setup the variables
String maxNums = new String(); //Holds the string of the largest product
Long maxProduct = 0L; //Holds the largest product of 13 numbers
@@ -73,10 +74,8 @@ public class Problem8{
//Stop the timer
timer.stop();
//Print the results
System.out.println("The greatest product is " + maxProduct.toString());
System.out.println("The numbers are " + maxNums);
System.out.println("It took " + timer.getStr() + " to run this algorithm");
//Save the results
result = String.format("The greatest product is " + maxProduct.toString() + "\nThe numbers are " + maxNums);
}
}

View File

@@ -20,15 +20,15 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
public class Problem9{
public static void main(String[] argv){
public class Problem9 extends Problem{
public Problem9(){
super("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.");
}
public void solve(){
//Setup the variables
Stopwatch timer = new Stopwatch(); //To time the execution time of this algorithm
Integer a = 1; //Holds the length of the first side
Integer b = 0; //Holds the length of the second side
Double c = 0D; //Holds the length of the hyp
@@ -60,14 +60,16 @@ public class Problem9{
//Stop the timer
timer.stop();
//Print the results
//Save the results
if(found){
System.out.printf("The Pythagorean triplet is %d + %d + %d\n", a, b, c.intValue());
System.out.printf("The numbers' product is %d\n", a * b * c.intValue());
System.out.println("It took " + timer.getStr() + " to run this algorithm");
result = String.format("The Pythagorean triplet is %d + %d + %d\nThe numbers' product is %d\n", a, b, c.intValue(), a * b * c.intValue());
}
else{
System.out.println("The number was not found!");
result = "The number was not found!";
}
}
}
}
/* Results:
*/