Moved Unsolved and fixed a few typos

This commit is contained in:
2020-07-19 13:23:11 -04:00
parent 4a745be0e1
commit 4cdbed958f
35 changed files with 193 additions and 111 deletions

View File

@@ -22,6 +22,7 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Stopwatch;
import mattrixwv.ProjectEuler.Unsolved;
public abstract class Problem{
//Variables
@@ -43,14 +44,23 @@ public abstract class Problem{
}
//Returns the result of solving the problem
public String getResult(){
if(!solved){
throw new Unsolved();
}
return result;
}
//Returns the time taken to run the problem as a string
public String getTime(){
if(!solved){
throw new Unsolved();
}
return timer.getStr();
}
//Returns the timer as a stopwatch
public Stopwatch getTimer(){
if(!solved){
throw new Unsolved();
}
return timer;
}
//Solve the problem

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem1.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 07-10-20
//Modified: 07-17-20
//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
/*
@@ -23,12 +23,15 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem1 extends Problem{
//Variables
//Static variables
private static final int TOP_NUM = 999; //The largest number to be checked
//Instance variables
private int fullSum; //For the sum of all the numbers
private int fullSum; //The sum of all the numbers
//Functions
//Constructor
@@ -60,7 +63,7 @@ public class Problem1 extends Problem{
//Stop the timer
timer.stop();
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
//Save the results

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem10.java
//Matthew Ellison
// Created: 03-03-19
//Modified: 07-10-20
//Modified: 07-18-20
//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
/*
@@ -24,6 +24,7 @@ package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem10 extends Problem{
@@ -59,7 +60,7 @@ public class Problem10 extends Problem{
//Save the results
result = String.format("The sum of all the primes < %d is %d\n", GOAL_NUMBER + 1, sum);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem11.java
//Matthew Ellison
// Created: 03-03-19
//Modified: 07-10-20
//Modified: 07-18-20
//What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
/*
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
@@ -48,6 +48,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem11 extends Problem{
@@ -196,7 +197,7 @@ public class Problem11 extends Problem{
//Save the resutls
result = String.format("The greatest product of 4 numbers in a line is %d\nThe numbers are %s", Algorithms.getProd(greatestProduct), greatestProduct.toString());
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-10-20
//Modified: 07-18-20
//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
/*
@@ -26,6 +26,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem12 extends Problem{
@@ -79,7 +80,7 @@ public class Problem12 extends Problem{
//Save the results
result = String.format("The triangular number %d is the sum of all numbers >= %d and has %d divisors\n", sum, counter - 1, divisors.size());
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem13.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-10-20
//Modified: 07-18-20
//Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
/*
37107287533902102798797998220837590246510135740250
@@ -129,6 +129,7 @@ import java.math.BigInteger;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem13 extends Problem{
@@ -276,7 +277,7 @@ public class Problem13 extends Problem{
//Save the results
result = String.format("The sum of all %d numbers is %d\nThe first 10 digits of the sum of the numbers is %s\n", nums.size(), sum, (sum.toString()).substring(0, 10));
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem14.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-10-20
//Modified: 07-18-20
/*
The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
@@ -28,6 +28,9 @@ Which starting number, under one million, produces the longest chain?
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem14 extends Problem{
//Variables
//Static variables
@@ -54,7 +57,7 @@ public class Problem14 extends Problem{
//Start the timer
timer.start();
//Loop through all numbers less than 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){
long currentLength = checkSeries(currentNum);
//If the current number has a longer series than the max then the current becomes the max
@@ -70,7 +73,7 @@ public class Problem14 extends Problem{
//Save the results
result = String.format("The number %d produced a chain of %d steps\n", maxNum, maxLength);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//This function follows the rules of the sequence and returns its length

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem15.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-10-20
//Modified: 07-18-20
//How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
@@ -22,6 +22,7 @@
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem15 extends Problem{
//Variables
@@ -45,16 +46,12 @@ public class Problem15 extends Problem{
return;
}
//Setup the rest of the variables
int currentX = 0; //The current x location on the grid
int currentY = 0; //The current y location on the grid
//Start the timer
timer.start();
//We write this as a recursive function
//When in a location it always moves right first, then down
move(currentX, currentY);
move(0, 0);
//Stop the timer
timer.stop();
@@ -62,7 +59,7 @@ public class Problem15 extends Problem{
//Save the results
result = String.format("The number of routes is " + numOfRoutes);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//This function acts as a handler for moving the position on the grid and counting the distance

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem16.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-10-20
//Modified: 07-18-20
//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
/*
@@ -25,6 +25,8 @@ package mattrixwv.ProjectEuler.Problems;
import java.math.BigInteger;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem16 extends Problem{
//Variables
@@ -68,7 +70,7 @@ public class Problem16 extends Problem{
//Save the results
result = String.format("%d^%d = %s\nThe sum of the elements is %d\n", NUM_TO_POWER, POWER, num.toString(), sumOfElements);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem16.java
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem17.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-10-20
//Modified: 07-18-20
//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
/*
@@ -23,6 +23,9 @@
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem17 extends Problem{
//Variables
//Static variables
@@ -62,7 +65,7 @@ public class Problem17 extends Problem{
//Save the results
result = String.format("The sum of all the letters in all the numbers %d-%d is %d\n", START_NUM, STOP_NUM, letterCount);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java
//Matthew Ellison
// Created: 03-11-19
//Modified: 07-10-20
//Modified: 07-18-20
//Find the maximum total from top to bottom
/*
75
@@ -44,6 +44,8 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import java.util.Arrays;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem18 extends Problem{
//Structures
@@ -123,12 +125,12 @@ public class Problem18 extends Problem{
return;
}
//Start the timer
timer.start();
//Setup the list you are trying to find a path through
setupList();
//Start the timer
timer.start();
//Invert the list
invert(list);
@@ -180,7 +182,7 @@ public class Problem18 extends Problem{
result = String.format("The value of the longest path is " + actualTotal);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again
@@ -197,8 +199,8 @@ public class Problem18 extends Problem{
if(!solved){
throw new Unsolved();
}
StringBuilder results = new StringBuilder();
StringBuilder results = new StringBuilder();
//Loop through all elements of the list and print them
for(ArrayList<Integer> row : list){
for(int column : row){

View File

@@ -33,6 +33,7 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem19 extends Problem{
//Variables
@@ -42,7 +43,7 @@ public class Problem19 extends Problem{
private static final int START_YEAR = 1901; //The first year we are going to test
private static final int END_YEAR = 2000; //The last year we are going to test
//Instance variables
private long totalSundays;
private long totalSundays; //Keep track of the number of sundays
//Functions
//Constructor
@@ -81,7 +82,7 @@ public class Problem19 extends Problem{
//Save the results
result = String.format("There are %d Sundays that landed on the first of the months from %d to %d\n", totalSundays, START_YEAR, END_YEAR);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Return the day of the week that the date you pass into it is on

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem2.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 07-10-20
//Modified: 07-17-20
//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
/*
@@ -26,6 +26,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem2 extends Problem{
@@ -68,7 +69,7 @@ public class Problem2 extends Problem{
//Save the results
result = String.format("The sum of all even fibonacci numbers <= %d is %d\n", TOP_NUM, fullSum);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem20.java
//Matthew Ellison
// Created: 03-14-19
//Modified: 07-10-20
//Modified: 07-18-20
//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
/*
@@ -25,6 +25,8 @@ package mattrixwv.ProjectEuler.Problems;
import java.math.BigInteger;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem20 extends Problem{
//Variables
@@ -71,10 +73,10 @@ public class Problem20 extends Problem{
//Save the results
result = String.format("%d! = %s\nThe sum of the digits is: %d\n", TOP_NUM, num.toString(), sum);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the proble so it can be run again
//Reset the problem so it can be run again
public void reset(){
super.reset();
num = BigInteger.ONE;

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem21.java
//Matthew Ellison
// Created: 03-18-19
//Modified: 07-10-20
//Modified: 07-19-20
//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
/*
@@ -24,6 +24,7 @@ package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import java.util.ArrayList;
import java.util.Collections;
@@ -90,12 +91,12 @@ public class Problem21 extends Problem{
}
}
//Stop the timer
timer.stop();
//Sort the arraylist for neatness
Collections.sort(amicable);
//Stop the timer
timer.stop();
//Save the results
result = String.format("All amicable numbers less than %d are\n", LIMIT);
for(int cnt = 0;cnt < amicable.size();++cnt){
@@ -103,7 +104,7 @@ public class Problem21 extends Problem{
}
result += String.format("The sum of all of these amicable numbers is %d\n", Algorithms.getSum(amicable));
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem22.java
//Matthew Ellison
// Created: 03-20-19
//Modified: 07-10-20
//Modified: 07-19-20
//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
/*
@@ -24,6 +24,8 @@ package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -448,7 +450,7 @@ public class Problem22 extends Problem{
//Save the results
result = String.format("The answer to the question is %d\n", sum);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem23.java
//Matthew Ellison
// Created: 03-22-19
//Modified: 07-10-20
//Modified: 07-19-20
//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
/*
@@ -24,6 +24,7 @@ package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import java.util.ArrayList;
@@ -94,7 +95,7 @@ public class Problem23 extends Problem{
//Save the results
result = String.format("The answer is %d\n", sum);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//A function that returns true if num can be created by adding two elements from abund and false if it cannot
@@ -116,6 +117,14 @@ public class Problem23 extends Problem{
//If you have run through the entire list and did not find a sum then it is false
return false;
}
//Reset the problem so it can be run again
public void reset(){
super.reset();
divisorSums.clear();
reserveArray();
sum = 0;
}
//Gets
//Returns the sum of the numbers asked for
public long getSum(){
//If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem24.java
//Matthew Ellison
// Created: 03-24-19
//Modified: 07-10-20
//Modified: 07-19-20
//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
/*
@@ -24,6 +24,7 @@ package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import java.util.ArrayList;
@@ -62,7 +63,7 @@ public class Problem24 extends Problem{
//Save the results
result = String.format("The 1 millionth permutation is %s\n", permutations.get(NEEDED_PERM - 1));
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again
@@ -71,7 +72,7 @@ public class Problem24 extends Problem{
permutations.clear();
}
//Gets
//Returns a vector with all of the permutations
//Returns an ArrayList with all of the permutations
public ArrayList<String> getPermutationsList(){
//If the problem hasn't been solved throw an exception
if(!solved){
@@ -79,8 +80,8 @@ public class Problem24 extends Problem{
}
return permutations;
}
//Returns the specific permutations you are looking for
public String getPermutations(){
//Returns the requested permutation
public String getPermutation(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem25.java
//Matthew Ellison
// Created: 03-25-19
//Modified: 07-10-20
//Modified: 07-19-20
//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
/*
@@ -24,6 +24,7 @@ package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import java.math.BigInteger;
@@ -66,7 +67,7 @@ public class Problem25 extends Problem{
//Save the results
result = String.format("The first Fibonacci number with %d digits is %s\nIts index is %d\n", NUM_DIGITS, number.toString(), index);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem26.java
//Matthew Ellison
// Created: 07-28-19
//Modified: 07-10-20
//Modified: 07-19-20
//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
/*
@@ -24,6 +24,7 @@ package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import java.util.ArrayList;
@@ -98,7 +99,7 @@ public class Problem26 extends Problem{
//Save the results
result = String.format("The longest cycle is %d digits long\nIt started with the number %d\n", longestCycle, longestNumber);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem27.java
//Matthew Ellison
// Created: 09-15-19
//Modified: 07-10-20
//Modified: 07-19-20
//Find the product of the coefficients, |a| < 1000 and |b| <= 1000, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n=0.
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
@@ -24,6 +24,7 @@ package mattrixwv.ProjectEuler.Problems;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
import java.util.ArrayList;
@@ -87,7 +88,7 @@ public class Problem27 extends Problem{
//Save the restuls
result = String.format("The greatest number of primes found is %d\nIt was found with A = %d, B = %d\nThe product of A and B is %d\n", topN, topA, topB, topA * topB);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem28.java
//Matthew Ellison
// Created: 09-22-19
//Modified: 07-10-20
//Modified: 07-19-20
//What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/*
@@ -25,6 +25,8 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem28 extends Problem{
//Variables
@@ -134,7 +136,7 @@ public class Problem28 extends Problem{
//Print the restuls
result = String.format("The sum of the diagonals in the given grid is %d\n", sumOfDiagonals);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem29.java
//Matthew Ellison
// Created: 10-09-19
//Modified: 07-10-20
//Modified: 07-19-20
//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
/*
@@ -24,6 +24,9 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.ProjectEuler.Unsolved;
import java.math.BigInteger;
@@ -73,7 +76,7 @@ public class Problem29 extends Problem{
//Print the results
result = String.format("The number of unique values generated by a^b for %d <= a <= %d and %d <= b <= %d is %d\n", BOTTOM_A, TOP_A, BOTTOM_B, TOP_B, unique.size());
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again
@@ -81,6 +84,7 @@ public class Problem29 extends Problem{
super.reset();
unique.clear();
}
//Gets
//Returns the lowest possible value for a
public int getBottomA(){
//If the problem hasn't been solved throw an exception

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem3.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 07-10-20
//Modified: 07-17-20
//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
/*
@@ -26,6 +26,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem3 extends Problem{
@@ -61,7 +62,7 @@ public class Problem3 extends Problem{
//Save the results
result = String.format("The largest factor of the number %d is %d\n", GOAL_NUMBER, factors.get(factors.size() - 1));
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again
@@ -86,12 +87,8 @@ public class Problem3 extends Problem{
}
return factors.get(factors.size() - 1);
}
//Returns the number
public long getGoalNumber(){
//If the problem hasn't been solved throw an exception
if(!solved){
throw new Unsolved();
}
//Returns the number for which we are getting the factor
public static long getGoalNumber(){
return GOAL_NUMBER;
}
}

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem30.java
//Matthew Ellison
// Created: 10-27-19
//Modified: 07-10-20
//Modified: 07-19-20
//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
/*
@@ -26,6 +26,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem30 extends Problem{
@@ -88,7 +89,7 @@ public class Problem30 extends Problem{
//Print the results
result = String.format("The sum of all the numbers that can be written as the sum of the fifth powers of their digits is %d\n", Algorithms.getLongSum(sumOfFifthNumbers));
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem31.java
//Matthew Ellison
// Created: 06-19-20
//Modified: 07-10-20
//Modified: 07-1920
//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
/*
@@ -22,6 +22,8 @@
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem31 extends Problem{
//Variables
//Static variables
@@ -69,9 +71,10 @@ public class Problem31 extends Problem{
//Save the result
result = String.format("There are %d ways to make 2 pounds with the given denominations of coins", permutations);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}//Reset the problem so it can be run again
}
//Reset the problem so it can be run again
public void reset(){
super.reset();
permutations = 0;

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem4.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 07-10-20
//Modified: 07-17-20
//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
/*
@@ -26,6 +26,8 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import java.util.Collections;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem4 extends Problem{
//Variables
@@ -70,16 +72,16 @@ public class Problem4 extends Problem{
}
}
//Stop the timer
timer.stop();
//Sort the palindromes so that the last one is the largest
Collections.sort(palindromes);
//Stop the timer
timer.stop();
//Save the results
result = String.format("The largest palindrome is %d\n", palindromes.get(palindromes.size() - 1));
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem5.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 07-10-20
//Modified: 07-17-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
/*
@@ -22,6 +22,7 @@
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem5 extends Problem{
//Variables
@@ -46,8 +47,8 @@ public class Problem5 extends Problem{
timer.start();
//Start at 20 because it must at least be divisible by 20. Increment by 2 because it must be an even number to be divisible by 2
boolean numFound = false;
int currentNum = 20;
boolean numFound = false; //A flag for finding the divisible number
int currentNum = 20; //The number that it are currently checking against
while((currentNum > 0) && (!numFound)){
//Start by assuming you found the number (because we throw a flag if we didn't find it)
numFound = true;
@@ -73,7 +74,7 @@ public class Problem5 extends Problem{
//Save the results
result = String.format("The smallest positive number evenly divisibly by all number 1-20 is " + currentNum);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem6.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 07-10-20
//Modified: 07-18-20
//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
/*
@@ -22,6 +22,7 @@
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem6 extends Problem{
//Variables
@@ -64,7 +65,7 @@ public class Problem6 extends Problem{
//Save the results
result = String.format("The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is %d\n", Math.abs(sumOfSquares - squareOfSum));
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem67.java
//Matthew Ellison
// Created: 03-26-19
//Modified: 07-11-20
//Modified: 07-196-20
//Find the maximum total from top to bottom
/*
59
@@ -129,6 +129,8 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import java.util.Arrays;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem67 extends Problem{
//Structures
@@ -293,12 +295,12 @@ public class Problem67 extends Problem{
return;
}
//Start the timer
timer.start();
//Setup the list you are trying to find a path through
setupList();
//Start the timer
timer.start();
//Invert the list
invert(list);
@@ -356,7 +358,7 @@ public class Problem67 extends Problem{
result = "The value of the longest path is " + actualTotal;
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again
@@ -373,8 +375,8 @@ public class Problem67 extends Problem{
if(!solved){
throw new Unsolved();
}
StringBuilder results = new StringBuilder();
StringBuilder results = new StringBuilder();
//Loop through all elements of the list and print them
for(ArrayList<Integer> row : list){
for(int column : row){

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem7.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 07-10-20
//Modified: 07-18-20
//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
/*
@@ -26,6 +26,7 @@ package mattrixwv.ProjectEuler.Problems;
import java.util.ArrayList;
import mattrixwv.Algorithms;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem7 extends Problem{
@@ -61,7 +62,7 @@ public class Problem7 extends Problem{
//Save the results
result = String.format("The " + NUMBER_OF_PRIMES + "th prime number is " + primes.get(primes.size() - 1));
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem8.java
//Matthew Ellison
// Created: 03-28-19
//Modified: 07-10-20
//Modified: 07-18-20
//Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
/*
73167176531330624919225119674426574742355349194934
@@ -44,6 +44,7 @@
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem8 extends Problem{
//Variables
@@ -89,7 +90,7 @@ public class Problem8 extends Problem{
//Save the results
result = String.format("The greatest product is " + maxProduct + "\nThe numbers are " + maxNums);
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem9.java
//Matthew Ellison
// Created: 03-02-19
//Modified: 07-10-20
//Modified: 07-18-20
//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
/*
@@ -22,6 +22,7 @@
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem9 extends Problem{
//Variables
@@ -82,7 +83,7 @@ public class Problem9 extends Problem{
result = "The number was not found!";
}
//Throw a flag to show the porblem is solved
//Throw a flag to show the problem is solved
solved = true;
}
//Reset the problem so it can be run again

View File

@@ -1,12 +0,0 @@
package mattrixwv.ProjectEuler.Problems;
public class Unsolved extends RuntimeException{
private static final long serialVersionUID = 1L;
public Unsolved(){
super();
}
public Unsolved(String exceptionString){
super(exceptionString);
}
}

View File

@@ -0,0 +1,34 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Unsolved.java
//Matthew Ellison
// Created: 07-17-20
//Modified: 07-17-20
//This is an exception that is thrown by the problem classes
/*
Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mattrixwv.ProjectEuler;
public class Unsolved extends RuntimeException{
private static final long serialVersionUID = 1L;
public Unsolved(){
super();
}
public Unsolved(String exceptionString){
super(exceptionString);
}
}