Optimized Problem17

This commit is contained in:
2021-03-29 14:35:54 -04:00
parent a2298b5b8c
commit 4fc455bff0

View File

@@ -81,31 +81,31 @@ public class Problem17 extends Problem{
//Starting with the largest digit create a string based on the number passed in //Starting with the largest digit create a string based on the number passed in
//Check for negative //Check for negative
if(num < 0){ if(num < 0){
numberString = numberString.append("negative "); numberString.append("negative ");
} }
//Check if the number is zero //Check if the number is zero
if(num == 0){ if(num == 0){
numberString = numberString.append("zero"); numberString.append("zero");
} }
//Start with the thousands place //Start with the thousands place
if((num / 1000D) >= 1D){ if((num / 1000D) >= 1D){
numberString = numberString.append(makeWordFromNum((int)Math.floor(num / 1000D))); numberString.append(makeWordFromNum((int)Math.floor(num / 1000D)));
numberString = numberString.append(" thousand"); numberString.append(" thousand");
num -= ((int)Math.floor(num / 1000D) * 1000); num -= ((int)Math.floor(num / 1000D) * 1000);
} }
//Check the hundreds place //Check the hundreds place
if((num / 100D) >= 1D){ if((num / 100D) >= 1D){
numberString = numberString.append(makeWordFromNum((int)Math.floor(num / 100D))); numberString.append(makeWordFromNum((int)Math.floor(num / 100D)));
numberString = numberString.append(" hundred"); numberString.append(" hundred");
num -= ((int)Math.floor(num / 100D) * 100); num -= ((int)Math.floor(num / 100D) * 100);
} }
//Insert an and if there is need //Insert an and if there is need
if((!numberString.toString().isBlank() && (num > 0))){ if(!numberString.toString().isBlank() && (num > 0)){
numberString = numberString.append(" and "); numberString.append(" and ");
} }
//Check for tens place //Check for tens place
@@ -125,7 +125,7 @@ public class Problem17 extends Problem{
num -= (tensPlace * 10); num -= (tensPlace * 10);
//If there is something left in the number you will need a dash to separate the tens and ones place //If there is something left in the number you will need a dash to separate the tens and ones place
if(num > 0){ if(num > 0){
numberString = numberString.append("-"); numberString.append("-");
} }
} }
//Check for teens //Check for teens
@@ -206,5 +206,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 an average of 507.896 microseconds to run this problem through 100 iterations It took an average of 216.210 microseconds to run this problem through 100 iterations
*/ */