Updated to work with new library layout

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

View File

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

View File

@@ -1,11 +1,11 @@
//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: 10-26-20 //Modified: 07-03-21
//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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -23,9 +23,6 @@
package mattrixwv.ProjectEuler.Problems; package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem1 extends Problem{ public class Problem1 extends Problem{
//Variables //Variables
//Static variables //Static variables
@@ -51,9 +48,11 @@ public class Problem1 extends Problem{
//Start the timer //Start the timer
timer.start(); timer.start();
//Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap //Get the sum of the progressions of 3 and 5 and remove the sum of progressions of the overlap
fullSum = sumOfProgression(3) + sumOfProgression(5) - sumOfProgression(3 * 5); fullSum = sumOfProgression(3) + sumOfProgression(5) - sumOfProgression(3 * 5);
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -76,21 +75,17 @@ public class Problem1 extends Problem{
//Returns the result of solving the problem //Returns the result of solving the problem
@Override @Override
public String getResult(){ public String getResult(){
if(!solved){ solvedCheck("result");
throw new Unsolved();
}
return String.format("The sum of all numbers < %d is %d", (TOP_NUM + 1), fullSum); return String.format("The sum of all numbers < %d is %d", (TOP_NUM + 1), fullSum);
} }
//Returns the requested sum //Returns the requested sum
public int getSum(){ public int getSum(){
//If the problem hasn't been solved throw an exception solvedCheck("sum");
if(!solved){
throw new Unsolved();
}
return fullSum; return fullSum;
} }
} }
/* Results: /* Results:
The sum of all numbers < 1000 is 233168 The sum of all numbers < 1000 is 233168
It took an average of 298.000 nanoseconds to run this problem through 100 iterations It took an average of 298.000 nanoseconds to run this problem through 100 iterations

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,11 +1,11 @@
//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: 08-27-20 //Modified: 07-03-21
//What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral //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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -25,8 +25,6 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList; import java.util.ArrayList;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem28 extends Problem{ public class Problem28 extends Problem{
//Variables //Variables
@@ -126,11 +124,13 @@ public class Problem28 extends Problem{
//Start the timer //Start the timer
timer.start(); timer.start();
//Setup the grid //Setup the grid
setupGrid(); setupGrid();
//Find the sum of the diagonals in the grid //Find the sum of the diagonals in the grid
findSum(); findSum();
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -147,30 +147,23 @@ public class Problem28 extends Problem{
//Returns the result of solving the problem //Returns the result of solving the problem
@Override @Override
public String getResult(){ public String getResult(){
if(!solved){ solvedCheck("result");
throw new Unsolved();
}
return String.format("The sum of the diagonals in the given grid is %d", sumOfDiagonals); return String.format("The sum of the diagonals in the given grid is %d", sumOfDiagonals);
} }
//Returns the grid //Returns the grid
public ArrayList<ArrayList<Integer>> getGrid(){ public ArrayList<ArrayList<Integer>> getGrid(){
//If the problem hasn't been solved throw an exception solvedCheck("grid");
if(!solved){
throw new Unsolved();
}
return grid; return grid;
} }
//Returns the sum of the diagonals //Returns the sum of the diagonals
public int getSum(){ public int getSum(){
//If the problem hasn't been solved throw an exception solvedCheck("sum");
if(!solved){
throw new Unsolved();
}
return sumOfDiagonals; 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 an average of 17.307 milliseconds to run this problem through 100 iterations It took an average of 17.307 milliseconds to run this problem through 100 iterations

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,11 +1,11 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem4.java //ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem4.java
//Matthew Ellison //Matthew Ellison
// Created: 03-01-19 // Created: 03-01-19
//Modified: 08-27-20 //Modified: 07-03-21
//Find the largest palindrome made from the product of two 3-digit numbers //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
/* /*
Copyright (C) 2020 Matthew Ellison Copyright (C) 2021 Matthew Ellison
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by it under the terms of the GNU Lesser General Public License as published by
@@ -26,8 +26,6 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem4 extends Problem{ public class Problem4 extends Problem{
//Variables //Variables
@@ -54,6 +52,7 @@ public class Problem4 extends Problem{
//Start the timer //Start the timer
timer.start(); timer.start();
//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)
@@ -76,6 +75,7 @@ public class Problem4 extends Problem{
//Sort the palindromes so that the last one is the largest //Sort the palindromes so that the last one is the largest
Collections.sort(palindromes); Collections.sort(palindromes);
//Stop the timer //Stop the timer
timer.stop(); timer.stop();
@@ -92,29 +92,22 @@ public class Problem4 extends Problem{
//Returns the result of solving the problem //Returns the result of solving the problem
@Override @Override
public String getResult(){ public String getResult(){
if(!solved){ solvedCheck("result");
throw new Unsolved();
}
return String.format("The largest palindrome is %d", palindromes.get(palindromes.size() - 1)); return String.format("The largest palindrome is %d", palindromes.get(palindromes.size() - 1));
} }
//Returns the list of all palindromes //Returns the list of all palindromes
public ArrayList<Integer> getPalindromes(){ public ArrayList<Integer> getPalindromes(){
//If the problem hasn't been solved throw an exception solvedCheck("palindromes");
if(!solved){
throw new Unsolved();
}
return palindromes; return palindromes;
} }
//Returns the largest palindrome //Returns the largest palindrome
public int getLargestPalindrome(){ public int getLargestPalindrome(){
//If the problem hasn't been solved throw an exception solvedCheck("largest palindrome");
if(!solved){
throw new Unsolved();
}
return palindromes.get(palindromes.size() - 1); return palindromes.get(palindromes.size() - 1);
} }
} }
/* Results: /* Results:
The largest palindrome is 906609 The largest palindrome is 906609
It took an average of 16.926 milliseconds to run this problem through 100 iterations It took an average of 16.926 milliseconds to run this problem through 100 iterations

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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