mirror of
https://bitbucket.org/Mattrixwv/projecteulerjava.git
synced 2025-12-06 17:13:58 -05:00
Optimized Problem17
This commit is contained in:
@@ -81,31 +81,31 @@ public class Problem17 extends Problem{
|
||||
//Starting with the largest digit create a string based on the number passed in
|
||||
//Check for negative
|
||||
if(num < 0){
|
||||
numberString = numberString.append("negative ");
|
||||
numberString.append("negative ");
|
||||
}
|
||||
|
||||
//Check if the number is zero
|
||||
if(num == 0){
|
||||
numberString = numberString.append("zero");
|
||||
numberString.append("zero");
|
||||
}
|
||||
|
||||
//Start with the thousands place
|
||||
if((num / 1000D) >= 1D){
|
||||
numberString = numberString.append(makeWordFromNum((int)Math.floor(num / 1000D)));
|
||||
numberString = numberString.append(" thousand");
|
||||
numberString.append(makeWordFromNum((int)Math.floor(num / 1000D)));
|
||||
numberString.append(" thousand");
|
||||
num -= ((int)Math.floor(num / 1000D) * 1000);
|
||||
}
|
||||
|
||||
//Check the hundreds place
|
||||
if((num / 100D) >= 1D){
|
||||
numberString = numberString.append(makeWordFromNum((int)Math.floor(num / 100D)));
|
||||
numberString = numberString.append(" hundred");
|
||||
numberString.append(makeWordFromNum((int)Math.floor(num / 100D)));
|
||||
numberString.append(" hundred");
|
||||
num -= ((int)Math.floor(num / 100D) * 100);
|
||||
}
|
||||
|
||||
//Insert an and if there is need
|
||||
if((!numberString.toString().isBlank() && (num > 0))){
|
||||
numberString = numberString.append(" and ");
|
||||
if(!numberString.toString().isBlank() && (num > 0)){
|
||||
numberString.append(" and ");
|
||||
}
|
||||
|
||||
//Check for tens place
|
||||
@@ -125,7 +125,7 @@ public class Problem17 extends Problem{
|
||||
num -= (tensPlace * 10);
|
||||
//If there is something left in the number you will need a dash to separate the tens and ones place
|
||||
if(num > 0){
|
||||
numberString = numberString.append("-");
|
||||
numberString.append("-");
|
||||
}
|
||||
}
|
||||
//Check for teens
|
||||
@@ -166,7 +166,7 @@ public class Problem17 extends Problem{
|
||||
|
||||
//If the number is not 0 there was a problem
|
||||
if(num != 0){
|
||||
throw new Unsolved("The number " + number + "was not completely reduced!\nRemainder is " + num);
|
||||
throw new Unsolved("The number " + number + " was not completely reduced!\nRemainder is " + num);
|
||||
}
|
||||
|
||||
//Return the string
|
||||
@@ -206,5 +206,5 @@ public class Problem17 extends Problem{
|
||||
|
||||
/* Results:
|
||||
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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user