mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Updated to improve benchmarking function and code
style
This commit is contained in:
@@ -169,8 +169,9 @@ public class Benchmark{
|
|||||||
double totalTime = 0;
|
double totalTime = 0;
|
||||||
System.out.print("Solving");
|
System.out.print("Solving");
|
||||||
for(int cnt = 0;cnt < timesToRun;++cnt){
|
for(int cnt = 0;cnt < timesToRun;++cnt){
|
||||||
//Reset the data so you are actually counting the run time a second time
|
|
||||||
System.out.print('.');
|
System.out.print('.');
|
||||||
|
//Reset the data so you are actually counting the run time a second time
|
||||||
|
problem.reset();
|
||||||
//Solve the problem
|
//Solve the problem
|
||||||
problem.solve();
|
problem.solve();
|
||||||
//Get the time data
|
//Get the time data
|
||||||
|
|||||||
@@ -24,24 +24,41 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
import mattrixwv.Stopwatch;
|
import mattrixwv.Stopwatch;
|
||||||
|
|
||||||
public abstract class Problem{
|
public abstract class Problem{
|
||||||
|
//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
|
protected String result = null; //Holds the results of the problem
|
||||||
private final String description;
|
private final String description; //Holds the description of the problem
|
||||||
|
protected boolean solved; //Shows whether the problem has already been solved
|
||||||
|
|
||||||
Problem(String description){
|
//Constructor
|
||||||
|
public Problem(String description){
|
||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Gets
|
||||||
|
//Returns the description of the problem
|
||||||
public String getDescription(){
|
public String getDescription(){
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
public String getTime(){
|
//Returns the result of solving the problem
|
||||||
return timer.getStr();
|
|
||||||
}
|
|
||||||
public String getResult(){
|
public String getResult(){
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
//Returns the time taken to run the problem as a string
|
||||||
|
public String getTime(){
|
||||||
|
return timer.getStr();
|
||||||
|
}
|
||||||
|
//Returns the timer as a stopwatch
|
||||||
public Stopwatch getTimer(){
|
public Stopwatch getTimer(){
|
||||||
return timer;
|
return timer;
|
||||||
}
|
}
|
||||||
|
//Solve the problem
|
||||||
public abstract void solve();
|
public abstract void solve();
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
timer.reset();
|
||||||
|
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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -24,31 +24,65 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem1 extends Problem{
|
public class Problem1 extends Problem{
|
||||||
//The largest number to be checked
|
//Variables
|
||||||
private static final int TOP_NUM = 999;
|
//Static variables
|
||||||
|
private static final int TOP_NUM = 999; //The largest number to be checked
|
||||||
|
//Instance variables
|
||||||
|
private int fullSum; //For the sum of all the numbers
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem1(){
|
public Problem1(){
|
||||||
super("What is the sum of all the multiples of 3 or 5 that are less than 1000");
|
super("What is the sum of all the multiples of 3 or 5 that are less than 1000");
|
||||||
|
fullSum = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
int sum = 0; //Holds the sum of all the correct elements
|
//If the problem has already been solved do nothing and end the function
|
||||||
//Check every number < 1000 to see if it is a multiple of 3 or 5. If it is add it to the running sum
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
|
//Check every number < 1000 to see if it is a multiple of 3 or 5. If it is add it to the running sum
|
||||||
for(int cnt = 1;cnt <= TOP_NUM;++cnt){
|
for(int cnt = 1;cnt <= TOP_NUM;++cnt){
|
||||||
if((cnt % 3) == 0){
|
if((cnt % 3) == 0){
|
||||||
sum += cnt;
|
fullSum += cnt;
|
||||||
}
|
}
|
||||||
else if((cnt % 5) == 0){
|
else if((cnt % 5) == 0){
|
||||||
sum += cnt;
|
fullSum += cnt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = "The sum of all numbers < " + (TOP_NUM + 1) + " is " + sum;
|
result = "The sum of all numbers < " + (TOP_NUM + 1) + " is " + fullSum;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
fullSum = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the requested sum
|
||||||
|
public int getSum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return fullSum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The sum of all numbers < 1000 is 233168
|
The sum of all numbers < 1000 is 233168
|
||||||
It took 22.000 microseconds to solve this problem.
|
It took an average of 27.833 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -27,28 +27,58 @@ import mattrixwv.Algorithms;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem10 extends Problem{
|
public class Problem10 extends Problem{
|
||||||
//The largest number to check for primes
|
//Variables
|
||||||
private static final long GOAL_NUMBER = 2000000L;
|
//Static variables
|
||||||
|
private static final long GOAL_NUMBER = 2000000 - 1; //The largest number to check for primes
|
||||||
|
//Instance variables
|
||||||
|
private long sum; //The sum of all of the prime numbers
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem10(){
|
public Problem10(){
|
||||||
super("Find the sum of all the primes below two million");
|
super("Find the sum of all the primes below two million");
|
||||||
|
sum = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
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 - 1L)); //Subtract 1 from the number so that it is < the number
|
long 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();
|
||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The sum of all the primes < %d is %d\n", GOAL_NUMBER, sum);
|
result = String.format("The sum of all the primes < %d is %d\n", GOAL_NUMBER + 1, sum);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
sum = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the sum that was requested
|
||||||
|
public long getSum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The sum of all the primes < 2000000 is 142913828922
|
The sum of all the primes < 2000000 is 142913828922
|
||||||
It took 159.610 milliseconds to solve this problem.
|
It took an average of 186.751 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
@@ -51,6 +51,8 @@ import mattrixwv.Algorithms;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem11 extends Problem{
|
public class Problem11 extends Problem{
|
||||||
|
//Variables
|
||||||
|
//Static variables
|
||||||
//This is the grid of numbers that we will be working with
|
//This is the grid of numbers that we will be working with
|
||||||
private static final Integer[][] grid = {{8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8},
|
private static final Integer[][] grid = {{8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 8},
|
||||||
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00},
|
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00},
|
||||||
@@ -72,13 +74,23 @@ public class Problem11 extends Problem{
|
|||||||
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16},
|
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16},
|
||||||
{20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54},
|
{20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54},
|
||||||
{01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}};
|
{01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}};
|
||||||
|
//Instance variables
|
||||||
|
private ArrayList<Integer> greatestProduct; //Holds the largest product we have found so far
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem11(){
|
public Problem11(){
|
||||||
super("What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?");
|
super("What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?");
|
||||||
|
greatestProduct = new ArrayList<Integer>();
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//Holds the largest product we have found so far
|
//If the problem has already been solved do nothing and end the function
|
||||||
ArrayList<Integer> greatestProduct = new ArrayList<Integer>();
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Holds the numbers we are currently working on
|
//Holds the numbers we are currently working on
|
||||||
ArrayList<Integer> currentProduct = new ArrayList<Integer>();
|
ArrayList<Integer> currentProduct = new ArrayList<Integer>();
|
||||||
|
|
||||||
@@ -183,11 +195,36 @@ public class Problem11 extends Problem{
|
|||||||
|
|
||||||
//Save the resutls
|
//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());
|
result = String.format("The greatest product of 4 numbers in a line is %d\nThe numbers are %s", Algorithms.getProd(greatestProduct), greatestProduct.toString());
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
greatestProduct.clear();
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the numbers that were being searched
|
||||||
|
ArrayList<Integer> getNumbers(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return greatestProduct;
|
||||||
|
}
|
||||||
|
//Returns the product that was requested
|
||||||
|
public int getProduct(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return Algorithms.getProd(greatestProduct);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The greatest product of 4 numbers in a line is 70600674
|
The greatest product of 4 numbers in a line is 70600674
|
||||||
The numbers are [89, 94, 97, 87]
|
The numbers are [89, 94, 97, 87]
|
||||||
It took 268.600 microseconds to solve this problem.
|
It took an average of 301.209 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -29,18 +29,32 @@ import mattrixwv.Algorithms;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem12 extends Problem{
|
public class Problem12 extends Problem{
|
||||||
//The minimum number of divisors that you want
|
//Variables
|
||||||
private static final Long GOAL_DIVISORS = 500L;
|
//Static variables
|
||||||
|
private static final Long GOAL_DIVISORS = 500L; //The minimum number of divisors that you want
|
||||||
|
//Instance variables
|
||||||
|
private long sum; //The sum of the numbers up to counter
|
||||||
|
private long counter; //The next number to be added to sum
|
||||||
|
private ArrayList<Long> divisors; //Holds the divisors of the triangular number sum
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem12(){
|
public Problem12(){
|
||||||
super("What is the value of the first triangle number to have over five hundred divisors?");
|
super("What is the value of the first triangle number to have over five hundred divisors?");
|
||||||
|
sum = 1;
|
||||||
|
counter = 2;
|
||||||
|
divisors = new ArrayList<Long>();
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Setup the other variables
|
//Setup the other variables
|
||||||
boolean foundNumber = false; //To flag whether the number has been found
|
boolean foundNumber = false; //To flag whether the number has been found
|
||||||
long sum = 1L;
|
|
||||||
long counter = 2L; //The next number to be added to the sum to make a triangular number
|
|
||||||
ArrayList<Long> divisors = new ArrayList<Long>();
|
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -64,10 +78,53 @@ public class Problem12 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//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());
|
result = String.format("The triangular number %d is the sum of all numbers >= %d and has %d divisors\n", sum, counter - 1, divisors.size());
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
sum = 1;
|
||||||
|
counter = 2;
|
||||||
|
divisors.clear();
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the triangular number
|
||||||
|
public long getTriangularNumber(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
//Get the final number that was added to the triangular number
|
||||||
|
public long getLastNumberAdded(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return counter - 1;
|
||||||
|
}
|
||||||
|
//Returns the list of divisors of the requested number
|
||||||
|
public ArrayList<Long> getDivisorsOfTriangularNumber(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return divisors;
|
||||||
|
}
|
||||||
|
//Returns the number of divisors of the requested number
|
||||||
|
public int getNumberOfDivisors(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return divisors.size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The triangulare number 76576500 is the sum of all numbers >= 12375 and has 576 divisors
|
The triangular number 76576500 is the sum of all numbers >= 12375 and has 576 divisors
|
||||||
It took 305.674 milliseconds to solve this problem.
|
It took an average of 344.173 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
@@ -132,15 +132,25 @@ import mattrixwv.Algorithms;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem13 extends Problem{
|
public class Problem13 extends Problem{
|
||||||
|
//Variables
|
||||||
|
//Instance variables
|
||||||
|
private ArrayList<BigInteger> nums; //Holds the numbers that are being summed
|
||||||
|
private BigInteger sum; //The sum of all the numbers
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem13(){
|
public Problem13(){
|
||||||
super("Work out the first ten digits of the sum of the one-hundred 50-digit numbers");
|
super("Work out the first ten digits of the sum of the one-hundred 50-digit numbers");
|
||||||
|
reserveVectors();
|
||||||
|
sum = BigInteger.ZERO;
|
||||||
}
|
}
|
||||||
public void solve(){
|
//Operational functions
|
||||||
ArrayList<BigInteger> nums = new ArrayList<BigInteger>(); //Holds the numbers that are being summed
|
//A function to set the nums vector
|
||||||
|
private void setNums(){
|
||||||
//Start the timer
|
nums.ensureCapacity(100);
|
||||||
timer.start();
|
}
|
||||||
|
//Reserve the size of the vector to speed up insertion
|
||||||
|
private void reserveVectors(){
|
||||||
//Setup the array
|
//Setup the array
|
||||||
nums.add(new BigInteger("37107287533902102798797998220837590246510135740250"));
|
nums.add(new BigInteger("37107287533902102798797998220837590246510135740250"));
|
||||||
nums.add(new BigInteger("46376937677490009712648124896970078050417018260538"));
|
nums.add(new BigInteger("46376937677490009712648124896970078050417018260538"));
|
||||||
@@ -242,20 +252,60 @@ public class Problem13 extends Problem{
|
|||||||
nums.add(new BigInteger("72107838435069186155435662884062257473692284509516"));
|
nums.add(new BigInteger("72107838435069186155435662884062257473692284509516"));
|
||||||
nums.add(new BigInteger("20849603980134001723930671666823555245252804609722"));
|
nums.add(new BigInteger("20849603980134001723930671666823555245252804609722"));
|
||||||
nums.add(new BigInteger("53503534226472524250874054075591789781264330331690"));
|
nums.add(new BigInteger("53503534226472524250874054075591789781264330331690"));
|
||||||
|
}
|
||||||
|
//Solve the problem
|
||||||
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Start the timer
|
||||||
|
timer.start();
|
||||||
|
|
||||||
|
//Setup the array
|
||||||
|
setNums();
|
||||||
|
|
||||||
//Get the sum of all the numbers
|
//Get the sum of all the numbers
|
||||||
BigInteger sum = Algorithms.getBigSum(nums);
|
sum = Algorithms.getBigSum(nums);
|
||||||
|
|
||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
//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));
|
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));
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
sum = BigInteger.ZERO;
|
||||||
|
nums.clear();
|
||||||
|
reserveVectors();
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the list 50-digit numbers
|
||||||
|
public ArrayList<BigInteger> getNumbers(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return nums;
|
||||||
|
}
|
||||||
|
//Returns the sum of the 50-digit numbers
|
||||||
|
public BigInteger getSum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672
|
The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672
|
||||||
The first 10 digits of the sum of the numbers is 5537376230
|
The first 10 digits of the sum of the numbers is 5537376230
|
||||||
It took 76.700 microseconds to solve this problem.
|
It took an average of 129.354 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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)
|
||||||
@@ -29,23 +29,33 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem14 extends Problem{
|
public class Problem14 extends Problem{
|
||||||
//This is the top number that you will be checking against the series
|
//Variables
|
||||||
private static final long MAX_NUM = 1000000L;
|
//Static variables
|
||||||
|
private static final long MAX_NUM = 1000000 - 1; //This is the top number that you will be checking against the series
|
||||||
|
//Instance variables
|
||||||
|
private long maxLength; //This is the length of the longest chain
|
||||||
|
private long maxNum; //This is the starting number of the longest chain
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem14(){
|
public Problem14(){
|
||||||
super("Which starting number, under one million, produces the longest chain using the itterative sequence?");
|
super("Which starting number, under one million, produces the longest chain using the itterative sequence?");
|
||||||
|
maxLength = 0;
|
||||||
|
maxNum = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//This is the length of the longest chain
|
//If the problem has already been solved do nothing and end the function
|
||||||
long maxLength = 0L;
|
if(solved){
|
||||||
//This is the starting number of the longest chain
|
return;
|
||||||
long maxNum = 0L;
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
//Loop through all numbers less than MAX_NUM and check them against the series
|
//Loop through all numbers less than MAX_NUM and check them against the series
|
||||||
for(long currentNum = 1L;currentNum < MAX_NUM;++currentNum){
|
for(long currentNum = 1L;currentNum <= MAX_NUM;++currentNum){
|
||||||
long currentLength = checkSeries(currentNum);
|
long currentLength = checkSeries(currentNum);
|
||||||
//If the current number has a longer series than the max then the current becomes the max
|
//If the current number has a longer series than the max then the current becomes the max
|
||||||
if(currentLength > maxLength){
|
if(currentLength > maxLength){
|
||||||
@@ -59,6 +69,9 @@ public class Problem14 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The number %d produced a chain of %d steps\n", maxNum, maxLength);
|
result = String.format("The number %d produced a chain of %d steps\n", maxNum, maxLength);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
}
|
}
|
||||||
//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){
|
||||||
@@ -78,9 +91,32 @@ public class Problem14 extends Problem{
|
|||||||
//Return the length of the series
|
//Return the length of the series
|
||||||
return length;
|
return length;
|
||||||
}
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
maxLength = 0;
|
||||||
|
maxNum = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the length of the requested chain
|
||||||
|
public long getLength(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return maxLength;
|
||||||
|
}
|
||||||
|
//Returns the starting number of the requested chain
|
||||||
|
public long getStartingNumber(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return maxNum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The number 837799 produced a chain of 525 steps
|
The number 837799 produced a chain of 525 steps
|
||||||
It took 260.459 milliseconds to solve this problem.
|
It took an average of 286.004 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -24,17 +24,27 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem15 extends Problem{
|
public class Problem15 extends Problem{
|
||||||
//The width of the box to traverse
|
//Variables
|
||||||
private static final int WIDTH = 20;
|
//Static vaiables
|
||||||
//The height of the box to traverse
|
private static final int WIDTH = 20; //The width of the box to traverse
|
||||||
private static final int LENGTH = 20;
|
private static final int LENGTH = 20; //The height of the box to traverse
|
||||||
//The number of routes from 0, 0 to 20, 20
|
//Instance variables
|
||||||
private Long numOfRoutes = 0L;
|
private long numOfRoutes; //The number of routes from 0, 0 to 20, 20
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//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("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?");
|
||||||
|
numOfRoutes = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problems
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Setup the rest of the variables
|
//Setup the rest of the variables
|
||||||
int currentX = 0; //The current x location on the grid
|
int currentX = 0; //The current x location on the grid
|
||||||
int currentY = 0; //The current y location on the grid
|
int currentY = 0; //The current y location on the grid
|
||||||
@@ -50,7 +60,10 @@ public class Problem15 extends Problem{
|
|||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The number of routes is " + numOfRoutes.toString());
|
result = String.format("The number of routes is " + numOfRoutes);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
}
|
}
|
||||||
//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
|
||||||
@@ -71,9 +84,23 @@ public class Problem15 extends Problem{
|
|||||||
move(currentX, currentY + 1);
|
move(currentX, currentY + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
numOfRoutes = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the number of routes found
|
||||||
|
public long getNumberOfRoutes(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return numOfRoutes;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The number of routes is 137846528820
|
The number of routes is 137846528820
|
||||||
It took 19.764 minutes to solve this problem.
|
It took an average of 20.702 minutes to run this problem through 10 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -27,18 +27,26 @@ import java.math.BigInteger;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem16 extends Problem{
|
public class Problem16 extends Problem{
|
||||||
//The number that is going to be raised to a power
|
//Variables
|
||||||
private static final int NUM_TO_POWER = 2;
|
//Static variables
|
||||||
//The power that the number is going to be raised to
|
private static final int NUM_TO_POWER = 2; //The number that is going to be raised to a power
|
||||||
private static final int POWER = 1000;
|
private static final int POWER = 1000; //The power that the number is going to be raised to
|
||||||
|
//Instance variables
|
||||||
|
private BigInteger num; //The number to be calculated
|
||||||
|
private int sumOfElements; //THe sum of all digits in the number
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem16(){
|
public Problem16(){
|
||||||
super("What is the sum of the digits of the number 2^1000?");
|
super("What is the sum of the digits of the number 2^1000?");
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//Setup the other variables
|
//If the problem has already been solved do nothing and end the function
|
||||||
BigInteger num = new BigInteger("0"); //The number to be calculated
|
if(solved){
|
||||||
int sumOfElements = 0; //The sum of all digits in the number
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -59,11 +67,37 @@ public class Problem16 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("%d^%d = %s\nThe sum of the elements is %d\n", NUM_TO_POWER, POWER, num.toString(), sumOfElements);
|
result = String.format("%d^%d = %s\nThe sum of the elements is %d\n", NUM_TO_POWER, POWER, num.toString(), sumOfElements);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
num = BigInteger.ZERO;
|
||||||
|
sumOfElements = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the number that was calculated
|
||||||
|
public BigInteger getNumber(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
//Return the sum of the digits of the number
|
||||||
|
public int getSum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return sumOfElements;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
|
2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
|
||||||
The sum of the elements is 1366
|
The sum of the elements is 1366
|
||||||
It took 133.800 microseconds to solve this problem.
|
It took an average of 60.914 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -24,16 +24,26 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem17 extends Problem{
|
public class Problem17 extends Problem{
|
||||||
//The first number to be
|
//Variables
|
||||||
private static final int START_NUM = 1;
|
//Static variables
|
||||||
private static final int STOP_NUM = 1000;
|
private static final int START_NUM = 1; //This is the smallest number to get the words of
|
||||||
|
private static final int STOP_NUM = 1000; //This is the largest number to get the words of
|
||||||
|
//Instance variables
|
||||||
|
private long letterCount; //This is the cumulative number of letters in the words of the numbers
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//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("If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?");
|
||||||
|
letterCount = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//Setup the variables needed
|
//If the problem has already been solved do nothing and end the function
|
||||||
int sumOfLetters = 0;
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -43,15 +53,24 @@ public class Problem17 extends Problem{
|
|||||||
//Pass the number to a function that will create a string for the number
|
//Pass the number to a function that will create a string for the number
|
||||||
String currentNumString = getStringFromNum(num);
|
String currentNumString = getStringFromNum(num);
|
||||||
//Pass the string to the function that will count the number of letters in it, ignoring whitespace and punctuation and add that number to the running tally
|
//Pass the string to the function that will count the number of letters in it, ignoring whitespace and punctuation and add that number to the running tally
|
||||||
sumOfLetters += getNumberChars(currentNumString);
|
letterCount += getNumberChars(currentNumString);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The sum of all the letters in all the numbers %d-%d is %d\n", START_NUM, STOP_NUM, sumOfLetters);
|
result = String.format("The sum of all the letters in all the numbers %d-%d is %d\n", START_NUM, STOP_NUM, letterCount);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
}
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
letterCount = 0;
|
||||||
|
}
|
||||||
|
//This function makes a word out of the number passed into it
|
||||||
private String getStringFromNum(int number){
|
private String getStringFromNum(int number){
|
||||||
String numberString = new String();
|
String numberString = new String();
|
||||||
//Starting with the largest digit create a string based on the number passed in
|
//Starting with the largest digit create a string based on the number passed in
|
||||||
@@ -191,6 +210,7 @@ public class Problem17 extends Problem{
|
|||||||
//Return the string
|
//Return the string
|
||||||
return numberString;
|
return numberString;
|
||||||
}
|
}
|
||||||
|
//This counts the number of letters in the string that is passed in (ignoring numbers and punctuation)
|
||||||
private int getNumberChars(String number){
|
private int getNumberChars(String number){
|
||||||
int sumOfLetters = 0;
|
int sumOfLetters = 0;
|
||||||
//Start at location 0 and count the number of letters, ignoring punctuation and whitespace
|
//Start at location 0 and count the number of letters, ignoring punctuation and whitespace
|
||||||
@@ -203,9 +223,18 @@ public class Problem17 extends Problem{
|
|||||||
//Return the number of letters
|
//Return the number of letters
|
||||||
return sumOfLetters;
|
return sumOfLetters;
|
||||||
}
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the number of letters asked for
|
||||||
|
public long getLetterCount(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return letterCount;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The sum of all the letters in all the numbers 1-1000 is 21124
|
The sum of all the letters in all the numbers 1-1000 is 21124
|
||||||
It took 285.699 microseconds to solve this problem.
|
It took an average of 507.896 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-20
|
||||||
//Find the maximum total from top to bottom
|
//Find the maximum total from top to bottom
|
||||||
/*
|
/*
|
||||||
75
|
75
|
||||||
@@ -46,10 +46,7 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem18 extends Problem{
|
public class Problem18 extends Problem{
|
||||||
//The number of rows in the array
|
//Structures
|
||||||
private static final int NUM_ROWS = 15;
|
|
||||||
//The list to hold the numbers in
|
|
||||||
private ArrayList<ArrayList<Integer>> list;
|
|
||||||
//Used to keep track of where the best location came from
|
//Used to keep track of where the best location came from
|
||||||
private static class location{
|
private static class location{
|
||||||
public int xLocation;
|
public int xLocation;
|
||||||
@@ -66,9 +63,24 @@ public class Problem18 extends Problem{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Variables
|
||||||
|
//Static variables
|
||||||
|
private static final int NUM_ROWS = 15; //The number of rows in the array
|
||||||
|
private ArrayList<ArrayList<Integer>> list; //The list to hold the numbers in
|
||||||
|
//Instance variables
|
||||||
|
private ArrayList<location> foundPoints; //For the points that I have already found the shortest distance to
|
||||||
|
private ArrayList<location> possiblePoints; //For the locations you are checking this round
|
||||||
|
private int actualTotal; //The true total of the path from the top to the bottom
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem18(){
|
public Problem18(){
|
||||||
super("Find the maximum total from top to bottom");
|
super("Find the maximum total from top to bottom");
|
||||||
|
foundPoints = new ArrayList<location>();
|
||||||
|
possiblePoints = new ArrayList<location>();
|
||||||
|
actualTotal = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
//This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest
|
//This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest
|
||||||
private void invert(ArrayList<ArrayList<Integer>> list){
|
private void invert(ArrayList<ArrayList<Integer>> list){
|
||||||
//Loop through every row in the list
|
//Loop through every row in the list
|
||||||
@@ -84,6 +96,8 @@ public class Problem18 extends Problem{
|
|||||||
private void removeHelper(ArrayList<location> possiblePoints, final location minLoc){
|
private void removeHelper(ArrayList<location> possiblePoints, final location minLoc){
|
||||||
possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation)));
|
possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation)));
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Setup the list
|
||||||
private void setupList(){
|
private void setupList(){
|
||||||
list = new ArrayList<ArrayList<Integer>>();
|
list = new ArrayList<ArrayList<Integer>>();
|
||||||
list.add(new ArrayList<Integer>(Arrays.asList(75)));
|
list.add(new ArrayList<Integer>(Arrays.asList(75)));
|
||||||
@@ -102,7 +116,13 @@ public class Problem18 extends Problem{
|
|||||||
list.add(new ArrayList<Integer>(Arrays.asList(63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31)));
|
list.add(new ArrayList<Integer>(Arrays.asList(63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31)));
|
||||||
list.add(new ArrayList<Integer>(Arrays.asList(04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23)));
|
list.add(new ArrayList<Integer>(Arrays.asList(04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23)));
|
||||||
}
|
}
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
@@ -112,9 +132,7 @@ public class Problem18 extends Problem{
|
|||||||
//Invert the list
|
//Invert the list
|
||||||
invert(list);
|
invert(list);
|
||||||
|
|
||||||
ArrayList<location> foundPoints = new ArrayList<location>(); //For the points that I have already found the shortest distance to
|
|
||||||
foundPoints.add(new location(0, 0, list.get(0).get(0), false)); //Add the first row as a found point because you have to go through the tip
|
foundPoints.add(new location(0, 0, list.get(0).get(0), false)); //Add the first row as a found point because you have to go through the tip
|
||||||
ArrayList<location> possiblePoints = new ArrayList<location>(); //For the locations you are checking this round
|
|
||||||
//Add the second row as possible points
|
//Add the second row as possible points
|
||||||
possiblePoints.add(new location(0, 1, (list.get(0).get(0) + list.get(1).get(0)), true));
|
possiblePoints.add(new location(0, 1, (list.get(0).get(0) + list.get(1).get(0)), true));
|
||||||
possiblePoints.add(new location(1, 1, (list.get(0).get(0) + list.get(1).get(1)), false));
|
possiblePoints.add(new location(1, 1, (list.get(0).get(0) + list.get(1).get(1)), false));
|
||||||
@@ -161,10 +179,58 @@ public class Problem18 extends Problem{
|
|||||||
int actualTotal = ((100 * NUM_ROWS) - foundPoints.get(foundPoints.size() - 1).total);
|
int actualTotal = ((100 * NUM_ROWS) - foundPoints.get(foundPoints.size() - 1).total);
|
||||||
|
|
||||||
result = String.format("The value of the longest path is " + actualTotal);
|
result = String.format("The value of the longest path is " + actualTotal);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
foundPoints.clear();
|
||||||
|
possiblePoints.clear();
|
||||||
|
actualTotal = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the pyramid that was traversed as a string
|
||||||
|
public String getPyramid(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
StringBuilder results = new StringBuilder();
|
||||||
|
|
||||||
|
//Loop through all elements of the list and print them
|
||||||
|
for(ArrayList<Integer> row : list){
|
||||||
|
for(int column : row){
|
||||||
|
results.append(String.format("%2d ", column));
|
||||||
|
}
|
||||||
|
results.append('\n');
|
||||||
|
}
|
||||||
|
return results.toString();
|
||||||
|
}
|
||||||
|
//Returns the trail the algorithm took as a string
|
||||||
|
public String getTrail(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder results = new StringBuilder();
|
||||||
|
//TODO: Implement this
|
||||||
|
results.append("This has not yet been implemented");
|
||||||
|
return results.toString();
|
||||||
|
}
|
||||||
|
//Returns the total that was asked for
|
||||||
|
public int getTotal(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return actualTotal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The value of the longest path is 1074
|
The value of the longest path is 1074
|
||||||
It took 278.899 microseconds to solve this problem.
|
It took an average of 252.472 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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.
|
||||||
@@ -35,18 +35,28 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem19 extends Problem{
|
public class Problem19 extends Problem{
|
||||||
|
//Variables
|
||||||
|
//Static variables
|
||||||
|
//An easier way to signal day of the week
|
||||||
private static enum DAYS{SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, NUMBER_OF_DAYS, ERROR};
|
private static enum DAYS{SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, NUMBER_OF_DAYS, ERROR};
|
||||||
//The first year we are going to test
|
private static final int START_YEAR = 1901; //The first year we are going to test
|
||||||
private static final int START_YEAR = 1901;
|
private static final int END_YEAR = 2000; //The last year we are going to test
|
||||||
//The last year we are going to test
|
//Instance variables
|
||||||
private static final int END_YEAR = 2000;
|
private long totalSundays;
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem19(){
|
public Problem19(){
|
||||||
super("How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?");
|
super("How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?");
|
||||||
|
totalSundays = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//The total number of sundays
|
//If the problem has already been solved do nothing and end the function
|
||||||
long totalSundays = 0L;
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -70,6 +80,9 @@ public class Problem19 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//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);
|
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 porblem is solved
|
||||||
|
solved = true;
|
||||||
}
|
}
|
||||||
//Return the day of the week that the date you pass into it is on
|
//Return the day of the week that the date you pass into it is on
|
||||||
private DAYS getDay(int month, int day, int year){
|
private DAYS getDay(int month, int day, int year){
|
||||||
@@ -168,9 +181,23 @@ public class Problem19 extends Problem{
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
totalSundays = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the total sundays that were asked for
|
||||||
|
public long getTotalSundays(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return totalSundays;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
There are 171 Sundays that landed on the first of the months from 1901 to 2000
|
There are 171 Sundays that landed on the first of the months from 1901 to 2000
|
||||||
It took 1.767 milliseconds to solve this problem.
|
It took an average of 4.656 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -29,33 +29,65 @@ import mattrixwv.Algorithms;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem2 extends Problem{
|
public class Problem2 extends Problem{
|
||||||
//The largest number that will be checked as a fibonacci number
|
//Variables
|
||||||
private static final int TOP_NUM = 3999999;
|
//Static variables
|
||||||
|
private static final int TOP_NUM = 4000000 - 1; //The largest number that will be checked as a fibonacci number
|
||||||
|
//Instance variables
|
||||||
|
private int fullSum; //Holds the sum of all the numbers
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem2(){
|
public Problem2(){
|
||||||
super("What is the sum of the even Fibonacci numbers less than 4,000,000?");
|
super("What is the sum of the even Fibonacci numbers less than 4,000,000?");
|
||||||
|
fullSum = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
//Get a list of all fibonacci numbers < 4,000,000
|
//Get a list of all fibonacci numbers < 4,000,000
|
||||||
ArrayList<Integer> fibNums = Algorithms.getAllFib(TOP_NUM);
|
ArrayList<Integer> fibNums = Algorithms.getAllFib(TOP_NUM);
|
||||||
int sum = 0;
|
|
||||||
//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){
|
||||||
//If the number is even add it to the running tally
|
//If the number is even add it to the running tally
|
||||||
if((num % 2) == 0){
|
if((num % 2) == 0){
|
||||||
sum += num;
|
fullSum += num;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The sum of all even fibonacci numbers <= %d is %d\n", TOP_NUM, sum);
|
result = String.format("The sum of all even fibonacci numbers <= %d is %d\n", TOP_NUM, fullSum);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
fullSum = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the requested sum
|
||||||
|
public int getSum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return fullSum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The sum of all even fibonacci numbers <= 3999999 is 4613732
|
The sum of all even fibonacci numbers <= 3999999 is 4613732
|
||||||
It took 36.299 microseconds to solve this problem.
|
It took an average of 29.713 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -27,16 +27,27 @@ import java.math.BigInteger;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem20 extends Problem{
|
public class Problem20 extends Problem{
|
||||||
//The largest number that will be multiplied
|
//Variables
|
||||||
private static final int TOP_NUM = 100;
|
//Static variables
|
||||||
|
private static final int TOP_NUM = 100; //The largest number that will be multiplied
|
||||||
|
//Instance variables
|
||||||
|
private BigInteger num; //Holds the number 100!
|
||||||
|
private long sum; //The sum of the digts of num
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem20(){
|
public Problem20(){
|
||||||
super("What is the sum of the digits of 100!?");
|
super("What is the sum of the digits of 100!?");
|
||||||
|
num = BigInteger.ONE;
|
||||||
|
sum = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//The number that is being generated
|
//If the problem has already been solved do nothing and end the function
|
||||||
BigInteger num = new BigInteger("1");
|
if(solved){
|
||||||
long sum = 0L; //The sum of the digits of num
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -59,11 +70,45 @@ public class Problem20 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("%d! = %s\nThe sum of the digits is: %d\n", TOP_NUM, num.toString(), sum);
|
result = String.format("%d! = %s\nThe sum of the digits is: %d\n", TOP_NUM, num.toString(), sum);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the proble so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
num = BigInteger.ONE;
|
||||||
|
sum = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the number 100!
|
||||||
|
public BigInteger getNumber(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
//Returns the number 100! in a string
|
||||||
|
public String getNumberString(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return num.toString(10);
|
||||||
|
}
|
||||||
|
//Returns the sum of the digits of 100!
|
||||||
|
public long getSum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Restuls:
|
/* Restuls:
|
||||||
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
|
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
|
||||||
The sum of the digits is: 648
|
The sum of the digits is: 648
|
||||||
It took 137.199 microseconds to solve this problem.
|
It took an average of 91.162 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -30,20 +30,36 @@ import java.util.Collections;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem21 extends Problem{
|
public class Problem21 extends Problem{
|
||||||
//The number that is > the largest number to be checked
|
//Variables
|
||||||
private static final int LIMIT = 10000;
|
//Static variables
|
||||||
|
private static final int LIMIT = 10000; //The number that is > the largest number to be checked
|
||||||
|
//Instance variables
|
||||||
|
private ArrayList<Integer> divisorSum; //Holds the sum of the divisors of the subscript number
|
||||||
|
private ArrayList<Integer> amicable; //Holds all amicable numbers
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem21(){
|
public Problem21(){
|
||||||
super("Evaluate the sum of all the amicable numbers under 10000");
|
super("Evaluate the sum of all the amicable numbers under 10000");
|
||||||
|
divisorSum = new ArrayList<Integer>();
|
||||||
|
amicable = new ArrayList<Integer>();
|
||||||
|
reserveArray();
|
||||||
}
|
}
|
||||||
public void solve(){
|
//Operational functions
|
||||||
//Setup the variables
|
//Reserve the size of the array to speed up insertion
|
||||||
ArrayList<Integer> divisorSum = new ArrayList<Integer>(); //Holds the sum of the divisors of the subscript number
|
private void reserveArray(){
|
||||||
divisorSum.ensureCapacity(LIMIT); //Reserving it now makes it faster later
|
divisorSum.ensureCapacity(LIMIT); //Reserving it now makes it faster later
|
||||||
//Make sure the arraylist is filled with 0's
|
//Make sure the arraylist is filled with 0's
|
||||||
while(divisorSum.size() < LIMIT){
|
while(divisorSum.size() < LIMIT){
|
||||||
divisorSum.add(0);
|
divisorSum.add(0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
//Solve the problem
|
||||||
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -57,7 +73,6 @@ public class Problem21 extends Problem{
|
|||||||
divisorSum.set(cnt, Algorithms.getSum(divisors)); //Add the sum of the divisors of the vector
|
divisorSum.set(cnt, Algorithms.getSum(divisors)); //Add the sum of the divisors of the vector
|
||||||
}
|
}
|
||||||
//Check every sum of divisors in the list for a matching sum
|
//Check every sum of divisors in the list for a matching sum
|
||||||
ArrayList<Integer> amicable = new ArrayList<Integer>();
|
|
||||||
for(int cnt = 1;cnt < divisorSum.size();++cnt){
|
for(int cnt = 1;cnt < divisorSum.size();++cnt){
|
||||||
int sum = divisorSum.get(cnt);
|
int sum = divisorSum.get(cnt);
|
||||||
//If the sum is greater than the number of divisors then it is impossible to be amicable. Skip the number and continue
|
//If the sum is greater than the number of divisors then it is impossible to be amicable. Skip the number and continue
|
||||||
@@ -87,6 +102,33 @@ public class Problem21 extends Problem{
|
|||||||
result += amicable.get(cnt).toString() + "\n";
|
result += amicable.get(cnt).toString() + "\n";
|
||||||
}
|
}
|
||||||
result += String.format("The sum of all of these amicable numbers is %d\n", Algorithms.getSum(amicable));
|
result += String.format("The sum of all of these amicable numbers is %d\n", Algorithms.getSum(amicable));
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
divisorSum.clear();
|
||||||
|
amicable.clear();
|
||||||
|
reserveArray();
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns a vector with all of the amicable numbers calculated
|
||||||
|
public ArrayList<Integer> getAmicable(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return amicable;
|
||||||
|
}
|
||||||
|
//Returns the sum of all of the amicable numbers
|
||||||
|
public int getSum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return Algorithms.getSum(amicable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,5 +145,5 @@ All amicable numbers less than 10000 are
|
|||||||
6232
|
6232
|
||||||
6368
|
6368
|
||||||
The sum of all of these amicable numbers is 31626
|
The sum of all of these amicable numbers is 31626
|
||||||
It took 8.616 milliseconds to solve this problem.
|
It took an average of 9.848 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -30,6 +30,9 @@ import java.util.Collections;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem22 extends Problem{
|
public class Problem22 extends Problem{
|
||||||
|
//Variables
|
||||||
|
//Static variables
|
||||||
|
//Holds the names that will be scored
|
||||||
private static final ArrayList<String> names = new ArrayList<String>(Arrays.asList("MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN",
|
private static final ArrayList<String> names = new ArrayList<String>(Arrays.asList("MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA","NANCY","KAREN",
|
||||||
"BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON","MICHELLE","LAURA","SARAH","KIMBERLY","DEBORAH","JESSICA","SHIRLEY",
|
"BETTY","HELEN","SANDRA","DONNA","CAROL","RUTH","SHARON","MICHELLE","LAURA","SARAH","KIMBERLY","DEBORAH","JESSICA","SHIRLEY",
|
||||||
"CYNTHIA","ANGELA","MELISSA","BRENDA","AMY","ANNA","REBECCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBRA","AMANDA","STEPHANIE",
|
"CYNTHIA","ANGELA","MELISSA","BRENDA","AMY","ANNA","REBECCA","VIRGINIA","KATHLEEN","PAMELA","MARTHA","DEBRA","AMANDA","STEPHANIE",
|
||||||
@@ -398,14 +401,24 @@ public class Problem22 extends Problem{
|
|||||||
"HAYDEN","HARLAND","ARNOLDO","RUEBEN","LEANDRO","KRAIG","JERRELL","JEROMY","HOBERT","CEDRICK","ARLIE","WINFORD","WALLY","LUIGI",
|
"HAYDEN","HARLAND","ARNOLDO","RUEBEN","LEANDRO","KRAIG","JERRELL","JEROMY","HOBERT","CEDRICK","ARLIE","WINFORD","WALLY","LUIGI",
|
||||||
"KENETH","JACINTO","GRAIG","FRANKLYN","EDMUNDO","SID","PORTER","LEIF","JERAMY","BUCK","WILLIAN","VINCENZO","SHON","LYNWOOD","JERE",
|
"KENETH","JACINTO","GRAIG","FRANKLYN","EDMUNDO","SID","PORTER","LEIF","JERAMY","BUCK","WILLIAN","VINCENZO","SHON","LYNWOOD","JERE",
|
||||||
"HAI","ELDEN","DORSEY","DARELL","BRODERICK","ALONSO"));
|
"HAI","ELDEN","DORSEY","DARELL","BRODERICK","ALONSO"));
|
||||||
|
//Instance variables
|
||||||
|
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
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem22(){
|
public Problem22(){
|
||||||
super("What is the total of all the name scores in this file?");
|
super("What is the total of all the name scores in this file?");
|
||||||
|
sums = new ArrayList<Long>();
|
||||||
|
prod = new ArrayList<Long>();
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//Setup the variables
|
//If the problem has already been solved do nothing and end the function
|
||||||
ArrayList<Long> sums = new ArrayList<Long>(); //Holds the score based on the sum of the characters in the name
|
if(solved){
|
||||||
ArrayList<Long> prod = new ArrayList<Long>(); //Holds the score based on the sum of characters and the location in alphabetical order
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -434,10 +447,36 @@ public class Problem22 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The answer to the question is %d\n", sum);
|
result = String.format("The answer to the question is %d\n", sum);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
sums.clear();
|
||||||
|
prod.clear();
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the vector of the names being scored
|
||||||
|
public ArrayList<String> getNames(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
//Returns the sum of the names scores
|
||||||
|
public long getNameScoreSum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return Algorithms.getLongSum(prod);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The answer to the question is 871198282
|
The answer to the question is 871198282
|
||||||
It took 935.700 microseconds to solve this problem.
|
It took an average of 2.609 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -29,21 +29,36 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem23 extends Problem{
|
public class Problem23 extends Problem{
|
||||||
//The largest number to be checked
|
//Variables
|
||||||
public static final int MAX_NUM = 28123;
|
//Static variables
|
||||||
|
private static final int MAX_NUM = 28123; //The largest number to be checked
|
||||||
|
//Instance variables
|
||||||
|
private ArrayList<Integer> divisorSums; //This gives the sum of the divisors at subscripts
|
||||||
|
private long sum; //The sum of all the numbers we are looking for
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem23(){
|
public Problem23(){
|
||||||
super("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers");
|
super("Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers");
|
||||||
|
divisorSums = new ArrayList<Integer>();
|
||||||
|
reserveArray();
|
||||||
|
sum = 0;
|
||||||
}
|
}
|
||||||
public void solve(){
|
//Operational functions
|
||||||
//Setup the variables
|
//Reserve the size of the array to speed up insertion
|
||||||
ArrayList<Integer> divisorSums = new ArrayList<Integer>(); //Holds the sum of all the divisors of a number
|
private void reserveArray(){
|
||||||
divisorSums.ensureCapacity(MAX_NUM); //It is faster to reserve the appropriate amount of ram now
|
divisorSums.ensureCapacity(MAX_NUM); //It is faster to reserve the appropriate amount of ram now
|
||||||
//Make sure every element has a 0 in it's location
|
//Make sure every element has a 0 in it's location
|
||||||
for(int cnt = 0;cnt < MAX_NUM;++cnt){
|
while(divisorSums.size() <= MAX_NUM){
|
||||||
divisorSums.add(0);
|
divisorSums.add(0);
|
||||||
}
|
}
|
||||||
divisorSums.add(0);
|
}
|
||||||
|
//Solve the problem
|
||||||
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -67,7 +82,6 @@ public class Problem23 extends Problem{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Check if each number can be the sum of 2 abundant numbers and add to the sum if no
|
//Check if each number can be the sum of 2 abundant numbers and add to the sum if no
|
||||||
long sum = 0L;
|
|
||||||
for(int cnt = 1;cnt < MAX_NUM;++cnt){
|
for(int cnt = 1;cnt < MAX_NUM;++cnt){
|
||||||
if(!isSum(abund, cnt)){
|
if(!isSum(abund, cnt)){
|
||||||
sum += cnt;
|
sum += cnt;
|
||||||
@@ -79,6 +93,9 @@ public class Problem23 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The answer is %d\n", sum);
|
result = String.format("The answer is %d\n", sum);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
}
|
}
|
||||||
//A function that returns true if num can be created by adding two elements from abund and false if it cannot
|
//A function that returns true if num can be created by adding two elements from abund and false if it cannot
|
||||||
private boolean isSum(final ArrayList<Integer> abund, int num){
|
private boolean isSum(final ArrayList<Integer> abund, int num){
|
||||||
@@ -99,9 +116,17 @@ public class Problem23 extends Problem{
|
|||||||
//If you have run through the entire list and did not find a sum then it is false
|
//If you have run through the entire list and did not find a sum then it is false
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
//Returns the sum of the numbers asked for
|
||||||
|
public long getSum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The answer is 4179871
|
The answer is 4179871
|
||||||
It took 12.277 seconds to solve this problem.
|
It took an average of 15.048 seconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -29,31 +29,67 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem24 extends Problem{
|
public class Problem24 extends Problem{
|
||||||
//The number of the permutation I need
|
//Variables
|
||||||
|
//Static variables
|
||||||
private static final int NEEDED_PERM = 1000000; //The number of the permutation that you need
|
private static final int NEEDED_PERM = 1000000; //The number of the permutation that you need
|
||||||
|
private static String nums = "0123456789"; //All of the characters that we need to get the permutations of
|
||||||
|
//Instance variables
|
||||||
|
private ArrayList<String> permutations; //Holds all of the permutations of the string nums
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//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("What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?");
|
||||||
|
permutations = new ArrayList<String>();
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problems
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//Setup the variables
|
//If the problem has already been solved do nothing and end the function
|
||||||
String nums = "0123456789"; //The string that you are trying to find the permutations of
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
//Get all the permutations of the string
|
//Get all the permutations of the string
|
||||||
ArrayList<String> permutations = Algorithms.getPermutations(nums);
|
permutations = Algorithms.getPermutations(nums);
|
||||||
|
|
||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The 1 millionth permutation is %s\n", permutations.get(NEEDED_PERM - 1));
|
result = String.format("The 1 millionth permutation is %s\n", permutations.get(NEEDED_PERM - 1));
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
permutations.clear();
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns a vector with all of the permutations
|
||||||
|
public ArrayList<String> getPermutationsList(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return permutations;
|
||||||
|
}
|
||||||
|
//Returns the specific permutations you are looking for
|
||||||
|
public String getPermutations(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return permutations.get(NEEDED_PERM - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results
|
/* Results
|
||||||
The 1 millionth permutation is 2783915460
|
The 1 millionth permutation is 2783915460
|
||||||
It took 1.650 seconds to solve this problem.
|
It took an average of 1.140 seconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -29,16 +29,27 @@ import java.math.BigInteger;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem25 extends Problem{
|
public class Problem25 extends Problem{
|
||||||
//The number of digits to calculate up to
|
//Variables
|
||||||
private static final int NUM_DIGITS = 1000;
|
//Static variables
|
||||||
|
private static final int NUM_DIGITS = 1000; //The number of digits to calculate up to
|
||||||
|
//Instance variables
|
||||||
|
private BigInteger number; //The current Fibonacci number
|
||||||
|
private BigInteger index; //The index of the current Fibonacci number just calculated
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem25(){
|
public Problem25(){
|
||||||
super("What is the index of the first term in the Fibonacci sequence to contain 1000 digits?");
|
super("What is the index of the first term in the Fibonacci sequence to contain 1000 digits?");
|
||||||
|
number = BigInteger.ZERO;
|
||||||
|
index = BigInteger.TWO;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//Setup the variables
|
//If the problem has already been solved do nothing and end the function
|
||||||
BigInteger number = BigInteger.ZERO; //Holds the current Fibonacci number
|
if(solved){
|
||||||
BigInteger index = BigInteger.valueOf(2L); //The index of the Fibonacci number just calculated
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -54,11 +65,61 @@ public class Problem25 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//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);
|
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 porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
number = BigInteger.ZERO;
|
||||||
|
index = BigInteger.TWO;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the Fibonacci number asked for
|
||||||
|
public BigInteger getNumber(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return number;
|
||||||
|
}
|
||||||
|
//Returns the Fibonacci number asked for as a string
|
||||||
|
public String getNumberString(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return number.toString(10);
|
||||||
|
}
|
||||||
|
//Returns the index of the requested Fibonacci number
|
||||||
|
public BigInteger getIndex(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
//Returns the index of the requested Fibonacci number as a string
|
||||||
|
public String getIndexString(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return index.toString(10);
|
||||||
|
}
|
||||||
|
//Returns the index of the requested Fibonacci number as a long
|
||||||
|
public long getIndexInt(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return index.longValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The first Fibonacci number with 1000 digits is 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816
|
The first Fibonacci number with 1000 digits is 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816
|
||||||
Its index is 4782
|
Its index is 4782
|
||||||
It took 643.584 milliseconds to solve this problem.
|
It took an average of 662.517 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -29,17 +29,27 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem26 extends Problem{
|
public class Problem26 extends Problem{
|
||||||
//The number of digits needed in the number
|
//Variables
|
||||||
private static final int TOP_NUM = 999;
|
//Static variables
|
||||||
|
private static final int TOP_NUM = 999; //The number of digits needed in the number
|
||||||
|
//Instance variables
|
||||||
|
private int longestCycle; //The length of the longest cycle
|
||||||
|
private int longestNumber; //The starting denominator of the longest cycle
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//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("Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.");
|
||||||
|
longestCycle = 0;
|
||||||
|
longestNumber = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//The length of the longest cycle
|
//If the problem has already been solved do nothing and end the function
|
||||||
int longestCycle = 0;
|
if(solved){
|
||||||
//The starting denominator of the longest cycle
|
return;
|
||||||
int longestNumber = 0;
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -87,11 +97,37 @@ public class Problem26 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The longest cycle is %d digits long\nIt started with the number %d\n", longestCycle, longestNumber);
|
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 porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
longestCycle = 0;
|
||||||
|
longestNumber = 1;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the length of the longest cycle
|
||||||
|
public int getLongestCycle(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return longestCycle;
|
||||||
|
}
|
||||||
|
//Returns the denominator that starts the longest cycle
|
||||||
|
public int getLongestNumber(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return longestNumber;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The longest cycle is 982 digits long
|
The longest cycle is 982 digits long
|
||||||
It started with the number 983
|
It started with the number 983
|
||||||
It took 10.429 milliseconds to solve this problem.
|
It took an average of 12.182 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -29,22 +29,36 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem27 extends Problem{
|
public class Problem27 extends Problem{
|
||||||
//The A for the most n's generated
|
//Variables
|
||||||
private int topA = 0;
|
//Instance variables
|
||||||
//The B for the most n's generated
|
private int topA; //The A for the most n's generated
|
||||||
private int topB = 0;
|
private int topB; //The B for the most n's generated
|
||||||
//The most n's generated
|
private int topN; //The most n's generated
|
||||||
private int topN = 0;
|
private ArrayList<Integer> primes; //A list of all primes that could possibly be generated with this formula
|
||||||
//A list of all primes that could possibly be generated with this formula
|
|
||||||
private ArrayList<Integer> primes = Algorithms.getPrimes(12000);
|
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//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("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");
|
||||||
|
topA = 0;
|
||||||
|
topB = 0;
|
||||||
|
topN = 0;
|
||||||
|
primes = new ArrayList<Integer>();
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
|
//Get the primes
|
||||||
|
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 = -999;a <= 999;++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
|
||||||
@@ -72,6 +86,42 @@ public class Problem27 extends Problem{
|
|||||||
|
|
||||||
//Save the restuls
|
//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);
|
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 porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
topA = 0;
|
||||||
|
topB = 0;
|
||||||
|
topN = 0;
|
||||||
|
primes.clear();
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the top A that was generated
|
||||||
|
public int getTopA(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return topA;
|
||||||
|
}
|
||||||
|
//Returns the top B that was generated
|
||||||
|
public int getTopB(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return topB;
|
||||||
|
}
|
||||||
|
//Returns the top N that was generated
|
||||||
|
public int getTopN(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return topN;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,5 +129,5 @@ public class Problem27 extends Problem{
|
|||||||
The greatest number of primes found is 70
|
The greatest number of primes found is 70
|
||||||
It was found with A = -61, B = 971
|
It was found with A = -61, B = 971
|
||||||
The product of A and B is -59231
|
The product of A and B is -59231
|
||||||
It took 3.184 seconds to solve this problem.
|
It took an average of 3.764 seconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -27,14 +27,19 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem28 extends Problem{
|
public class Problem28 extends Problem{
|
||||||
//Holds the grid that we will be filling and searching
|
//Variables
|
||||||
private static ArrayList<ArrayList<Integer>> grid;
|
//Static variables
|
||||||
//Holds the sum of the diagonals of the grid
|
private static ArrayList<ArrayList<Integer>> grid; //Holds the grid that we will be filling and searching
|
||||||
private int sumOfDiagonals = 0;
|
//Instance variables
|
||||||
|
private int sumOfDiagonals; //Holds the sum of the diagonals of the grid
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem28(){
|
public Problem28(){
|
||||||
super("What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral");
|
super("What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral");
|
||||||
|
sumOfDiagonals = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
//Sets up the grid
|
//Sets up the grid
|
||||||
private void setupGrid(){
|
private void setupGrid(){
|
||||||
grid = new ArrayList<ArrayList<Integer>>();
|
grid = new ArrayList<ArrayList<Integer>>();
|
||||||
@@ -108,7 +113,13 @@ public class Problem28 extends Problem{
|
|||||||
--rightSide;
|
--rightSide;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
@@ -122,10 +133,36 @@ public class Problem28 extends Problem{
|
|||||||
|
|
||||||
//Print the restuls
|
//Print the restuls
|
||||||
result = String.format("The sum of the diagonals in the given grid is %d\n", sumOfDiagonals);
|
result = String.format("The sum of the diagonals in the given grid is %d\n", sumOfDiagonals);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
}
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
sumOfDiagonals = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the grid
|
||||||
|
public ArrayList<ArrayList<Integer>> getGrid(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return grid;
|
||||||
|
}
|
||||||
|
//Returns the sum of the diagonals
|
||||||
|
public int getSum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return sumOfDiagonals;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The sum of the diagonals in the given grid is 669171001
|
The sum of the diagonals in the given grid is 669171001
|
||||||
It took 16.687 milliseconds to solve this problem.
|
It took an average of 17.307 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -28,23 +28,28 @@ import java.math.BigInteger;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem29 extends Problem{
|
public class Problem29 extends Problem{
|
||||||
//The lowest possible value for a
|
//Variables
|
||||||
private static final int BOTTOM_A = 2;
|
//Static variables
|
||||||
//The highest possible value for a
|
private static final int BOTTOM_A = 2; //The lowest possible value for a
|
||||||
private static final int TOP_A = 100;
|
private static final int TOP_A = 100; //The highest possible value for a
|
||||||
//The lowest possible value for b
|
private static final int BOTTOM_B = 2; //The lowest possible value for b
|
||||||
private static final int BOTTOM_B = 2;
|
private static final int TOP_B = 100; //The highest possible value for b
|
||||||
//The highest possible value for b
|
//Instance variables
|
||||||
private static final int TOP_B = 100;
|
private ArrayList<BigInteger> unique; //Holds all unique values generated
|
||||||
//Holds all unique values generated
|
|
||||||
private ArrayList<BigInteger> unique;
|
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//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("How many distinct terms are in the sequence generated by a^b for 2 <= a <= 100 and 2 <= b <= 100?");
|
||||||
}
|
|
||||||
public void solve(){
|
|
||||||
//Setup the variables
|
|
||||||
unique = new ArrayList<BigInteger>();
|
unique = new ArrayList<BigInteger>();
|
||||||
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -67,10 +72,58 @@ public class Problem29 extends Problem{
|
|||||||
|
|
||||||
//Print the results
|
//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());
|
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 porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
unique.clear();
|
||||||
|
}
|
||||||
|
//Returns the lowest possible value for a
|
||||||
|
public int getBottomA(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return BOTTOM_A;
|
||||||
|
}
|
||||||
|
//Returns the highest possible value for a
|
||||||
|
public int getTopA(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return TOP_A;
|
||||||
|
}
|
||||||
|
//Returns the lowest possible value for b
|
||||||
|
public int getBottomB(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return BOTTOM_B;
|
||||||
|
}
|
||||||
|
//Returns the highest possible value for b
|
||||||
|
public int getTopB(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return TOP_B;
|
||||||
|
}
|
||||||
|
//Returns a vector of all the unique values for a^b
|
||||||
|
public ArrayList<BigInteger> getUnique(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return unique;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183
|
The number of unique values generated by a^b for 2 <= a <= 100 and 2 <= b <= 100 is 9183
|
||||||
It took 111.346 milliseconds to solve this problem.
|
It took an average of 118.773 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -23,36 +23,80 @@
|
|||||||
package mattrixwv.ProjectEuler.Problems;
|
package mattrixwv.ProjectEuler.Problems;
|
||||||
|
|
||||||
|
|
||||||
import java.math.BigInteger;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import mattrixwv.Algorithms;
|
import mattrixwv.Algorithms;
|
||||||
|
|
||||||
|
|
||||||
public class Problem3 extends Problem{
|
public class Problem3 extends Problem{
|
||||||
//The number that needs factored
|
//Variables
|
||||||
private static final BigInteger NUMBER = BigInteger.valueOf(600851475143L);
|
//Static variables
|
||||||
|
private static final long GOAL_NUMBER = 600851475143L; //The number that needs factored
|
||||||
|
//Instance variables
|
||||||
|
private ArrayList<Long> factors; //Holds the factors of goalNumber
|
||||||
|
|
||||||
|
//Constructor
|
||||||
public Problem3(){
|
public Problem3(){
|
||||||
super("What is the largest prime factor of 600851475143?");
|
super("What is the largest prime factor of 600851475143?");
|
||||||
|
factors = new ArrayList<Long>();
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
//Get all the factors of the number
|
//Get all the factors of the number
|
||||||
ArrayList<BigInteger> factors = Algorithms.getFactors(NUMBER);
|
factors = Algorithms.getFactors(GOAL_NUMBER);
|
||||||
//The last element should be the largest factor
|
//The last element should be the largest factor
|
||||||
|
|
||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The largest factor of the number %d is %d\n", NUMBER, factors.get(factors.size() - 1));
|
result = String.format("The largest factor of the number %d is %d\n", GOAL_NUMBER, factors.get(factors.size() - 1));
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
factors.clear();
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the list of factors of the number
|
||||||
|
public ArrayList<Long> getFactors(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return factors;
|
||||||
|
}
|
||||||
|
//Returns the largest factor of the number
|
||||||
|
public long getLargestFactor(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return factors.get(factors.size() - 1);
|
||||||
|
}
|
||||||
|
//Returns the number
|
||||||
|
public long getGoalNumber(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return GOAL_NUMBER;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The largest factor of the number 600851475143 is 6857
|
The largest factor of the number 600851475143 is 6857
|
||||||
It took 242.138 milliseconds to solve this problem.
|
It took an average of 48.209 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -25,20 +25,25 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
import mattrixwv.Algorithms;
|
||||||
|
|
||||||
|
|
||||||
public class Problem30 extends Problem{
|
public class Problem30 extends Problem{
|
||||||
//This is the largest number that will be checked
|
//Variables
|
||||||
private static final long TOP_NUM = 1000000L;
|
//Static variables
|
||||||
//Starts with 2 because 0 and 1 don't count
|
private static final long TOP_NUM = 1000000; //This is the largest number that will be checked
|
||||||
private static final long BOTTOM_NUM = 2L;
|
private static final long BOTTOM_NUM = 2; //Starts with 2 because 0 and 1 don't count
|
||||||
//This is the power that the digits are raised to
|
private static final long POWER_RAISED = 5; //This is the power that the digits are raised to
|
||||||
private static final long POWER_RAISED = 5L;
|
//Instance variables
|
||||||
//This is an ArrayList of the numbers that are the sum of the fifth power of their digits
|
private ArrayList<Long> sumOfFifthNumbers; //This is an ArrayList of the numbers that are the sum of the fifth power of their digits
|
||||||
private static ArrayList<Long> sumOfFifthNumbers;
|
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Operational functions
|
||||||
public Problem30(){
|
public Problem30(){
|
||||||
super("Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.");
|
super("Find the sum of all the numbers that can be written as the sum of the fifth powers of their digits.");
|
||||||
|
sumOfFifthNumbers = new ArrayList<Long>();
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
//Returns an ArrayList with the individual digits of the number passed to it
|
//Returns an ArrayList with the individual digits of the number passed to it
|
||||||
private ArrayList<Long> getDigits(long num){
|
private ArrayList<Long> getDigits(long num){
|
||||||
ArrayList<Long> listOfDigits = new ArrayList<Long>(); //This ArrayList holds the individual digits of num
|
ArrayList<Long> listOfDigits = new ArrayList<Long>(); //This ArrayList holds the individual digits of num
|
||||||
@@ -51,18 +56,12 @@ public class Problem30 extends Problem{
|
|||||||
//Return the list of digits
|
//Return the list of digits
|
||||||
return listOfDigits;
|
return listOfDigits;
|
||||||
}
|
}
|
||||||
private long getSumOfList(){
|
//Solve the problem
|
||||||
long sum = 0L; //Start the sum at 0 so you can add to it
|
|
||||||
//Add every number in the ArrayList to the sum
|
|
||||||
for(long num : sumOfFifthNumbers){
|
|
||||||
sum += num;
|
|
||||||
}
|
|
||||||
//Return the sum
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//Sum of the power of the fifth powers of their digits
|
//If the problem has already been solved do nothing and end the function
|
||||||
sumOfFifthNumbers = new ArrayList<Long>();
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -87,11 +86,44 @@ public class Problem30 extends Problem{
|
|||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Print the results
|
//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", getSumOfList());
|
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 porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
sumOfFifthNumbers.clear();
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//This returns the top number to be checked
|
||||||
|
public long getTopNum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return TOP_NUM;
|
||||||
|
}
|
||||||
|
//This returns a copy of the vector holding all the numbers that are the sum of the fifth power of their digits
|
||||||
|
public ArrayList<Long> getListOfSumOfFifths(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return sumOfFifthNumbers;
|
||||||
|
}
|
||||||
|
//This returns the sum of all entries in sumOfFifthNumbers
|
||||||
|
public long getSumOfList(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return Algorithms.getLongSum(sumOfFifthNumbers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839
|
The sum of all the numbers that can be written as the sum of the fifth powers of their digits is 443839
|
||||||
It took 227.085 milliseconds to solve this problem.
|
It took an average of 240.500 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-19-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -23,13 +23,25 @@
|
|||||||
package mattrixwv.ProjectEuler.Problems;
|
package mattrixwv.ProjectEuler.Problems;
|
||||||
|
|
||||||
public class Problem31 extends Problem{
|
public class Problem31 extends Problem{
|
||||||
private static final int desiredValue = 200;
|
//Variables
|
||||||
private int permutations;
|
//Static variables
|
||||||
|
private static final int desiredValue = 200; //The value of coins we want
|
||||||
|
//Instance variables
|
||||||
|
private int permutations; //The number of permutations that are found
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem31(){
|
public Problem31(){
|
||||||
super("How many different ways can 2 pounds be made using any number of coins?");
|
super("How many different ways can 2 pounds be made using any number of coins?");
|
||||||
}
|
|
||||||
public void solve(){
|
|
||||||
permutations = 0;
|
permutations = 0;
|
||||||
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -56,10 +68,26 @@ public class Problem31 extends Problem{
|
|||||||
|
|
||||||
//Save the result
|
//Save the result
|
||||||
result = String.format("There are %d ways to make 2 pounds with the given denominations of coins", permutations);
|
result = String.format("There are %d ways to make 2 pounds with the given denominations of coins", permutations);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
permutations = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the number of correct permutations of the coins
|
||||||
|
public int getPermutations(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return permutations;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
There are 73682 ways to make 2 pounds with the given denominations of coins
|
There are 73682 ways to make 2 pounds with the given denominations of coins
|
||||||
It took 17.899 microseconds to solve this problem.
|
It took an average of 19.175 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -28,20 +28,29 @@ import java.util.Collections;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem4 extends Problem{
|
public class Problem4 extends Problem{
|
||||||
//The first number to be multiplied
|
//Variables
|
||||||
private static final int START_NUM = 100;
|
//Static variables
|
||||||
//The last number to be multiplied
|
private static final int START_NUM = 100; //The first number to be multiplied
|
||||||
private static final int END_NUM = 999;
|
private static final int END_NUM = 999; //The last number to be multiplied
|
||||||
|
//Instance variables
|
||||||
|
private ArrayList<Integer> palindromes; //Holds all numbers that turn out to be palindromes
|
||||||
|
|
||||||
|
//Constructor
|
||||||
public Problem4(){
|
public Problem4(){
|
||||||
super("Find the largest palindrome made from the product of two 3-digit numbers");
|
super("Find the largest palindrome made from the product of two 3-digit numbers");
|
||||||
|
palindromes = new ArrayList<Integer>();
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
ArrayList<Integer> palindromes = new ArrayList<Integer>();
|
|
||||||
|
|
||||||
//Start at the first 3-digit number and check every one up to the last 3-digit number
|
//Start at the first 3-digit number and check every one up to the last 3-digit number
|
||||||
for(int firstNum = START_NUM;firstNum <= END_NUM;++firstNum){
|
for(int firstNum = START_NUM;firstNum <= END_NUM;++firstNum){
|
||||||
//You can start at the location of the first number because everything before that has already been tested. (100 * 101 == 101 * 100)
|
//You can start at the location of the first number because everything before that has already been tested. (100 * 101 == 101 * 100)
|
||||||
@@ -69,10 +78,35 @@ public class Problem4 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The largest palindrome is %d\n", palindromes.get(palindromes.size() - 1));
|
result = String.format("The largest palindrome is %d\n", palindromes.get(palindromes.size() - 1));
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
palindromes.clear();
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the list of all palindromes
|
||||||
|
public ArrayList<Integer> getPalindromes(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return palindromes;
|
||||||
|
}
|
||||||
|
//Returns the largest palindrome
|
||||||
|
public int getLargestPalindrome(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return palindromes.get(palindromes.size() - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The largest palindrome is 906609
|
The largest palindrome is 906609
|
||||||
It took 14.919 milliseconds to solve this problem.
|
It took an average of 16.926 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -24,10 +24,24 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem5 extends Problem{
|
public class Problem5 extends Problem{
|
||||||
|
//Variables
|
||||||
|
//Instance variables
|
||||||
|
private int smallestNum; //The smallest number that is found
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem5(){
|
public Problem5(){
|
||||||
super("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?");
|
super("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?");
|
||||||
|
smallestNum = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
@@ -51,15 +65,34 @@ public class Problem5 extends Problem{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
smallestNum = currentNum;
|
||||||
|
|
||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The smallest positive number evenly divisibly by all number 1-20 is " + currentNum);
|
result = String.format("The smallest positive number evenly divisibly by all number 1-20 is " + currentNum);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
smallestNum = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the requested number
|
||||||
|
public int getNumber(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return smallestNum;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The smallest positive number evenly divisibly by all number 1-20 is 232792560
|
The smallest positive number evenly divisibly by all number 1-20 is 232792560
|
||||||
It took 156.873 milliseconds to solve this problem.
|
It took an average of 280.352 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -24,22 +24,32 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem6 extends Problem{
|
public class Problem6 extends Problem{
|
||||||
//The first number that needs to be counted
|
//Variables
|
||||||
private static final int START_NUM = 1;
|
//Static variables
|
||||||
//The last number that needs to be counted
|
private static final int START_NUM = 1; //The first number that needs to be counted
|
||||||
private static final int END_NUM = 100;
|
private static final int END_NUM = 100; //The last number that needs to be counted
|
||||||
|
//Instance variables
|
||||||
|
private long sumOfSquares; //Holds the sum of the squares of all the numbers
|
||||||
|
private long squareOfSum; //Holds the square of the sum of all the numbers
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//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("Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.");
|
||||||
|
sumOfSquares = 0;
|
||||||
|
squareOfSum = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
//Setup the variables
|
|
||||||
long sumOfSquares = 0L; //Holds the sum of the squares of all the numbers
|
|
||||||
long squareOfSum = 0L; //Holds the square of the sum of all the numbers
|
|
||||||
|
|
||||||
//Run through all numbers and add them to the appropriate sums
|
//Run through all numbers and add them to the appropriate sums
|
||||||
for(int currentNum = START_NUM;currentNum <= END_NUM;++currentNum){
|
for(int currentNum = START_NUM;currentNum <= END_NUM;++currentNum){
|
||||||
sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable
|
sumOfSquares += (currentNum * currentNum); //Add the square to the correct variable
|
||||||
@@ -53,10 +63,44 @@ public class Problem6 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is %d\n", Math.abs(sumOfSquares - squareOfSum));
|
result = String.format("The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is %d\n", Math.abs(sumOfSquares - squareOfSum));
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
squareOfSum = 0;
|
||||||
|
sumOfSquares = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the sum of all the squares
|
||||||
|
public long getSumOfSquares(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return sumOfSquares;
|
||||||
|
}
|
||||||
|
//Returns the square of all of the sums
|
||||||
|
public long getSquareOfSum(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return squareOfSum;
|
||||||
|
}
|
||||||
|
//Returns the requested difference
|
||||||
|
public long getDifference(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return Math.abs(sumOfSquares - squareOfSum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150
|
The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150
|
||||||
It took 3.100 microseconds to solve this problem.
|
It took an average of 2.669 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem67.java
|
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem67.java
|
||||||
//Matthew Ellison
|
//Matthew Ellison
|
||||||
// Created: 03-26-19
|
// Created: 03-26-19
|
||||||
//Modified: 06-18-20
|
//Modified: 07-11-20
|
||||||
//Find the maximum total from top to bottom
|
//Find the maximum total from top to bottom
|
||||||
/*
|
/*
|
||||||
59
|
59
|
||||||
@@ -131,11 +131,7 @@ import java.util.Arrays;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem67 extends Problem{
|
public class Problem67 extends Problem{
|
||||||
//The number of rows in the array
|
//Structures
|
||||||
private static final int NUM_ROWS = 100;
|
|
||||||
//Setup the list you are trying to find a path through
|
|
||||||
private ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>();
|
|
||||||
|
|
||||||
//Used to keep track of where the best location came from
|
//Used to keep track of where the best location came from
|
||||||
private class location{
|
private class location{
|
||||||
public int xLocation;
|
public int xLocation;
|
||||||
@@ -152,9 +148,25 @@ public class Problem67 extends Problem{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Variables
|
||||||
|
//Static variables
|
||||||
|
private static final int NUM_ROWS = 100; //The number of rows in the array
|
||||||
|
//Instance variables
|
||||||
|
private ArrayList<ArrayList<Integer>> list; //Setup the list you are trying to find a path through
|
||||||
|
private ArrayList<location> foundPoints; //For the points that I have already found the shortest distance to
|
||||||
|
private ArrayList<location> possiblePoints; //For the locations you are checking this round
|
||||||
|
private int actualTotal; //The true total of the path from the top to the bottom
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem67(){
|
public Problem67(){
|
||||||
super("Find the maximum total from top to bottom");
|
super("Find the maximum total from top to bottom");
|
||||||
|
list = new ArrayList<ArrayList<Integer>>();
|
||||||
|
foundPoints = new ArrayList<location>();
|
||||||
|
possiblePoints = new ArrayList<location>();
|
||||||
|
actualTotal = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
//This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest
|
//This function turns every number in the array into (100 - num) to allow you to find the largest numbers rather than the smallest
|
||||||
private void invert(ArrayList<ArrayList<Integer>> list){
|
private void invert(ArrayList<ArrayList<Integer>> list){
|
||||||
//Loop through every row in the list
|
//Loop through every row in the list
|
||||||
@@ -170,6 +182,7 @@ public class Problem67 extends Problem{
|
|||||||
private void removeHelper(ArrayList<location> possiblePoints, final location minLoc){
|
private void removeHelper(ArrayList<location> possiblePoints, final location minLoc){
|
||||||
possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation)));
|
possiblePoints.removeIf(loc -> ((loc.xLocation == minLoc.xLocation) && (loc.yLocation == minLoc.yLocation)));
|
||||||
}
|
}
|
||||||
|
//Setup the list of numbers to check
|
||||||
private void setupList(){
|
private void setupList(){
|
||||||
list.ensureCapacity(NUM_ROWS);
|
list.ensureCapacity(NUM_ROWS);
|
||||||
list.add(new ArrayList<Integer>(Arrays.asList(59)));
|
list.add(new ArrayList<Integer>(Arrays.asList(59)));
|
||||||
@@ -273,7 +286,13 @@ public class Problem67 extends Problem{
|
|||||||
list.add(new ArrayList<Integer>(Arrays.asList(30, 11, 85, 31, 34, 71, 13, 48, 05, 14, 44, 03, 19, 67, 23, 73, 19, 57, 06, 90, 94, 72, 57, 69, 81, 62, 59, 68, 88, 57, 55, 69, 49, 13, 07, 87, 97, 80, 89, 05, 71, 05, 05, 26, 38, 40, 16, 62, 45, 99, 18, 38, 98, 24, 21, 26, 62, 74, 69, 04, 85, 57, 77, 35, 58, 67, 91, 79, 79, 57, 86, 28, 66, 34, 72, 51, 76, 78, 36, 95, 63, 90, 8, 78, 47, 63, 45, 31, 22, 70, 52, 48, 79, 94, 15, 77, 61, 67, 68)));
|
list.add(new ArrayList<Integer>(Arrays.asList(30, 11, 85, 31, 34, 71, 13, 48, 05, 14, 44, 03, 19, 67, 23, 73, 19, 57, 06, 90, 94, 72, 57, 69, 81, 62, 59, 68, 88, 57, 55, 69, 49, 13, 07, 87, 97, 80, 89, 05, 71, 05, 05, 26, 38, 40, 16, 62, 45, 99, 18, 38, 98, 24, 21, 26, 62, 74, 69, 04, 85, 57, 77, 35, 58, 67, 91, 79, 79, 57, 86, 28, 66, 34, 72, 51, 76, 78, 36, 95, 63, 90, 8, 78, 47, 63, 45, 31, 22, 70, 52, 48, 79, 94, 15, 77, 61, 67, 68)));
|
||||||
list.add(new ArrayList<Integer>(Arrays.asList(23, 33, 44, 81, 80, 92, 93, 75, 94, 88, 23, 61, 39, 76, 22, 03, 28, 94, 32, 06, 49, 65, 41, 34, 18, 23, 8, 47, 62, 60, 03, 63, 33, 13, 80, 52, 31, 54, 73, 43, 70, 26, 16, 69, 57, 87, 83, 31, 03, 93, 70, 81, 47, 95, 77, 44, 29, 68, 39, 51, 56, 59, 63, 07, 25, 70, 07, 77, 43, 53, 64, 03, 94, 42, 95, 39, 18, 01, 66, 21, 16, 97, 20, 50, 90, 16, 70, 10, 95, 69, 29, 06, 25, 61, 41, 26, 15, 59, 63, 35)));
|
list.add(new ArrayList<Integer>(Arrays.asList(23, 33, 44, 81, 80, 92, 93, 75, 94, 88, 23, 61, 39, 76, 22, 03, 28, 94, 32, 06, 49, 65, 41, 34, 18, 23, 8, 47, 62, 60, 03, 63, 33, 13, 80, 52, 31, 54, 73, 43, 70, 26, 16, 69, 57, 87, 83, 31, 03, 93, 70, 81, 47, 95, 77, 44, 29, 68, 39, 51, 56, 59, 63, 07, 25, 70, 07, 77, 43, 53, 64, 03, 94, 42, 95, 39, 18, 01, 66, 21, 16, 97, 20, 50, 90, 16, 70, 10, 95, 69, 29, 06, 25, 61, 41, 26, 15, 59, 63, 35)));
|
||||||
}
|
}
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
@@ -336,10 +355,58 @@ public class Problem67 extends Problem{
|
|||||||
int actualTotal = ((100 * NUM_ROWS) - foundPoints.get(foundPoints.size() - 1).total);
|
int actualTotal = ((100 * NUM_ROWS) - foundPoints.get(foundPoints.size() - 1).total);
|
||||||
|
|
||||||
result = "The value of the longest path is " + actualTotal;
|
result = "The value of the longest path is " + actualTotal;
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
foundPoints.clear();
|
||||||
|
possiblePoints.clear();
|
||||||
|
actualTotal = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the pyramid that was traversed as a string
|
||||||
|
public String getPyramid(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
StringBuilder results = new StringBuilder();
|
||||||
|
|
||||||
|
//Loop through all elements of the list and print them
|
||||||
|
for(ArrayList<Integer> row : list){
|
||||||
|
for(int column : row){
|
||||||
|
results.append(String.format("%2d ", column));
|
||||||
|
}
|
||||||
|
results.append('\n');
|
||||||
|
}
|
||||||
|
return results.toString();
|
||||||
|
}
|
||||||
|
//Returns the trail the algorithm took as a string
|
||||||
|
public String getTrail(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder results = new StringBuilder();
|
||||||
|
//TODO: Implement this
|
||||||
|
results.append("This has not yet been implemented");
|
||||||
|
return results.toString();
|
||||||
|
}
|
||||||
|
//Returns the total that was asked for
|
||||||
|
public int getTotal(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return actualTotal;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The value of the longest path is 7273
|
The value of the longest path is 7273
|
||||||
It took 175.694 milliseconds to solve this problem.
|
It took an average of 182.482 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -24,34 +24,63 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.math.BigInteger;
|
|
||||||
|
|
||||||
import mattrixwv.Algorithms;
|
import mattrixwv.Algorithms;
|
||||||
|
|
||||||
|
|
||||||
public class Problem7 extends Problem{
|
public class Problem7 extends Problem{
|
||||||
//The number of primes we are trying to get
|
//Variables
|
||||||
private static final BigInteger NUMBER_OF_PRIMES = BigInteger.valueOf(10001);
|
//Static variables
|
||||||
|
private static final long NUMBER_OF_PRIMES = 10001; //The number of primes we are trying to get
|
||||||
|
//Instance variables
|
||||||
|
private ArrayList<Long> primes;
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem7(){
|
public Problem7(){
|
||||||
super("What is the 10001th prime number?");
|
super("What is the 10001th prime number?");
|
||||||
|
primes = new ArrayList<Long>();
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
|
//If the problem has already been solved do nothing and end the function
|
||||||
|
if(solved){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
|
|
||||||
//Setup the variables
|
//Setup the variables
|
||||||
ArrayList<BigInteger> primes = Algorithms.getNumPrimes(NUMBER_OF_PRIMES); //Holds the prime numbers
|
primes = Algorithms.getNumPrimes(NUMBER_OF_PRIMES); //Holds the prime numbers
|
||||||
|
|
||||||
//Stop the timer
|
//Stop the timer
|
||||||
timer.stop();
|
timer.stop();
|
||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The " + NUMBER_OF_PRIMES.toString() + "th prime number is " + primes.get(primes.size() - 1));
|
result = String.format("The " + NUMBER_OF_PRIMES + "th prime number is " + primes.get(primes.size() - 1));
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
primes.clear();
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the requested prime number
|
||||||
|
public long getPrime(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return primes.get(primes.size() - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The 10001th prime number is 104743
|
The 10001th prime number is 104743
|
||||||
It took 43.496 milliseconds to solve this problem.
|
It took an average of 27.343 milliseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
@@ -46,16 +46,28 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem8 extends Problem{
|
public class Problem8 extends Problem{
|
||||||
|
//Variables
|
||||||
|
//Static variables
|
||||||
//The 1000 digit number to check
|
//The 1000 digit number to check
|
||||||
private static final String NUMBER = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
|
private static final String NUMBER = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
|
||||||
|
//Instance variables
|
||||||
|
private String maxNums; //Holds the string of the largest product
|
||||||
|
private long maxProduct; //Holds the largest product of 13 numbers
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem8(){
|
public Problem8(){
|
||||||
super("Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?");
|
super("Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?");
|
||||||
|
maxNums = "";
|
||||||
|
maxProduct = 0;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//Setup the variables
|
//If the problem has already been solved do nothing and end the function
|
||||||
String maxNums = new String(); //Holds the string of the largest product
|
if(solved){
|
||||||
long maxProduct = 0L; //Holds the largest product of 13 numbers
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -76,11 +88,37 @@ public class Problem8 extends Problem{
|
|||||||
|
|
||||||
//Save the results
|
//Save the results
|
||||||
result = String.format("The greatest product is " + maxProduct + "\nThe numbers are " + maxNums);
|
result = String.format("The greatest product is " + maxProduct + "\nThe numbers are " + maxNums);
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
maxNums = "";
|
||||||
|
maxProduct = 0;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the string of numbers that produces the largest product
|
||||||
|
public String getLargestNums(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return maxNums;
|
||||||
|
}
|
||||||
|
//Returns the requested product
|
||||||
|
public long getLargestProduct(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return maxProduct;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The greatest product is 23514624000
|
The greatest product is 23514624000
|
||||||
The numbers are 5576689664895
|
The numbers are 5576689664895
|
||||||
It took 806.200 microseconds to solve this problem.
|
It took an average of 924.880 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -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: 06-17-20
|
//Modified: 07-10-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
|
||||||
/*
|
/*
|
||||||
@@ -24,15 +24,29 @@ package mattrixwv.ProjectEuler.Problems;
|
|||||||
|
|
||||||
|
|
||||||
public class Problem9 extends Problem{
|
public class Problem9 extends Problem{
|
||||||
|
//Variables
|
||||||
|
//Instance variables
|
||||||
|
private int a; //Holds the size of the first side
|
||||||
|
private int b; //Holds the size of the second side
|
||||||
|
private double c; //Holds the size of the hyp
|
||||||
|
private boolean found; //A flag to determine if we have found the solution yet
|
||||||
|
|
||||||
|
//Functions
|
||||||
|
//Constructor
|
||||||
public Problem9(){
|
public Problem9(){
|
||||||
super("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.");
|
super("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.");
|
||||||
|
a = 1;
|
||||||
|
b = 0;
|
||||||
|
c = 0;
|
||||||
|
found = false;
|
||||||
}
|
}
|
||||||
|
//Operational functions
|
||||||
|
//Solve the problem
|
||||||
public void solve(){
|
public void solve(){
|
||||||
//Setup the variables
|
//If the problem has already been solved do nothing and end the function
|
||||||
int a = 1; //Holds the length of the first side
|
if(solved){
|
||||||
int b = 0; //Holds the length of the second side
|
return;
|
||||||
double c = 0D; //Holds the length of the hyp
|
}
|
||||||
boolean found = false; //A flag to show whether the solution has been found
|
|
||||||
|
|
||||||
//Start the timer
|
//Start the timer
|
||||||
timer.start();
|
timer.start();
|
||||||
@@ -67,11 +81,55 @@ public class Problem9 extends Problem{
|
|||||||
else{
|
else{
|
||||||
result = "The number was not found!";
|
result = "The number was not found!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Throw a flag to show the porblem is solved
|
||||||
|
solved = true;
|
||||||
|
}
|
||||||
|
//Reset the problem so it can be run again
|
||||||
|
public void reset(){
|
||||||
|
super.reset();
|
||||||
|
a = 1;
|
||||||
|
b = 0;
|
||||||
|
c = 0;
|
||||||
|
found = false;
|
||||||
|
}
|
||||||
|
//Gets
|
||||||
|
//Returns the length of the first side
|
||||||
|
public int getSideA(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
//Returns the length of the second side
|
||||||
|
public int getSideB(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
//Returns the length of the hyp
|
||||||
|
public int getSideC(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return (int)c;
|
||||||
|
}
|
||||||
|
//Returns the product of the 3 sides
|
||||||
|
public int getProduct(){
|
||||||
|
//If the problem hasn't been solved throw an exception
|
||||||
|
if(!solved){
|
||||||
|
throw new Unsolved();
|
||||||
|
}
|
||||||
|
return a * b * (int)c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Results:
|
/* Results:
|
||||||
The Pythagorean triplet is 200 + 375 + 425
|
The Pythagorean triplet is 200 + 375 + 425
|
||||||
The numbers' product is 31875000
|
The numbers' product is 31875000
|
||||||
It took 139.599 microseconds to solve this problem.
|
It took an average of 380.920 microseconds to run this problem through 100 iterations
|
||||||
*/
|
*/
|
||||||
|
|||||||
12
src/main/java/mattrixwv/ProjectEuler/Problems/Unsolved.java
Normal file
12
src/main/java/mattrixwv/ProjectEuler/Problems/Unsolved.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package mattrixwv.ProjectEuler.Problems;
|
||||||
|
|
||||||
|
public class Unsolved extends RuntimeException{
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
public Unsolved(){
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
public Unsolved(String exceptionString){
|
||||||
|
super(exceptionString);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user