Friday 10 April 2009

PROJECT EULER #19

Link to page

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)?



using System;

namespace ProjectEuler
{
class Program
{
static void Main()
{
//Problem 19
DateTime start = DateTime.Now;
DateTime date = new DateTime(1901,1,1);
int sundays = 0;
while (date.Year<2001)
{
if(date.Day==1 && date.DayOfWeek==DayOfWeek.Sunday)
{
sundays++;
}
date=date.AddDays(1);
}
Console.WriteLine("There were {0} sundays in the 20th Century",sundays);
TimeSpan time = DateTime.Now-start;
Console.WriteLine("This took {0}", time);
Console.ReadKey();
}
}
}

No comments: