Skip to content

Instantly share code, notes, and snippets.

@WilliamHester
Last active August 29, 2015 14:23
Show Gist options
  • Save WilliamHester/1cbef1461e1e4d5c5aa9 to your computer and use it in GitHub Desktop.
Save WilliamHester/1cbef1461e1e4d5c5aa9 to your computer and use it in GitHub Desktop.
import java.util.*;
public class TwoHundred {
public static final String[] ONES = new String[]{"",
"ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX",
"SEVEN", "EIGHT", "NINE"};
public static final String[] TEENS = new String[]{"TEN",
"ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN",
"FIFTEEN", "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"};
public static final String[] TENS = new String[]{"",
"TEN", "TWENTY", "THIRTY", "FORTY", "FIFTY",
"SIXTY", "SEVENTY", "EIGHTY", "NINETY"};
public static final String HUNDRED = "HUNDRED";
public static final String THOUSAND = "THOUSAND";
public static final String AND = "AND";
public static final int MAX = 9999;
public static void main(String[] args) {
for (int i = 1; i <= MAX; i++) {
StringBuilder number = new StringBuilder();
int num = i;
if (num / 1000 > 0) {
num %= 1000;
number.append(ONES[i / 1000])
.append(THOUSAND);
}
if (num / 100 > 0) {
number.append(ONES[num / 100])
.append(HUNDRED);
num %= 100;
}
if (number.length() > 0) {
number.append(AND);
}
if (num > 10 && num < 20) {
number.append(TEENS[num % 10]);
} else if (num > 0) {
number.append(TENS[num / 10])
.append(ONES[num % 10]);
}
if (sumChars(number.toString()) == i) {
System.out.println(number.toString());
}
}
}
public static int sumChars(String s) {
int sum = 0;
for (int i = 0; i < s.length(); i++) {
sum += s.charAt(i) - 'A' + 1;
}
return sum;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment