package margusmartseppcode.From_10_to_19;
import java.util.Calendar;
public class Problem_19 {
public static void main(String[] a) {
int Sundays = 0, D = Calendar.DATE;
Calendar F = Calendar.getInstance();
for (F.set(1901, 0, 1); F.get(Calendar.YEAR) < 2001; F.add(D, 1))
if (F.get(Calendar.DAY_OF_MONTH) == 1)
if (F.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY)
Sundays++;
System.out.println(Sundays);
}
}
Showing posts with label time. Show all posts
Showing posts with label time. Show all posts
Tuesday, September 8, 2009
Euler Problem 19 solution
Time (s): ~0.098
Labels:
calendar,
dates,
Euler Problem 10-19,
time
Euler Problem 17 solution
Time (s): ~0.002
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);
}
}
Subscribe to:
Posts (Atom)