Tuesday 16 December 2014

Counting Sundays Problem 19 Project Euler

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


C PROGRAM:
main()
{
int counter=0;
int n;
int k=1;
int i;
int year;
for(year=1900;year<=2001;year++)
{
    for(i=0;i<=11;i++)
    {


        if(i==0||i==2||i==4||i==6||i==7||i==9||i==11)
        k=k+(31%7);
        if(i==3||i==5||i==8||i==10)
        k=k+(30%7);
        if(i==1)
        {
            if(year%100==0&&year%400==0)
            {
                k=k+(29%7);
            }
            if(year%100!=0&&year%4==0)
            {
                k=k+(29%7);
            }
            if(year%4!=0)
                k=k+(28%7);
        }
        if(k%7==0)counter++;
        }
if(year==1900){counter=-counter;}
}
printf("%d",counter);


}


No comments:

Post a Comment