Files
ProjectEulerJava/Problem17.java

211 lines
6.5 KiB
Java

//ProjectEuler/Java/Problem16.java
//Matthew Ellison
// Created: 03-04-19
//Modified: 03-28-19
//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
/*
Copyright (C) 2019 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import mattrixwv.Stopwatch;
public class Problem17{
private static final Integer START_NUM = 1;
private static final Integer STOP_NUM = 1000;
public static void main(String[] argv){
Stopwatch timer = new Stopwatch(); //To time the algorithm's run time
//Setup the variables needed
Integer sumOfLetters = 0;
//Start the timer
timer.start();
//Start with 1 and increment
for(Integer 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
sumOfLetters += getNumberChars(currentNumString);
}
//Stop the timer
timer.stop();
//Print the results
System.out.printf("The sum of all the letters in all the numbers %d-%d is %d\n", START_NUM, STOP_NUM, sumOfLetters);
System.out.println("It took " + timer.getStr() + " to run this algorithm");
}
private static String getStringFromNum(Integer number){
String numberString = new String();
//Starting with the largest digit create a string based on teh number passed in
//Check for negative
if(number < 0){
numberString = numberString.concat("negative ");
}
//Check if teh number is zero
if(number == 0){
numberString = numberString.concat("zero");
}
//Start with the thousands place
if((number.doubleValue() / 1000D) >= 1D){
numberString = numberString.concat(getStringFromNum((int)Math.floor(number / 1000D)));
numberString = numberString.concat(" thousand");
number -= ((int)Math.floor(number.doubleValue() / 1000D)) * 1000;
}
//Check the hundreds place
if((number.doubleValue() / 100D) >= 1D){
numberString = numberString.concat(getStringFromNum((int)Math.floor(number.doubleValue() / 100D)));
numberString = numberString.concat(" hundred");
number -= ((int)Math.floor(number.doubleValue() / 100D)) * 100;
}
//Insert an and if there is need
if((!numberString.isBlank() && (number > 0))){
numberString = numberString.concat(" and ");
}
//Check for tens place
if((number.doubleValue() / 10D) >= 2D){
//FOr the tens you need to do something special
Integer tensPlace = (int)Math.floor(number.doubleValue() / 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");
}
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("-");
}
}
//Check for teens
else if((number.doubleValue() / 10D) >= 1D){
Integer 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");
}
//If this was hit the number was completed
number = 0;
}
//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");
}
//If this was hit the number was completed
number = 0;
}
//Return the string
return numberString;
}
private static Integer getNumberChars(String number){
Integer sumOfLetters = 0;
//Start at location 0 and count the number of letters, ignoring punctuation and whitespace
for(Integer location = 0;location < number.length();++location){
if(Character.isAlphabetic(number.charAt(location))){
sumOfLetters += 1;
}
}
//Return the number of letters
return sumOfLetters;
}
}
/* Results:
The sum of all the letters in all the numbers 1-1000 is 21124
It took 4.862 milliseconds to run this algorithm
*/