mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Updated to depend less on string create for an answer
This commit is contained in:
@@ -29,7 +29,6 @@ public abstract class Problem{
|
|||||||
//Variables
|
//Variables
|
||||||
//Instance variables
|
//Instance variables
|
||||||
protected final Stopwatch timer = new Stopwatch(); //To time how long it takes to run the algorithm
|
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; //Holds the description of the problem
|
private final String description; //Holds the description of the problem
|
||||||
protected boolean solved; //Shows whether the problem has already been solved
|
protected boolean solved; //Shows whether the problem has already been solved
|
||||||
|
|
||||||
@@ -44,12 +43,7 @@ public abstract class Problem{
|
|||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
//Returns the result of solving the problem
|
//Returns the result of solving the problem
|
||||||
public String getResult(){
|
public abstract String getResult();
|
||||||
if(!solved){
|
|
||||||
throw new Unsolved();
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
//Returns the time taken to run the problem as a string
|
//Returns the time taken to run the problem as a string
|
||||||
public String getTime() throws InvalidResult{
|
public String getTime() throws InvalidResult{
|
||||||
if(!solved){
|
if(!solved){
|
||||||
@@ -70,6 +64,5 @@ public abstract class Problem{
|
|||||||
public void reset(){
|
public void reset(){
|
||||||
timer.reset();
|
timer.reset();
|
||||||
solved = false;
|
solved = false;
|
||||||
result = null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem1.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem1.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 08-23-20
|
//Modified: 08-27-20
|
||||||
//What is the sum of all the multiples of 3 or 5 that are less than 1000
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -36,11 +36,12 @@ public class Problem1 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem1(){
|
public Problem1(){
|
||||||
super("What is the sum of all the multiples of 3 or 5 that are less than 1000");
|
super(String.format("What is the sum of all the multiples of 3 or 5 that are less than %d?", (TOP_NUM + 1)));
|
||||||
fullSum = 0;
|
fullSum = 0;
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -65,16 +66,22 @@ public class Problem1 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The sum of all numbers < %d is %d", (TOP_NUM + 1), fullSum);
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
fullSum = 0;
|
fullSum = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The sum of all numbers < %d is %d", (TOP_NUM + 1), fullSum);
|
||||||
|
}
|
||||||
//Returns the requested sum
|
//Returns the requested sum
|
||||||
public int getSum(){
|
public int getSum(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem10.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem10.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-03-19
|
// Created: 03-03-19
|
||||||
//Modified: 08-26-20
|
//Modified: 08-27-20
|
||||||
//Find the sum of all the primes below two million
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -37,11 +37,12 @@ public class Problem10 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem10(){
|
public Problem10(){
|
||||||
super("Find the sum of all the primes below two million");
|
super(String.format("Find the sum of all the primes below %d.", GOAL_NUMBER + 1));
|
||||||
sum = 0;
|
sum = 0;
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -52,23 +53,29 @@ public class Problem10 extends Problem{
|
|||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
//Get the sum of all prime numbers < GOAL_NUMBER
|
//Get the sum of all prime numbers < GOAL_NUMBER
|
||||||
long sum = Algorithms.getLongSum(Algorithms.getPrimes(GOAL_NUMBER)); //Subtract 1 from the number so that it is < the number
|
sum = Algorithms.getLongSum(Algorithms.getPrimes(GOAL_NUMBER)); //Subtract 1 from the number so that it is < the number
|
||||||
|
|
||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The sum of all the primes < %d is %d\n", GOAL_NUMBER + 1, sum);
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
sum = 0;
|
sum = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The sum of all the primes < %d is %d", GOAL_NUMBER + 1, sum);
|
||||||
|
}
|
||||||
//Returns the sum that was requested
|
//Returns the sum that was requested
|
||||||
public long getSum(){
|
public long getSum(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem11.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem11.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-03-19
|
// Created: 03-03-19
|
||||||
//Modified: 08-26-20
|
//Modified: 08-27-20
|
||||||
//What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
|
//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
|
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
|
||||||
@@ -86,6 +86,7 @@ public class Problem11 extends Problem{
|
|||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -196,16 +197,22 @@ public class Problem11 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//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());
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
greatestProduct.clear();
|
greatestProduct.clear();
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//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());
|
||||||
|
}
|
||||||
//Returns the numbers that were being searched
|
//Returns the numbers that were being searched
|
||||||
ArrayList<Integer> getNumbers(){
|
ArrayList<Integer> getNumbers(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-04-19
|
// Created: 03-04-19
|
||||||
//Modified: 08-26-20
|
//Modified: 08-27-20
|
||||||
//What is the value of the first triangle number to have over five hundred divisors?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -41,7 +41,7 @@ public class Problem12 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem12(){
|
public Problem12(){
|
||||||
super("What is the value of the first triangle number to have over five hundred divisors?");
|
super(String.format("What is the value of the first triangle number to have over %d divisors?", GOAL_DIVISORS));
|
||||||
sum = 1;
|
sum = 1;
|
||||||
counter = 2;
|
counter = 2;
|
||||||
divisors = new ArrayList<Long>();
|
divisors = new ArrayList<Long>();
|
||||||
@@ -79,9 +79,6 @@ public class Problem12 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The triangular number %d is the sum of all numbers >= %d and has %d divisors\n", sum, counter - 1, divisors.size());
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
public void reset(){
|
public void reset(){
|
||||||
@@ -91,6 +88,14 @@ public class Problem12 extends Problem{
|
|||||||
divisors.clear();
|
divisors.clear();
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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
|
//Returns the triangular number
|
||||||
public long getTriangularNumber(){
|
public long getTriangularNumber(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem13.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem13.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-04-19
|
// Created: 03-04-19
|
||||||
//Modified: 08-26-20
|
//Modified: 08-27-20
|
||||||
//Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
|
//Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
|
||||||
/*
|
/*
|
||||||
37107287533902102798797998220837590246510135740250
|
37107287533902102798797998220837590246510135740250
|
||||||
@@ -256,6 +256,7 @@ public class Problem13 extends Problem{
|
|||||||
nums.add(new BigInteger("53503534226472524250874054075591789781264330331690"));
|
nums.add(new BigInteger("53503534226472524250874054075591789781264330331690"));
|
||||||
}
|
}
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -276,11 +277,9 @@ public class Problem13 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//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));
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
sum = BigInteger.ZERO;
|
sum = BigInteger.ZERO;
|
||||||
@@ -288,6 +287,14 @@ public class Problem13 extends Problem{
|
|||||||
reserveVectors();
|
reserveVectors();
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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 50-digit numbers
|
//Returns the list 50-digit numbers
|
||||||
public ArrayList<BigInteger> getNumbers(){
|
public ArrayList<BigInteger> getNumbers(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem14.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem14.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-04-19
|
// Created: 03-04-19
|
||||||
//Modified: 08-26-20
|
//Modified: 08-27-20
|
||||||
/*
|
/*
|
||||||
The following iterative sequence is defined for the set of positive integers:
|
The following iterative sequence is defined for the set of positive integers:
|
||||||
n → n/2 (n is even)
|
n → n/2 (n is even)
|
||||||
@@ -42,12 +42,13 @@ public class Problem14 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem14(){
|
public Problem14(){
|
||||||
super("Which starting number, under one million, produces the longest chain using the itterative sequence?");
|
super(String.format("Which starting number, under %d, produces the longest chain using the itterative sequence?", MAX_NUM));
|
||||||
maxLength = 0;
|
maxLength = 0;
|
||||||
maxNum = 0;
|
maxNum = 0;
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -72,9 +73,6 @@ public class Problem14 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//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
|
//This function follows the rules of the sequence and returns its length
|
||||||
private long checkSeries(long num){
|
private long checkSeries(long num){
|
||||||
@@ -95,12 +93,21 @@ public class Problem14 extends Problem{
|
|||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
maxLength = 0;
|
maxLength = 0;
|
||||||
maxNum = 0;
|
maxNum = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The number %d produced a chain of %d steps\n", maxNum, maxLength);
|
||||||
|
}
|
||||||
//Returns the length of the requested chain
|
//Returns the length of the requested chain
|
||||||
public long getLength(){
|
public long getLength(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem15.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem15.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-04-19
|
// Created: 03-04-19
|
||||||
//Modified: 08-26-20
|
//Modified: 08-27-20
|
||||||
//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?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -37,11 +37,12 @@ public class Problem15 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem15(){
|
public Problem15(){
|
||||||
super("How many routes from the top left corner to the bottom right corner are there through a 20x20 grid if you can only move right and down?");
|
super(String.format("How many routes from the top left corner to the bottom right corner are there through a %dx%d grid if you can only move right and down?", WIDTH, LENGTH));
|
||||||
numOfRoutes = 0;
|
numOfRoutes = 0;
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problems
|
//Solve the problems
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -60,9 +61,6 @@ public class Problem15 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The number of routes is " + numOfRoutes);
|
|
||||||
}
|
}
|
||||||
//This function acts as a handler for moving the position on the grid and counting the distance
|
//This function acts as a handler for moving the position on the grid and counting the distance
|
||||||
//It moves right first, then down
|
//It moves right first, then down
|
||||||
@@ -84,11 +82,20 @@ public class Problem15 extends Problem{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
numOfRoutes = 0;
|
numOfRoutes = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The number of routes is %d", numOfRoutes);
|
||||||
|
}
|
||||||
//Returns the number of routes found
|
//Returns the number of routes found
|
||||||
public long getNumberOfRoutes(){
|
public long getNumberOfRoutes(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem16.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem16.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-04-19
|
// Created: 03-04-19
|
||||||
//Modified: 08-26-20
|
//Modified: 08-27-20
|
||||||
//What is the sum of the digits of the number 2^1000?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -40,10 +40,11 @@ public class Problem16 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem16(){
|
public Problem16(){
|
||||||
super("What is the sum of the digits of the number 2^1000?");
|
super(String.format("What is the sum of the digits of the number %d^%d?", NUM_TO_POWER, POWER));
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -69,17 +70,23 @@ public class Problem16 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("%d^%d = %s\nThe sum of the elements is %d\n", NUM_TO_POWER, POWER, num.toString(), sumOfElements);
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
num = BigInteger.ZERO;
|
num = BigInteger.ZERO;
|
||||||
sumOfElements = 0;
|
sumOfElements = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("%d^%d = %s\nThe sum of the elements is %d\n", NUM_TO_POWER, POWER, num.toString(), sumOfElements);
|
||||||
|
}
|
||||||
//Returns the number that was calculated
|
//Returns the number that was calculated
|
||||||
public BigInteger getNumber(){
|
public BigInteger getNumber(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem17.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem17.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-04-19
|
// Created: 03-04-19
|
||||||
//Modified: 08-26-20
|
//Modified: 08-27-20
|
||||||
//If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -37,11 +37,12 @@ public class Problem17 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem17(){
|
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?");
|
super(String.format("If all the numbers from %d to %d inclusive were written out in words, how many letters would be used?", START_NUM, STOP_NUM));
|
||||||
letterCount = 0;
|
letterCount = 0;
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -64,11 +65,9 @@ public class Problem17 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//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, letterCount);
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
letterCount = 0;
|
letterCount = 0;
|
||||||
@@ -179,6 +178,14 @@ public class Problem17 extends Problem{
|
|||||||
return sumOfLetters;
|
return sumOfLetters;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The sum of all the letters in all the numbers %d-%d is %d\n", START_NUM, STOP_NUM, letterCount);
|
||||||
|
}
|
||||||
//Returns the number of letters asked for
|
//Returns the number of letters asked for
|
||||||
public long getLetterCount(){
|
public long getLetterCount(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-11-19
|
// Created: 03-11-19
|
||||||
//Modified: 08-26-20
|
//Modified: 08-27-20
|
||||||
//Find the maximum total from top to bottom
|
//Find the maximum total from top to bottom
|
||||||
/*
|
/*
|
||||||
75
|
75
|
||||||
@@ -170,20 +170,18 @@ public class Problem18 extends Problem{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Stop the timer
|
|
||||||
timer.stop();
|
|
||||||
|
|
||||||
//Invert the list again so it is correct
|
//Invert the list again so it is correct
|
||||||
invert(list);
|
invert(list);
|
||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
//Get the correct total which will be the inversion of the current one
|
//Get the correct total which will be the inversion of the current one
|
||||||
int actualTotal = ((100 * list.size()) - foundPoints.get(foundPoints.size() - 1).total);
|
actualTotal = ((100 * list.size()) - foundPoints.get(foundPoints.size() - 1).total);
|
||||||
|
|
||||||
|
//Stop the timer
|
||||||
|
timer.stop();
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
result = String.format("The value of the longest path is " + actualTotal);
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
public void reset(){
|
public void reset(){
|
||||||
@@ -193,6 +191,14 @@ public class Problem18 extends Problem{
|
|||||||
actualTotal = 0;
|
actualTotal = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The value of the longest path is %d", actualTotal);
|
||||||
|
}
|
||||||
//Returns the pyramid that was traversed as a string
|
//Returns the pyramid that was traversed as a string
|
||||||
public String getPyramid(){
|
public String getPyramid(){
|
||||||
StringBuilder results = new StringBuilder();
|
StringBuilder results = new StringBuilder();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem19.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem19.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-13-19
|
// Created: 03-13-19
|
||||||
//Modified: 08-26-20
|
//Modified: 08-27-20
|
||||||
//How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
|
//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.
|
You are given the following information, but you may prefer to do some research for yourself.
|
||||||
@@ -53,6 +53,7 @@ public class Problem19 extends Problem{
|
|||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -79,9 +80,6 @@ public class Problem19 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//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);
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
@@ -109,21 +107,26 @@ public class Problem19 extends Problem{
|
|||||||
}
|
}
|
||||||
//Add the correct number of days for every month
|
//Add the correct number of days for every month
|
||||||
while(currentMonth < month){
|
while(currentMonth < month){
|
||||||
//February
|
switch(currentMonth){
|
||||||
if(currentMonth == 2){
|
case 1:
|
||||||
if(isLeapYear(currentYear)){
|
case 3:
|
||||||
numDays += 29;
|
case 5:
|
||||||
}
|
case 7:
|
||||||
else{
|
case 8:
|
||||||
numDays += 28;
|
case 10:
|
||||||
}
|
case 12: numDays += 31; break;
|
||||||
}
|
case 4:
|
||||||
//31 day months
|
case 6:
|
||||||
else if((currentMonth == 1) || (currentMonth == 3) || (currentMonth == 5) || (currentMonth == 7) || (currentMonth == 8) || (currentMonth == 10) || (currentMonth == 12)){
|
case 9:
|
||||||
numDays += 31;
|
case 11: numDays += 30; break;
|
||||||
}
|
case 2:
|
||||||
else{
|
if(isLeapYear(currentYear)){
|
||||||
numDays += 30;
|
numDays += 29;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
numDays += 28;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
++currentMonth;
|
++currentMonth;
|
||||||
}
|
}
|
||||||
@@ -183,11 +186,20 @@ public class Problem19 extends Problem{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
totalSundays = 0;
|
totalSundays = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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
|
//Returns the total sundays that were asked for
|
||||||
public long getTotalSundays(){
|
public long getTotalSundays(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem2.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem2.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 08-23-20
|
//Modified: 08-27-20
|
||||||
//The sum of the even Fibonacci numbers less than 4,000,000
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -39,11 +39,12 @@ public class Problem2 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem2(){
|
public Problem2(){
|
||||||
super("What is the sum of the even Fibonacci numbers less than 4,000,000?");
|
super(String.format("What is the sum of the even Fibonacci numbers less than %d?", (TOP_NUM + 1)));
|
||||||
fullSum = 0;
|
fullSum = 0;
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -53,7 +54,7 @@ public class Problem2 extends Problem{
|
|||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
//Get a list of all fibonacci numbers < 4,000,000
|
//Get a list of all fibonacci numbers <= TOP_NUM
|
||||||
ArrayList<Integer> fibNums = Algorithms.getAllFib(TOP_NUM);
|
ArrayList<Integer> fibNums = Algorithms.getAllFib(TOP_NUM);
|
||||||
//Step through every element in the list checking if it is even
|
//Step through every element in the list checking if it is even
|
||||||
for(int num : fibNums){
|
for(int num : fibNums){
|
||||||
@@ -68,16 +69,22 @@ public class Problem2 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The sum of all even fibonacci numbers <= %d is %d", TOP_NUM, fullSum);
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
fullSum = 0;
|
fullSum = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The sum of all even fibonacci numbers <= %d is %d", TOP_NUM, fullSum);
|
||||||
|
}
|
||||||
//Returns the requested sum
|
//Returns the requested sum
|
||||||
public int getSum(){
|
public int getSum(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem20.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem20.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-14-19
|
// Created: 03-14-19
|
||||||
//Modified: 07-18-20
|
//Modified: 08-27-20
|
||||||
//What is the sum of the digits of 100!?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -34,17 +34,18 @@ public class Problem20 extends Problem{
|
|||||||
private static final int TOP_NUM = 100; //The largest number that will be multiplied
|
private static final int TOP_NUM = 100; //The largest number that will be multiplied
|
||||||
//Instance variables
|
//Instance variables
|
||||||
private BigInteger num; //Holds the number 100!
|
private BigInteger num; //Holds the number 100!
|
||||||
private long sum; //The sum of the digts of num
|
private long sum; //The sum of the digits of num
|
||||||
|
|
||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem20(){
|
public Problem20(){
|
||||||
super("What is the sum of the digits of 100!?");
|
super(String.format("What is the sum of the digits of %d!?", TOP_NUM));
|
||||||
num = BigInteger.ONE;
|
num = BigInteger.ONE;
|
||||||
sum = 0;
|
sum = 0;
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -63,26 +64,32 @@ public class Problem20 extends Problem{
|
|||||||
String numString = num.toString();
|
String numString = num.toString();
|
||||||
//Run through every character in the string, convert it back to an integer and add it to the running sum
|
//Run through every character in the string, convert it back to an integer and add it to the running sum
|
||||||
for(int cnt = 0;cnt < numString.length();++cnt){
|
for(int cnt = 0;cnt < numString.length();++cnt){
|
||||||
Character temp = numString.charAt(cnt);
|
Character digit = numString.charAt(cnt);
|
||||||
sum += Integer.valueOf(temp.toString());
|
sum += Integer.valueOf(digit.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("%d! = %s\nThe sum of the digits is: %d\n", TOP_NUM, num.toString(), sum);
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
num = BigInteger.ONE;
|
num = BigInteger.ONE;
|
||||||
sum = 0;
|
sum = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("%d! = %s\nThe sum of the digits is: %d", TOP_NUM, num.toString(), sum);
|
||||||
|
}
|
||||||
//Returns the number 100!
|
//Returns the number 100!
|
||||||
public BigInteger getNumber(){
|
public BigInteger getNumber(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem21.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem21.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-18-19
|
// Created: 03-18-19
|
||||||
//Modified: 07-19-20
|
//Modified: 08-27-20
|
||||||
//Evaluate the sum of all the amicable numbers under 10000
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -41,7 +41,7 @@ public class Problem21 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem21(){
|
public Problem21(){
|
||||||
super("Evaluate the sum of all the amicable numbers under 10000");
|
super(String.format("Evaluate the sum of all the amicable numbers under %d", LIMIT));
|
||||||
divisorSum = new ArrayList<Integer>();
|
divisorSum = new ArrayList<Integer>();
|
||||||
amicable = new ArrayList<Integer>();
|
amicable = new ArrayList<Integer>();
|
||||||
reserveArray();
|
reserveArray();
|
||||||
@@ -56,6 +56,7 @@ public class Problem21 extends Problem{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -97,17 +98,11 @@ public class Problem21 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("All amicable numbers less than %d are\n", LIMIT);
|
|
||||||
for(int cnt = 0;cnt < amicable.size();++cnt){
|
|
||||||
result += amicable.get(cnt).toString() + "\n";
|
|
||||||
}
|
|
||||||
result += String.format("The sum of all of these amicable numbers is %d\n", Algorithms.getSum(amicable));
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
divisorSum.clear();
|
divisorSum.clear();
|
||||||
@@ -115,6 +110,20 @@ public class Problem21 extends Problem{
|
|||||||
reserveArray();
|
reserveArray();
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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)));
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
//Returns a vector with all of the amicable numbers calculated
|
//Returns a vector with all of the amicable numbers calculated
|
||||||
public ArrayList<Integer> getAmicable(){
|
public ArrayList<Integer> getAmicable(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-20-19
|
// Created: 03-20-19
|
||||||
//Modified: 07-19-20
|
//Modified: 08-27-20
|
||||||
//What is the total of all the name scores in this file?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -406,6 +406,7 @@ public class Problem22 extends Problem{
|
|||||||
//Instance variables
|
//Instance variables
|
||||||
private ArrayList<Long> sums; //Holds the score based on the sum of the characters in the name
|
private ArrayList<Long> sums; //Holds the score based on the sum of the characters in the name
|
||||||
private ArrayList<Long> prod; //Holds the score based on the sum of the characters and the location in alphabetical order
|
private ArrayList<Long> prod; //Holds the score based on the sum of the characters and the location in alphabetical order
|
||||||
|
private long sum; //Holds the sum of the scores
|
||||||
|
|
||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
@@ -416,6 +417,7 @@ public class Problem22 extends Problem{
|
|||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -442,24 +444,31 @@ public class Problem22 extends Problem{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Get the sum of all the numbers
|
//Get the sum of all the numbers
|
||||||
long sum = Algorithms.getLongSum(prod);
|
sum = Algorithms.getLongSum(prod);
|
||||||
|
|
||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The answer to the question is %d\n", sum);
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
sums.clear();
|
sums.clear();
|
||||||
prod.clear();
|
prod.clear();
|
||||||
|
sum = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The answer to the question is %d", sum);
|
||||||
|
}
|
||||||
//Returns the vector of the names being scored
|
//Returns the vector of the names being scored
|
||||||
public ArrayList<String> getNames(){
|
public ArrayList<String> getNames(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-22-19
|
// Created: 03-22-19
|
||||||
//Modified: 07-19-20
|
//Modified: 08-27-20
|
||||||
//Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -55,6 +55,7 @@ public class Problem23 extends Problem{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -92,9 +93,6 @@ public class Problem23 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The answer is %d\n", sum);
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
@@ -118,6 +116,7 @@ public class Problem23 extends Problem{
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
divisorSums.clear();
|
divisorSums.clear();
|
||||||
@@ -125,6 +124,14 @@ public class Problem23 extends Problem{
|
|||||||
sum = 0;
|
sum = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The answer is %d", sum);
|
||||||
|
}
|
||||||
//Returns the sum of the numbers asked for
|
//Returns the sum of the numbers asked for
|
||||||
public long getSum(){
|
public long getSum(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem24.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem24.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-24-19
|
// Created: 03-24-19
|
||||||
//Modified: 07-19-20
|
//Modified: 08-27-20
|
||||||
//What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -40,11 +40,12 @@ public class Problem24 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem24(){
|
public Problem24(){
|
||||||
super("What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?");
|
super(String.format("What is the millionth lexicographic permutation of the digits %s?", nums));
|
||||||
permutations = new ArrayList<String>();
|
permutations = new ArrayList<String>();
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problems
|
//Solve the problems
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -60,18 +61,24 @@ public class Problem24 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The 1 millionth permutation is %s\n", permutations.get(NEEDED_PERM - 1));
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
permutations.clear();
|
permutations.clear();
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The 1 millionth permutation is %s", permutations.get(NEEDED_PERM - 1));
|
||||||
|
}
|
||||||
//Returns an ArrayList with all of the permutations
|
//Returns an ArrayList with all of the permutations
|
||||||
public ArrayList<String> getPermutationsList(){
|
public ArrayList<String> getPermutationsList(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem25.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem25.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-25-19
|
// Created: 03-25-19
|
||||||
//Modified: 07-19-20
|
//Modified: 08-27-20
|
||||||
//What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -40,12 +40,13 @@ public class Problem25 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem25(){
|
public Problem25(){
|
||||||
super("What is the index of the first term in the Fibonacci sequence to contain 1000 digits?");
|
super(String.format("What is the index of the first term in the Fibonacci sequence to contain %d digits?", NUM_DIGITS));
|
||||||
number = BigInteger.ZERO;
|
number = BigInteger.ZERO;
|
||||||
index = BigInteger.TWO;
|
index = BigInteger.TWO;
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -64,19 +65,25 @@ public class Problem25 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//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);
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
number = BigInteger.ZERO;
|
number = BigInteger.ZERO;
|
||||||
index = BigInteger.TWO;
|
index = BigInteger.TWO;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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
|
//Returns the Fibonacci number asked for
|
||||||
public BigInteger getNumber(){
|
public BigInteger getNumber(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem26.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem26.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 07-28-19
|
// Created: 07-28-19
|
||||||
//Modified: 07-19-20
|
//Modified: 08-27-20
|
||||||
//Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -40,7 +40,7 @@ public class Problem26 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem26(){
|
public Problem26(){
|
||||||
super("Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.");
|
super(String.format("Find the value of d <= %d for which 1/d contains the longest recurring cycle in its decimal fraction part.", TOP_NUM));
|
||||||
longestCycle = 0;
|
longestCycle = 0;
|
||||||
longestNumber = 0;
|
longestNumber = 0;
|
||||||
}
|
}
|
||||||
@@ -96,9 +96,6 @@ public class Problem26 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The longest cycle is %d digits long\nIt started with the number %d\n", longestCycle, longestNumber);
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
@@ -109,6 +106,14 @@ public class Problem26 extends Problem{
|
|||||||
longestNumber = 1;
|
longestNumber = 1;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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
|
//Returns the length of the longest cycle
|
||||||
public int getLongestCycle(){
|
public int getLongestCycle(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem27.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem27.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 09-15-19
|
// Created: 09-15-19
|
||||||
//Modified: 07-19-20
|
//Modified: 08-27-20
|
||||||
//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.
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -31,6 +31,9 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
public class Problem27 extends Problem{
|
public class Problem27 extends Problem{
|
||||||
//Variables
|
//Variables
|
||||||
|
//Static varibles
|
||||||
|
private static int LARGEST_POSSIBLE_A = 999;
|
||||||
|
private static int LARGEST_POSSIBLE_B = 1000;
|
||||||
//Instance variables
|
//Instance variables
|
||||||
private int topA; //The A for the most n's generated
|
private int topA; //The A for the most n's generated
|
||||||
private int topB; //The B for the most n's generated
|
private int topB; //The B for the most n's generated
|
||||||
@@ -40,7 +43,7 @@ public class Problem27 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem27(){
|
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");
|
super(String.format("Find the product of the coefficients, |a| <= %d and |b| <= %d, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0", LARGEST_POSSIBLE_A, LARGEST_POSSIBLE_B));
|
||||||
topA = 0;
|
topA = 0;
|
||||||
topB = 0;
|
topB = 0;
|
||||||
topN = 0;
|
topN = 0;
|
||||||
@@ -48,6 +51,7 @@ public class Problem27 extends Problem{
|
|||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -61,9 +65,9 @@ public class Problem27 extends Problem{
|
|||||||
primes = Algorithms.getPrimes(12000);
|
primes = Algorithms.getPrimes(12000);
|
||||||
|
|
||||||
//Start with the lowest possible A and check all possibilities after that
|
//Start with the lowest possible A and check all possibilities after that
|
||||||
for(int a = -999;a <= 999;++a){
|
for(int a = -LARGEST_POSSIBLE_A;a <= LARGEST_POSSIBLE_A;++a){
|
||||||
//Start with the lowest possible B and check all possibilities after that
|
//Start with the lowest possible B and check all possibilities after that
|
||||||
for(int b = -1000;b <=1000;++b){
|
for(int b = -LARGEST_POSSIBLE_B;b <= LARGEST_POSSIBLE_B;++b){
|
||||||
//Start with n=0 and check the formula to see how many primes you can get with concecutive n's
|
//Start with n=0 and check the formula to see how many primes you can get with concecutive n's
|
||||||
int n = 0;
|
int n = 0;
|
||||||
int quadratic = (n * n) + (a * n) + b;
|
int quadratic = (n * n) + (a * n) + b;
|
||||||
@@ -85,13 +89,11 @@ public class Problem27 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//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);
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
topA = 0;
|
topA = 0;
|
||||||
@@ -100,6 +102,14 @@ public class Problem27 extends Problem{
|
|||||||
primes.clear();
|
primes.clear();
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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
|
//Returns the top A that was generated
|
||||||
public int getTopA(){
|
public int getTopA(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem28.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem28.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 09-22-19
|
// Created: 09-22-19
|
||||||
//Modified: 07-19-20
|
//Modified: 08-27-20
|
||||||
//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
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -116,6 +116,7 @@ public class Problem28 extends Problem{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -133,18 +134,24 @@ public class Problem28 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Print the restuls
|
|
||||||
result = String.format("The sum of the diagonals in the given grid is %d\n", sumOfDiagonals);
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
sumOfDiagonals = 0;
|
sumOfDiagonals = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The sum of the diagonals in the given grid is %d", sumOfDiagonals);
|
||||||
|
}
|
||||||
//Returns the grid
|
//Returns the grid
|
||||||
public ArrayList<ArrayList<Integer>> getGrid(){
|
public ArrayList<ArrayList<Integer>> getGrid(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem29.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem29.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 10-09-19
|
// Created: 10-09-19
|
||||||
//Modified: 07-19-20
|
//Modified: 08-27-20
|
||||||
//How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -23,12 +23,11 @@
|
|||||||
package mattrixwv.ProjectEuler.Problems;
|
package mattrixwv.ProjectEuler.Problems;
|
||||||
|
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import mattrixwv.ProjectEuler.Unsolved;
|
import mattrixwv.ProjectEuler.Unsolved;
|
||||||
|
|
||||||
import java.math.BigInteger;
|
|
||||||
|
|
||||||
|
|
||||||
public class Problem29 extends Problem{
|
public class Problem29 extends Problem{
|
||||||
//Variables
|
//Variables
|
||||||
@@ -43,11 +42,12 @@ public class Problem29 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem29(){
|
public Problem29(){
|
||||||
super("How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?");
|
super(String.format("How many distinct terms are in the sequence generated by a^b for %d <= a <= %d and %d <= b <= %d?", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B));
|
||||||
unique = new ArrayList<BigInteger>();
|
unique = new ArrayList<BigInteger>();
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -73,18 +73,24 @@ public class Problem29 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Print the results
|
|
||||||
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());
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
unique.clear();
|
unique.clear();
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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
|
//Returns the lowest possible value for a
|
||||||
public int getBottomA(){
|
public int getBottomA(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem3.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem3.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 08-23-20
|
//Modified: 08-27-20
|
||||||
//The largest prime factor of 600851475143
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -40,11 +40,12 @@ public class Problem3 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem3(){
|
public Problem3(){
|
||||||
super("What is the largest prime factor of 600851475143?");
|
super(String.format("What is the largest prime factor of %d?", GOAL_NUMBER));
|
||||||
factors = new ArrayList<Long>();
|
factors = new ArrayList<Long>();
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve() throws InvalidResult{
|
public void solve() throws InvalidResult{
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -63,16 +64,22 @@ public class Problem3 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The largest factor of the number %d is %d", GOAL_NUMBER, factors.get(factors.size() - 1));
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
factors.clear();
|
factors.clear();
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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
|
//Returns the list of factors of the number
|
||||||
public ArrayList<Long> getFactors(){
|
public ArrayList<Long> getFactors(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem30.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem30.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 10-27-19
|
// Created: 10-27-19
|
||||||
//Modified: 07-19-20
|
//Modified: 08-27-20
|
||||||
//Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -58,6 +58,7 @@ public class Problem30 extends Problem{
|
|||||||
return listOfDigits;
|
return listOfDigits;
|
||||||
}
|
}
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -86,18 +87,24 @@ public class Problem30 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Print the results
|
|
||||||
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", Algorithms.getLongSum(sumOfFifthNumbers));
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
sumOfFifthNumbers.clear();
|
sumOfFifthNumbers.clear();
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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", Algorithms.getLongSum(sumOfFifthNumbers));
|
||||||
|
}
|
||||||
//This returns the top number to be checked
|
//This returns the top number to be checked
|
||||||
public long getTopNum(){
|
public long getTopNum(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem31.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem31.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 06-19-20
|
// Created: 06-19-20
|
||||||
//Modified: 07-19-20
|
//Modified: 08-27-20
|
||||||
//How many different ways can £2 be made using any number of coins?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -22,8 +22,10 @@
|
|||||||
*/
|
*/
|
||||||
package mattrixwv.ProjectEuler.Problems;
|
package mattrixwv.ProjectEuler.Problems;
|
||||||
|
|
||||||
|
|
||||||
import mattrixwv.ProjectEuler.Unsolved;
|
import mattrixwv.ProjectEuler.Unsolved;
|
||||||
|
|
||||||
|
|
||||||
public class Problem31 extends Problem{
|
public class Problem31 extends Problem{
|
||||||
//Variables
|
//Variables
|
||||||
//Static variables
|
//Static variables
|
||||||
@@ -68,18 +70,24 @@ public class Problem31 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the result
|
|
||||||
result = String.format("There are %d ways to make 2 pounds with the given denominations of coins", permutations);
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
permutations = 0;
|
permutations = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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
|
//Returns the number of correct permutations of the coins
|
||||||
public int getPermutations(){
|
public int getPermutations(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem32.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem32.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 07-27-20
|
// Created: 07-27-20
|
||||||
//Modified: 07-27-20
|
//Modified: 08-27-20
|
||||||
//Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital.
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -87,6 +87,7 @@ public class Problem32 extends Problem{
|
|||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -122,9 +123,6 @@ public class Problem32 extends Problem{
|
|||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("There are %d unique 1-9 pandigitals\nThe sum of the products of these pandigitals is %d", listOfProducts.size(), sumOfPandigitals);
|
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
}
|
}
|
||||||
@@ -148,12 +146,20 @@ public class Problem32 extends Problem{
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
listOfProducts.clear();
|
listOfProducts.clear();
|
||||||
sumOfPandigitals = 0;
|
sumOfPandigitals = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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
|
//Returns the sum of the pandigitals
|
||||||
public long getSumOfPandigitals(){
|
public long getSumOfPandigitals(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem4.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem4.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 08-23-20
|
//Modified: 08-27-20
|
||||||
//Find the largest palindrome made from the product of two 3-digit numbers
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -44,6 +44,7 @@ public class Problem4 extends Problem{
|
|||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -80,16 +81,22 @@ public class Problem4 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The largest palindrome is %d", palindromes.get(palindromes.size() - 1));
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
palindromes.clear();
|
palindromes.clear();
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The largest palindrome is %d", palindromes.get(palindromes.size() - 1));
|
||||||
|
}
|
||||||
//Returns the list of all palindromes
|
//Returns the list of all palindromes
|
||||||
public ArrayList<Integer> getPalindromes(){
|
public ArrayList<Integer> getPalindromes(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem5.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem5.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 08-23-20
|
//Modified: 08-27-20
|
||||||
//What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -22,8 +22,10 @@
|
|||||||
*/
|
*/
|
||||||
package mattrixwv.ProjectEuler.Problems;
|
package mattrixwv.ProjectEuler.Problems;
|
||||||
|
|
||||||
|
|
||||||
import mattrixwv.ProjectEuler.Unsolved;
|
import mattrixwv.ProjectEuler.Unsolved;
|
||||||
|
|
||||||
|
|
||||||
public class Problem5 extends Problem{
|
public class Problem5 extends Problem{
|
||||||
//Variables
|
//Variables
|
||||||
//Instance variables
|
//Instance variables
|
||||||
@@ -37,6 +39,7 @@ public class Problem5 extends Problem{
|
|||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -73,16 +76,22 @@ public class Problem5 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The smallest positive number evenly divisible by all numbers 1-20 is " + currentNum);
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
smallestNum = 0;
|
smallestNum = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The smallest positive number evenly divisible by all numbers 1-20 is " + smallestNum);
|
||||||
|
}
|
||||||
//Returns the requested number
|
//Returns the requested number
|
||||||
public int getNumber(){
|
public int getNumber(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem6.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem6.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 08-23-20
|
//Modified: 08-27-20
|
||||||
//Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -36,12 +36,13 @@ public class Problem6 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem6(){
|
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.");
|
super(String.format("Find the difference between the sum of the squares and the square of the sum of the numbers %d-%d.", START_NUM, END_NUM));
|
||||||
sumOfSquares = 0;
|
sumOfSquares = 0;
|
||||||
squareOfSum = 0;
|
squareOfSum = 0;
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -64,17 +65,23 @@ public class Problem6 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//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", Math.abs(sumOfSquares - squareOfSum));
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
squareOfSum = 0;
|
squareOfSum = 0;
|
||||||
sumOfSquares = 0;
|
sumOfSquares = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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
|
//Returns the sum of all the squares
|
||||||
public long getSumOfSquares(){
|
public long getSumOfSquares(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem7.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem7.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-01-19
|
// Created: 03-01-19
|
||||||
//Modified: 08-23-20
|
//Modified: 08-27-20
|
||||||
//What is the 10001th prime number?
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -39,11 +39,12 @@ public class Problem7 extends Problem{
|
|||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem7(){
|
public Problem7(){
|
||||||
super("What is the 10001th prime number?");
|
super(String.format("What is the %dth prime number?", NUMBER_OF_PRIMES));
|
||||||
primes = new ArrayList<Long>();
|
primes = new ArrayList<Long>();
|
||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -61,16 +62,22 @@ public class Problem7 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The " + NUMBER_OF_PRIMES + "th prime number is " + primes.get(primes.size() - 1));
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
primes.clear();
|
primes.clear();
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The %dth prime number is %d", NUMBER_OF_PRIMES, primes.get(primes.size() - 1));
|
||||||
|
}
|
||||||
//Returns the requested prime number
|
//Returns the requested prime number
|
||||||
public long getPrime(){
|
public long getPrime(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem8.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem8.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-28-19
|
// Created: 03-28-19
|
||||||
//Modified: 08-23-20
|
//Modified: 08-27-20
|
||||||
//Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
|
//Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
|
||||||
/*
|
/*
|
||||||
73167176531330624919225119674426574742355349194934
|
73167176531330624919225119674426574742355349194934
|
||||||
@@ -64,6 +64,7 @@ public class Problem8 extends Problem{
|
|||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -89,17 +90,23 @@ public class Problem8 extends Problem{
|
|||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
result = String.format("The greatest product is " + maxProduct + "\nThe numbers are " + maxNums);
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
maxNums = "";
|
maxNums = "";
|
||||||
maxProduct = 0;
|
maxProduct = 0;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return String.format("The greatest product is %d\nThe numbers are %d", maxProduct, maxNums);
|
||||||
|
}
|
||||||
//Returns the string of numbers that produces the largest product
|
//Returns the string of numbers that produces the largest product
|
||||||
public String getLargestNums(){
|
public String getLargestNums(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem9.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem9.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-02-19
|
// Created: 03-02-19
|
||||||
//Modified: 08-26-20
|
//Modified: 08-27-20
|
||||||
//There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
|
//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
|
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
|
||||||
/*
|
/*
|
||||||
@@ -22,20 +22,24 @@
|
|||||||
*/
|
*/
|
||||||
package mattrixwv.ProjectEuler.Problems;
|
package mattrixwv.ProjectEuler.Problems;
|
||||||
|
|
||||||
|
|
||||||
import mattrixwv.ProjectEuler.Unsolved;
|
import mattrixwv.ProjectEuler.Unsolved;
|
||||||
|
|
||||||
|
|
||||||
public class Problem9 extends Problem{
|
public class Problem9 extends Problem{
|
||||||
//Variables
|
//Variables
|
||||||
|
//Static variables
|
||||||
|
private static int GOAL_SUM = 1000; //The number that we want the sum of a, b, and c to equal
|
||||||
//Instance variables
|
//Instance variables
|
||||||
private int a; //Holds the size of the first side
|
private int a; //The size of the first side
|
||||||
private int b; //Holds the size of the second side
|
private int b; //The size of the second side
|
||||||
private double c; //Holds the size of the hyp
|
private double c; //The size of the hyp
|
||||||
private boolean found; //A flag to determine if we have found the solution yet
|
private boolean found; //A flag to determine if we have found the solution yet
|
||||||
|
|
||||||
//Functions
|
//Functions
|
||||||
//Constructor
|
//Constructor
|
||||||
public Problem9(){
|
public Problem9(){
|
||||||
super("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.");
|
super(String.format("There exists exactly one Pythagorean triplet for which a + b + c = %d. Find the product abc.", GOAL_SUM));
|
||||||
a = 1;
|
a = 1;
|
||||||
b = 0;
|
b = 0;
|
||||||
c = 0;
|
c = 0;
|
||||||
@@ -43,6 +47,7 @@ public class Problem9 extends Problem{
|
|||||||
}
|
}
|
||||||
//Operational functions
|
//Operational functions
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
|
@Override
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//If the problem has already been solved do nothing and end the function
|
//If the problem has already been solved do nothing and end the function
|
||||||
if(solved){
|
if(solved){
|
||||||
@@ -53,18 +58,18 @@ public class Problem9 extends Problem{
|
|||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
//Loop through all possible a's
|
//Loop through all possible a's
|
||||||
while((a < 1000) && !found){
|
while((a < GOAL_SUM) && !found){
|
||||||
b = a + 1; //b must be larger than a
|
b = a + 1; //b must be larger than a
|
||||||
c = Math.sqrt((a * a) + (b * b)); //Compute the hyp
|
c = Math.sqrt((a * a) + (b * b)); //Compute the hyp
|
||||||
|
|
||||||
//Loop through all possible b's for this a
|
//Loop through all possible b's for this a
|
||||||
while((a + b + c) < 1000){
|
while((a + b + c) < GOAL_SUM){
|
||||||
++b;
|
++b;
|
||||||
c = Math.sqrt((a * a) + (b * b));
|
c = Math.sqrt((a * a) + (b * b));
|
||||||
}
|
}
|
||||||
|
|
||||||
//If the sum == 1000 you found the number, otherwise go to the next possible a
|
//If the sum == 1000 you found the number, otherwise go to the next possible a
|
||||||
if((a + b + c) == 1000){
|
if((a + b + c) == GOAL_SUM){
|
||||||
found = true;
|
found = true;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@@ -72,21 +77,18 @@ public class Problem9 extends Problem{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!found){
|
||||||
|
throw new Unsolved("The problem was not solved!");
|
||||||
|
}
|
||||||
|
|
||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Throw a flag to show the problem is solved
|
//Throw a flag to show the problem is solved
|
||||||
solved = true;
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
|
||||||
if(found){
|
|
||||||
result = String.format("The Pythagorean triplet is %d + %d + %d\nThe numbers' product is %d", a, b, Math.round(c), a * b * Math.round(c));
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
result = "The number was not found!";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
//Reset the problem so it can be run again
|
//Reset the problem so it can be run again
|
||||||
|
@Override
|
||||||
public void reset(){
|
public void reset(){
|
||||||
super.reset();
|
super.reset();
|
||||||
a = 1;
|
a = 1;
|
||||||
@@ -95,6 +97,14 @@ public class Problem9 extends Problem{
|
|||||||
found = false;
|
found = false;
|
||||||
}
|
}
|
||||||
//Gets
|
//Gets
|
||||||
|
//Returns the result of solving the problem
|
||||||
|
@Override
|
||||||
|
public String getResult(){
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
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
|
//Returns the length of the first side
|
||||||
public int getSideA(){
|
public int getSideA(){
|
||||||
//If the problem hasn't been solved throw an exception
|
//If the problem hasn't been solved throw an exception
|
||||||
|
|||||||
Reference in New Issue
Block a user