Euler Problem 19

by Andrew Poes

HTML

<!-- You are given the following information, but you may prefer to do some research for yourself.

1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? -->

CSS

.print {
    position: relative;
    display: inline-block;
    background-color: black;
    color: white;
    font-family: Helvetica, Helvetica-Neue, sans-serif;
    font-weight: bold;
    font-size: 24px;
    letter-spacing: -1.5px;
    padding: 4px 8px;
}

body {
    background-color: #eeeeee;
}
}

input {
    padding: 20px;
}
}

JavaScript

(function(D) {
    // DAYS
    D.Sunday 		= 0
    D.Monday 		= 1
    D.Tuesday 		= 2
    D.Wednesday		= 3
    D.Thursday 		= 4
    D.Friday		= 5
    D.Saturday		= 6
    D.TotalDays		= 7
    // MONTHS
    D.January 		= 0
    D.February 		= 1
    D.March 		= 2
    D.April 		= 3
    D.May 			= 4
    D.June 			= 5
    D.July 			= 6
    D.August 		= 7
    D.September		= 8
    D.October 		= 9
    D.November 		= 10
    D.December 		= 11

    D.daysOfMonth = {}
    D.daysOfMonth[D.January] 		= 31;
    D.daysOfMonth[D.February] 		= 28;
    D.daysOfMonth[D.March] 			= 31;
    D.daysOfMonth[D.April] 			= 30;
    D.daysOfMonth[D.May] 			= 31;
    D.daysOfMonth[D.June] 			= 30;
    D.daysOfMonth[D.July] 			= 31;
    D.daysOfMonth[D.August] 		= 31;
    D.daysOfMonth[D.September] 		= 30;
    D.daysOfMonth[D.October] 		= 31;
    D.daysOfMonth[D.November] 		= 30;
    D.daysOfMonth[D.December]		=  31;

    $(document).ready(function() {
        var total = sundaysOccuringOnFirstFromYearToYear(1901, 2001);
        print("Total on first: " + total);
    })

    function sundaysOccuringOnFirstFromYearToYear(fromYear, toYear) {
        var totalySundayOnFirst = 0;
        var month = 0;
        var firstSunday = firstKnownSunday(fromYear);
        var sunday = firstSunday;
        var curYear = fromYear;
        while (curYear < toYear) {
            if (sunday == 1) {
                totalySundayOnFirst++;
            }
            var daysOfMonth = getDays(month%12, curYear);
            sunday += 28;
            if (sunday <= daysOfMonth) {
                sunday += D.TotalDays;
            }
            sunday = sunday%daysOfMonth;
            month++;
            if (month%12 == 0) {
                curYear++;
            }
        }
        return totalySundayOnFirst;
    }

    function firstKnownSunday(year) {
        var firstKnownDate = 1;
        var firstKnownDay = 1;
        var firstKnownYear = 1900;
        var firstKnownSunday = -1;
        while (firstKnownSunday < 0)...