'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 1:[1]
So your problem is your range, right now its from 1900 to 2000 exclusive, it should be 1901 to 2000 inclusive. In short the answer is that you need to change the while loop to while(year<=2000)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
