From 349b8316171998d47f9d22ec3c7ef8ae4aca5b6a Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Tue, 16 Jun 2020 19:25:26 -0400 Subject: [PATCH] Updated for performance --- .../ProjectEuler/Problems/Problem17.java | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem17.java b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem17.java index 1f9ccde..e0945d6 100644 --- a/src/main/java/mattrixwv/ProjectEuler/Problems/Problem17.java +++ b/src/main/java/mattrixwv/ProjectEuler/Problems/Problem17.java @@ -1,7 +1,7 @@ //ProjectEuler/Java/Problem16.java //Matthew Ellison // 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? //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{ //The first number to be - private static final Integer START_NUM = 1; - private static final Integer STOP_NUM = 1000; + private static final int START_NUM = 1; + private static final int STOP_NUM = 1000; 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?"); } public void solve(){ //Setup the variables needed - Integer sumOfLetters = 0; + int sumOfLetters = 0; //Start the timer timer.start(); //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 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 @@ -52,7 +52,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, sumOfLetters); } - private String getStringFromNum(Integer number){ + private String getStringFromNum(int number){ String numberString = new String(); //Starting with the largest digit create a string based on teh number passed in //Check for negative @@ -66,17 +66,17 @@ public class Problem17 extends Problem{ } //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(" thousand"); - number -= ((int)Math.floor(number.doubleValue() / 1000D)) * 1000; + number -= ((int)Math.floor(number / 1000D)) * 1000; } //Check the hundreds place - if((number.doubleValue() / 100D) >= 1D){ - numberString = numberString.concat(getStringFromNum((int)Math.floor(number.doubleValue() / 100D))); + if((number / 100D) >= 1D){ + numberString = numberString.concat(getStringFromNum((int)Math.floor(number / 100D))); 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 @@ -85,9 +85,9 @@ public class Problem17 extends Problem{ } //Check for tens place - if((number.doubleValue() / 10D) >= 2D){ + if((number / 10D) >= 2D){ //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){ numberString = numberString.concat("ninety"); } @@ -119,8 +119,8 @@ public class Problem17 extends Problem{ } } //Check for teens - else if((number.doubleValue() / 10D) >= 1D){ - Integer onesPlace = (number % 10); + else if((number / 10D) >= 1D){ + int onesPlace = (number % 10); if(onesPlace == 9){ numberString = numberString.concat("nineteen"); } @@ -191,10 +191,10 @@ public class Problem17 extends Problem{ //Return the string return numberString; } - private Integer getNumberChars(String number){ - Integer sumOfLetters = 0; + private int getNumberChars(String number){ + int sumOfLetters = 0; //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))){ sumOfLetters += 1; } @@ -207,5 +207,5 @@ public class Problem17 extends Problem{ /* Results: 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. */