Java Implementation of Problem 19

View source code here on GitHub!

public class p0019 implements IEuler
Object answer()
Returns:

The answer to Project Euler problem 19

 1/*
 2Project Euler Problem 19
 3
 4This one ended up being very easy thanks to the DateTime library
 5
 6Problem:
 7
 8You are given the following information, but you may prefer to do some research
 9for yourself.
10
11    1 Jan 1900 was a Monday.
12    Thirty days has September,
13    April, June and November.
14    All the rest have thirty-one,
15    Saving February alone,
16    Which has twenty-eight, rain or shine.
17    And on leap years, twenty-nine.
18    A leap year occurs on any year evenly divisible by 4, but not on a century
19    unless it is divisible by 400.
20
21How many Sundays fell on the first of the month during the twentieth century
22(1 Jan 1901 to 31 Dec 2000)?
23*/
24package euler;
25
26import java.time.DayOfWeek;
27import java.time.LocalDate;
28
29public class p0019 implements IEuler {
30    @Override
31    public Object answer() {
32        byte answer = 0;
33        for (short x = 1901; x < 2001; x++)
34            for (byte y = 1; y < 13; y++)
35                if (LocalDate.of(x, y, 1).getDayOfWeek() == DayOfWeek.SUNDAY)
36                    answer++;
37        return answer;
38    }
39}

Tags: calendar, combinatorics