Updated for performance

This commit is contained in:
2020-06-16 19:25:26 -04:00
parent c043df9b44
commit 349b831617

View File

@@ -1,7 +1,7 @@
//ProjectEuler/Java/Problem16.java //ProjectEuler/Java/Problem16.java
//Matthew Ellison //Matthew Ellison
// Created: 03-04-19 // Created: 03-04-19
//Modified: 03-28-19 //Modified: 06-16-20
//If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used? //If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
//Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses //Unless otherwise listed all non-standard includes are my own creation and available from https://bibucket.org/Mattrixwv/JavaClasses
/* /*
@@ -25,21 +25,21 @@ package mattrixwv.ProjectEuler.Problems;
public class Problem17 extends Problem{ public class Problem17 extends Problem{
//The first number to be //The first number to be
private static final Integer START_NUM = 1; private static final int START_NUM = 1;
private static final Integer STOP_NUM = 1000; private static final int STOP_NUM = 1000;
public Problem17(){ public Problem17(){
super("If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?"); super("If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?");
} }
public void solve(){ public void solve(){
//Setup the variables needed //Setup the variables needed
Integer sumOfLetters = 0; int sumOfLetters = 0;
//Start the timer //Start the timer
timer.start(); timer.start();
//Start with 1 and increment //Start with 1 and increment
for(Integer 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
String currentNumString = getStringFromNum(num); String currentNumString = getStringFromNum(num);
//Pass the string to ta function that will count the number of letters in it, ignoring whitespace and punctuation and add that number to the running tally //Pass the string to ta function that will count the number of letters in it, ignoring whitespace and punctuation and add that number to the running tally
@@ -52,7 +52,7 @@ public class Problem17 extends Problem{
//Save the results //Save the results
result = String.format("The sum of all the letters in all the numbers %d-%d is %d\n", START_NUM, STOP_NUM, sumOfLetters); result = String.format("The sum of all the letters in all the numbers %d-%d is %d\n", START_NUM, STOP_NUM, sumOfLetters);
} }
private String getStringFromNum(Integer number){ private String getStringFromNum(int number){
String numberString = new String(); String numberString = new String();
//Starting with the largest digit create a string based on teh number passed in //Starting with the largest digit create a string based on teh number passed in
//Check for negative //Check for negative
@@ -66,17 +66,17 @@ public class Problem17 extends Problem{
} }
//Start with the thousands place //Start with the thousands place
if((number.doubleValue() / 1000D) >= 1D){ if((number / 1000D) >= 1D){
numberString = numberString.concat(getStringFromNum((int)Math.floor(number / 1000D))); numberString = numberString.concat(getStringFromNum((int)Math.floor(number / 1000D)));
numberString = numberString.concat(" thousand"); numberString = numberString.concat(" thousand");
number -= ((int)Math.floor(number.doubleValue() / 1000D)) * 1000; number -= ((int)Math.floor(number / 1000D)) * 1000;
} }
//Check the hundreds place //Check the hundreds place
if((number.doubleValue() / 100D) >= 1D){ if((number / 100D) >= 1D){
numberString = numberString.concat(getStringFromNum((int)Math.floor(number.doubleValue() / 100D))); numberString = numberString.concat(getStringFromNum((int)Math.floor(number / 100D)));
numberString = numberString.concat(" hundred"); numberString = numberString.concat(" hundred");
number -= ((int)Math.floor(number.doubleValue() / 100D)) * 100; number -= ((int)Math.floor(number / 100D)) * 100;
} }
//Insert an and if there is need //Insert an and if there is need
@@ -85,9 +85,9 @@ public class Problem17 extends Problem{
} }
//Check for tens place //Check for tens place
if((number.doubleValue() / 10D) >= 2D){ if((number / 10D) >= 2D){
//FOr the tens you need to do something special //FOr the tens you need to do something special
Integer tensPlace = (int)Math.floor(number.doubleValue() / 10D); int tensPlace = (int)Math.floor(number / 10D);
if(tensPlace == 9){ if(tensPlace == 9){
numberString = numberString.concat("ninety"); numberString = numberString.concat("ninety");
} }
@@ -119,8 +119,8 @@ public class Problem17 extends Problem{
} }
} }
//Check for teens //Check for teens
else if((number.doubleValue() / 10D) >= 1D){ else if((number / 10D) >= 1D){
Integer onesPlace = (number % 10); int onesPlace = (number % 10);
if(onesPlace == 9){ if(onesPlace == 9){
numberString = numberString.concat("nineteen"); numberString = numberString.concat("nineteen");
} }
@@ -191,10 +191,10 @@ public class Problem17 extends Problem{
//Return the string //Return the string
return numberString; return numberString;
} }
private Integer getNumberChars(String number){ private int getNumberChars(String number){
Integer sumOfLetters = 0; int sumOfLetters = 0;
//Start at location 0 and count the number of letters, ignoring punctuation and whitespace //Start at location 0 and count the number of letters, ignoring punctuation and whitespace
for(Integer location = 0;location < number.length();++location){ for(int location = 0;location < number.length();++location){
if(Character.isAlphabetic(number.charAt(location))){ if(Character.isAlphabetic(number.charAt(location))){
sumOfLetters += 1; sumOfLetters += 1;
} }
@@ -207,5 +207,5 @@ public class Problem17 extends Problem{
/* 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 4.367 milliseconds to solve this problem. It took 285.699 microseconds to solve this problem.
*/ */