javaswitch-statementleap-year

Project Euler #19, Answer is only correct when starting from year 1900?


I've been trying to understand why the following code only gives the correct answer when the start year is set to 1900. The problem specifies that Sundays should be counted from Jan 1st, 1901.

I thought that I figured it out when realizing there is no year 0, so 1900 is actually 1901... but that didn't work when adjusting the while loop from 2000 years to 1999 years.

I am completely flabbergasted at why this gives the right answer using the wrong starting year.

public class Euler19
{
    public static void main(String[] args)
    {
        int day = 2; //Jan 1st, 1901 is a Tuesday, This should be 3 but only works if set to 2 for Monday
        int sundays = 0;
        int month = 1;
        int monthDays = 1;
        int year = 1901;
        while(year <= 2000)
        {
            switch(month) //Case for each month
            {
                case 1:                   //January
                    if(monthDays == 31) 
                    {
                        month++;
                        monthDays = 1;
                    }
                    break;
                case 2:                                   //February
                    if((year % 4 == 0) && (year != 1900)) //Leap days
                    {
                        if(monthDays == 29)
                        {
                            month++;
                            monthDays = 1;
                        }
                    }
                    else
                    {
                        if(monthDays == 28)
                        {
                            month++;
                            monthDays = 1;
                        }
                    }
                    break;
                case 3:                 //March
                    if(monthDays == 31)
                    {
                        month++;
                        monthDays = 1;
                    }
                    break;
                case 4:                //April
                    if(monthDays == 30)
                    {
                        month++;
                        monthDays = 1;
                    }
                    break;
                case 5:                 //May
                    if(monthDays == 31)
                    {
                        month++;
                        monthDays = 1;
                    }
                    break;
                case 6:                 //June
                    if(monthDays == 30)
                    {
                        month++;
                        monthDays = 1;
                    }
                    break;
                case 7:                 //July
                    if(monthDays == 31)
                    {
                        month++;
                        monthDays = 1;
                    }
                    break;
                case 8:                 //August
                    if(monthDays == 31)
                    {
                        month++;
                        monthDays = 1;
                    }
                    break;
                case 9:                 //September
                    if(monthDays == 30)
                    {
                        month++;
                        monthDays = 1;
                    }
                    break;
                case 10:                //October
                    if(monthDays == 31)
                    {
                        month++;
                        monthDays = 1;
                    }
                    break;
                case 11:                //November
                    if(monthDays == 30)
                    {
                        month++;
                        monthDays = 1;
                    }
                    break;
                case 12:                //December
                    if(monthDays == 31)
                    {
                        month = 1;
                        monthDays = 1;
                        year++;
                    }
                    break;
            }
            
            if(day == 1 && monthDays == 1) //Sunday is 1st day of the week
            {
                sundays++;
            }
            
            if(day == 7)
            {
                day = 0;
            }
            day++;
            monthDays++;
        }
        System.out.println("Total Sundays: " + sundays);
    }
}

Solution

  • Your error is elsewhere: Your program is skipping the first of every month. That you happened to get the correct result when using a wrong start year is a complete coincidence.

    Starting out from Monday January 1, 1900 (whether that day was really a Monday or not), you are counting correctly through January and deem January 31 a Wednesday. So far it is correct based in the prerequisite. Then comes the trouble: Since month is 1 and monthDays 31, you increment month to 2 and set monthDays to 1 in your switch statement. After your switch and your two if statements you increment day (correct) and increment monthDays to 2, which gives an incorrect value. You have now got Thursday, February 2, 1900, which disagrees with January 31 being a Wednesday.

    The same incorrect increment happens every time a new month starts.

    I suggest you learn to use a debugger. This will enable you to find such bugs on your own.