package margusmartseppcode.From_10_to_19;
public class Problem_17 {
static int hundred = "hundred".length();
static int hundredand = "hundredand".length();
static int onethousand = "onethousand".length();
static int lens[] = new int[100];
static {
String tmp[] = ("| one two three four five six seven eight nine "
+ "| eleven twelve thirteen fourteen fifteen sixteen "
+ "seventeen eighteen nineteen | ten twenty thirty forty "
+ "fifty sixty seventy eighty ninety").split(" ");
for (int i = 1; i < 10; i++) {
lens[i] = tmp[i].length();
lens[i + 10] = tmp[i + 10].length();
lens[i * 10] = tmp[i + 20].length();
}
for (int i = 2; i < 10; i++)
for (int j = 1; j < 10; j++)
lens[i * 10 + j] = lens[i * 10] + lens[j];
}
public static void main(String[] args) {
int sum = 0, hundredsum = 0;
for (int i = 1; i < lens.length; i++)
hundredsum += lens[i];
sum = hundredsum;
for (int i = 1; i < 10; i++)
sum += lens[i] + hundred + (lens[i] + hundredand) * 99
+ hundredsum;
sum += onethousand;
System.out.println(sum);
}
}
Tuesday, September 8, 2009
Euler Problem 17 solution
Time (s): ~0.002
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment