Created
April 17, 2017 01:16
-
-
Save LuxXx/b8afab94ac143555aeb4ab60845b94f5 to your computer and use it in GitHub Desktop.
Project Euler - Problem 17
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package euler; | |
public class NumWords { | |
public static void main(String[] args) { | |
StringBuilder sb = new StringBuilder(); | |
for (int i = 1; i <= 1000; i++) { | |
sb.append(numToWord(i)); | |
} | |
System.out.println(sb.toString().length()); | |
} | |
public static String numToWord(int num) { | |
if (num == 0) return "zero"; | |
else if (num == 1) return "one"; | |
else if (num == 2) return "two"; | |
else if (num == 3) return "three"; | |
else if (num == 4) return "four"; | |
else if (num == 5) return "five"; | |
else if (num == 6) return "six"; | |
else if (num == 7) return "seven"; | |
else if (num == 8) return "eight"; | |
else if (num == 9) return "nine"; | |
else if (num == 10) return "ten"; | |
else if (num == 11) return "eleven"; | |
else if (num == 12) return "twelve"; | |
else if (num == 13) return "thirteen"; | |
else if (num == 14) return "fourteen"; | |
else if (num == 15) return "fifteen"; | |
else if (num == 16) return "sixteen"; | |
else if (num == 17) return "seventeen"; | |
else if (num == 18) return "eighteen"; | |
else if (num == 19) return "nineteen"; | |
else if (num == 20) return "twenty"; | |
else if (num == 30) return "thirty"; | |
else if (num == 40) return "forty"; | |
else if (num == 50) return "fifty"; | |
else if (num == 60) return "sixty"; | |
else if (num == 70) return "seventy"; | |
else if (num == 80) return "eighty"; | |
else if (num == 90) return "ninety"; | |
else if (num == 1000) return "onethousand"; | |
else if (num > 20 && num < 100) | |
return numToWord(num / 10 * 10) + numToWord(num - num / 10 * 10); | |
else if (num % 100 == 0) | |
return numToWord(num / 100) + "hundred"; | |
else if (num > 100 && num < 1000) | |
return numToWord(num / 100 * 100) + "and" + numToWord(num - num / 100 * 100); | |
else return "unknown"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment