Updated problem's flow to match the other languages

This commit is contained in:
2020-08-26 19:40:18 -04:00
parent 1242935cc1
commit 544efc7e29
12 changed files with 82 additions and 128 deletions

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem1.java
//Matthew Ellison
// Created: 03-01-19
//Modified: 07-17-20
//Modified: 08-23-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
/*
@@ -67,7 +67,7 @@ public class Problem1 extends Problem{
solved = true;
//Save the results
result = "The sum of all numbers < " + (TOP_NUM + 1) + " is " + fullSum;
result = String.format("The sum of all numbers < %d is %d", (TOP_NUM + 1), fullSum);
}
//Reset the problem so it can be run again
public void reset(){

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem10.java
//Matthew Ellison
// Created: 03-03-19
//Modified: 07-18-20
//Modified: 08-26-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
/*
@@ -57,11 +57,11 @@ public class Problem10 extends Problem{
//Stop the timer
timer.stop();
//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 problem is solved
solved = true;
//Save the results
result = String.format("The sum of all the primes < %d is %d\n", GOAL_NUMBER + 1, sum);
}
//Reset the problem so it can be run again
public void reset(){

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem11.java
//Matthew Ellison
// Created: 03-03-19
//Modified: 07-18-20
//Modified: 08-26-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
@@ -194,11 +194,11 @@ public class Problem11 extends Problem{
//Stop the timer
timer.stop();
//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 problem is solved
solved = true;
//Save the resutls
result = String.format("The greatest product of 4 numbers in a line is %d\nThe numbers are %s", Algorithms.getProd(greatestProduct), greatestProduct.toString());
}
//Reset the problem so it can be run again
public void reset(){

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem12.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-18-20
//Modified: 08-26-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
/*
@@ -77,11 +77,11 @@ public class Problem12 extends Problem{
//Stop the timer
timer.stop();
//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 problem is solved
solved = true;
//Save the results
result = String.format("The triangular number %d is the sum of all numbers >= %d and has %d divisors\n", sum, counter - 1, divisors.size());
}
//Reset the problem so it can be run again
public void reset(){

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem13.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-18-20
//Modified: 08-26-20
//Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
/*
37107287533902102798797998220837590246510135740250
@@ -274,11 +274,11 @@ public class Problem13 extends Problem{
//Stop the timer
timer.stop();
//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 problem is solved
solved = true;
//Save the results
result = String.format("The sum of all %d numbers is %d\nThe first 10 digits of the sum of the numbers is %s\n", nums.size(), sum, (sum.toString()).substring(0, 10));
}
//Reset the problem so it can be run again
public void reset(){

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem14.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-18-20
//Modified: 08-26-20
/*
The following iterative sequence is defined for the set of positive integers:
n → n/2 (n is even)
@@ -70,11 +70,11 @@ public class Problem14 extends Problem{
//Stop the timer
timer.stop();
//Save the results
result = String.format("The number %d produced a chain of %d steps\n", maxNum, maxLength);
//Throw a flag to show the problem is solved
solved = true;
//Save the results
result = String.format("The number %d produced a chain of %d steps\n", maxNum, maxLength);
}
//This function follows the rules of the sequence and returns its length
private long checkSeries(long num){

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem15.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-18-20
//Modified: 08-26-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,8 +22,10 @@
*/
package mattrixwv.ProjectEuler.Problems;
import mattrixwv.ProjectEuler.Unsolved;
public class Problem15 extends Problem{
//Variables
//Static vaiables
@@ -56,11 +58,11 @@ public class Problem15 extends Problem{
//Stop the timer
timer.stop();
//Save the results
result = String.format("The number of routes is " + numOfRoutes);
//Throw a flag to show the problem is solved
solved = true;
//Save the results
result = String.format("The number of routes is " + numOfRoutes);
}
//This function acts as a handler for moving the position on the grid and counting the distance
//It moves right first, then down

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem16.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-18-20
//Modified: 08-26-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
/*
@@ -67,11 +67,11 @@ public class Problem16 extends Problem{
//Stop the timer
timer.stop();
//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 problem is solved
solved = true;
//Save the results
result = String.format("%d^%d = %s\nThe sum of the elements is %d\n", NUM_TO_POWER, POWER, num.toString(), sumOfElements);
}
//Reset the problem so it can be run again
public void reset(){

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem17.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 07-18-20
//Modified: 08-26-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
/*
@@ -62,11 +62,11 @@ public class Problem17 extends Problem{
//Stop the timer
timer.stop();
//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 problem is solved
solved = true;
//Save the results
result = String.format("The sum of all the letters in all the numbers %d-%d is %d\n", START_NUM, STOP_NUM, letterCount);
}
//Reset the problem so it can be run again
public void reset(){
@@ -75,103 +75,71 @@ public class Problem17 extends Problem{
}
//This function makes a word out of the number passed into it
private String getStringFromNum(int number){
String numberString = new String();
StringBuilder numberString = new StringBuilder();
//Starting with the largest digit create a string based on the number passed in
//Check for negative
if(number < 0){
numberString = numberString.concat("negative ");
numberString = numberString.append("negative ");
}
//Check if the number is zero
if(number == 0){
numberString = numberString.concat("zero");
numberString = numberString.append("zero");
}
//Start with the thousands place
if((number / 1000D) >= 1D){
numberString = numberString.concat(getStringFromNum((int)Math.floor(number / 1000D)));
numberString = numberString.concat(" thousand");
numberString = numberString.append(getStringFromNum((int)Math.floor(number / 1000D)));
numberString = numberString.append(" thousand");
number -= ((int)Math.floor(number / 1000D)) * 1000;
}
//Check the hundreds place
if((number / 100D) >= 1D){
numberString = numberString.concat(getStringFromNum((int)Math.floor(number / 100D)));
numberString = numberString.concat(" hundred");
numberString = numberString.append(getStringFromNum((int)Math.floor(number / 100D)));
numberString = numberString.append(" hundred");
number -= ((int)Math.floor(number / 100D)) * 100;
}
//Insert an and if there is need
if((!numberString.isBlank() && (number > 0))){
numberString = numberString.concat(" and ");
if((!numberString.toString().isBlank() && (number > 0))){
numberString = numberString.append(" and ");
}
//Check for tens place
if((number / 10D) >= 2D){
//For the tens you need to do something special
int tensPlace = (int)Math.floor(number / 10D);
if(tensPlace == 9){
numberString = numberString.concat("ninety");
}
else if(tensPlace == 8){
numberString = numberString.concat("eighty");
}
else if(tensPlace == 7){
numberString = numberString.concat("seventy");
}
else if(tensPlace == 6){
numberString = numberString.concat("sixty");
}
else if(tensPlace == 5){
numberString = numberString.concat("fifty");
}
else if(tensPlace == 4){
numberString = numberString.concat("forty");
}
else if(tensPlace == 3){
numberString = numberString.concat("thirty");
}
else if(tensPlace == 2){
numberString = numberString.concat("twenty");
switch(tensPlace){
case 9: numberString.append("ninety"); break;
case 8: numberString.append("eighty"); break;
case 7: numberString.append("seventy"); break;
case 6: numberString.append("sixty"); break;
case 5: numberString.append("fifty"); break;
case 4: numberString.append("forty"); break;
case 3: numberString.append("thirty"); break;
case 2: numberString.append("twenty"); break;
}
number -= (tensPlace * 10);
//If there is something left in the number you will need a dash to separate the tens and ones place
if(number > 0){
numberString = numberString.concat("-");
numberString = numberString.append("-");
}
}
//Check for teens
else if((number / 10D) >= 1D){
int onesPlace = (number % 10);
if(onesPlace == 9){
numberString = numberString.concat("nineteen");
}
else if(onesPlace == 8){
numberString = numberString.concat("eighteen");
}
else if(onesPlace == 7){
numberString = numberString.concat("seventeen");
}
else if(onesPlace == 6){
numberString = numberString.concat("sixteen");
}
else if(onesPlace == 5){
numberString = numberString.concat("fifteen");
}
else if(onesPlace == 4){
numberString = numberString.concat("fourteen");
}
else if(onesPlace == 3){
numberString = numberString.concat("thirteen");
}
else if(onesPlace == 2){
numberString = numberString.concat("twelve");
}
else if(onesPlace == 1){
numberString = numberString.concat("eleven");
}
else if(onesPlace == 0){
numberString = numberString.concat("ten");
switch(onesPlace){
case 9: numberString.append("nineteen"); break;
case 8: numberString.append("eighteen"); break;
case 7: numberString.append("seventeen"); break;
case 6: numberString.append("sixteen"); break;
case 5: numberString.append("fifteen"); break;
case 4: numberString.append("fourteen"); break;
case 3: numberString.append("thirteen"); break;
case 2: numberString.append("twelve"); break;
case 1: numberString.append("eleven"); break;
case 0: numberString.append("ten"); break;
}
//If this was hit the number was completed
number = 0;
@@ -179,39 +147,23 @@ public class Problem17 extends Problem{
//Check for the ones place
if(number >= 1){
if(number == 9){
numberString = numberString.concat("nine");
}
else if(number == 8){
numberString = numberString.concat("eight");
}
else if(number == 7){
numberString = numberString.concat("seven");
}
else if(number == 6){
numberString = numberString.concat("six");
}
else if(number == 5){
numberString = numberString.concat("five");
}
else if(number == 4){
numberString = numberString.concat("four");
}
else if(number == 3){
numberString = numberString.concat("three");
}
else if(number == 2){
numberString = numberString.concat("two");
}
else if(number == 1){
numberString = numberString.concat("one");
switch(number){
case 9: numberString.append("nine"); break;
case 8: numberString.append("eight"); break;
case 7: numberString.append("seven"); break;
case 6: numberString.append("six"); break;
case 5: numberString.append("five"); break;
case 4: numberString.append("four"); break;
case 3: numberString.append("three"); break;
case 2: numberString.append("two"); break;
case 1: numberString.append("one"); break;
}
//If this was hit the number was completed
number = 0;
}
//Return the string
return numberString;
return numberString.toString();
}
//This counts the number of letters in the string that is passed in (ignoring numbers and punctuation)
private int getNumberChars(String number){

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem18.java
//Matthew Ellison
// Created: 03-11-19
//Modified: 07-18-20
//Modified: 08-26-20
//Find the maximum total from top to bottom
/*
75
@@ -180,10 +180,10 @@ public class Problem18 extends Problem{
//Get the correct total which will be the inversion of the current one
int actualTotal = ((100 * list.size()) - foundPoints.get(foundPoints.size() - 1).total);
result = String.format("The value of the longest path is " + actualTotal);
//Throw a flag to show the problem is solved
solved = true;
result = String.format("The value of the longest path is " + actualTotal);
}
//Reset the problem so it can be run again
public void reset(){

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem19.java
//Matthew Ellison
// Created: 03-13-19
//Modified: 07-10-20
//Modified: 08-26-20
//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.

View File

@@ -1,7 +1,7 @@
//ProjectEulerJava/src/main/java/mattrixwv/ProjectEuler/Problems/Problem9.java
//Matthew Ellison
// Created: 03-02-19
//Modified: 07-18-20
//Modified: 08-26-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
/*