Hint for Project Euler Problem 19 Counting Sundays
Tags: #Project Euler without coding series #junior high maths
Counting Sundays
Problem 19
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)?
Let's take a break from the latest brain-drilling problems and take a look at some rather simple issues. As you can see from the tags, we will do this without coding.
------------------------------------------------------
Method 1
Tool: Paper, pen
31 days mod 7 = 3 days
30 days mod 7 = 2 days
29 days mod 7 = 1 days
28 days mod 7 = 0 days
If assign Mon = 1, Tue = 2, ... , Sat = 6, Sun = 0; we have the table listed as below:
Non Leap Year
Jan(+3) Feb(+0) Mar(+3) Apr(+2) May(+3) Jun(+2) Jul(+3) Aug(+3) Sep(+2) Oct(+3) Nov(+2) Dec(+3)
1 4 4 0 2 5 0 3 6 1 4 6 -> If Jan 1st is Mon, then we have 2 Sundays fell on the 1st of month, Apr & Jul;
2 5 5 1 3 6 1 4 0 2 5 0 -> If Jan 1st is Tue, then we have 2 Sundays fell on the 1st of month, Sep & Dec;
3 6 6 2 4 0 2 5 1 3 6 1 -> If Jan 1st is Wed, then we have 1 Sundays fell on the 1st of month, Jun;
4 0 0 3 5 1 3 6 2 4 0 2 -> If Jan 1st is Thur, then we have 3 Sundays fell on the 1st of month, Feb,Mar,Nov;
5 1 1 4 6 2 4 0 3 5 1 3 -> If Jan 1st is Fri, then we have 1 Sundays fell on the 1st of month, Aug;
6 2 2 5 0 3 5 1 4 6 2 4 -> If Jan 1st is Sat, then we have 1 Sundays fell on the 1st of month, May;
0 3 3 6 1 4 6 2 5 0 3 5 -> If Jan 1st is Sun, then we have 2 Sundays fell on the 1st of month, Jan & Oct;
Leap Year
Since,
Non-leap year
Since (5,7)=1
Altogether, 3x3+1 = 10 groups of (0123456) + (23450)
Similar for the 25 leap years, (2000 mod 400 = 0, 2000 is a leap year), we started at
Since (5,7)=1
Altogether, 3 groups of (0123456) + (6420)
Using the table to count Sundays, we have the below equation.
Method 2
Jan(+3) Feb(+1) Mar(+3) Apr(+2) May(+3) Jun(+2) Jul(+3) Aug(+3) Sep(+2) Oct(+3) Nov(+2) Dec(+3)
1 4 5 1 3 6 1 4 0 2 5 0 -> If Jan 1st is Mon, then we have 2 Sundays fell on the 1st of month;
2 5 6 2 4 0 2 5 1 3 6 1 -> If Jan 1st is Tue, then we have 1 Sundays fell on the 1st of month;
3 6 0 3 5 1 3 6 2 4 0 2 -> If Jan 1st is Wed, then we have 2 Sundays fell on the 1st of month;
4 0 1 4 6 2 4 0 3 5 1 3 -> If Jan 1st is Thur, then we have 2 Sundays fell on the 1st of month;
5 1 2 5 0 3 5 1 4 6 2 4 -> If Jan 1st is Fri, then we have 1 Sundays fell on the 1st of month;
6 2 3 6 1 4 6 2 5 0 3 5 -> If Jan 1st is Sat, then we have 1 Sundays fell on the 1st of month;
0 3 4 0 2 5 0 3 6 1 4 6 -> If Jan 1st is Sun, then we have 3 Sundays fell on the 1st of month;
365 days mod 7 = 1 day
366 days mod 7 = 2 days
1901/01/01 -> 2
1902/01/01 -> 3
1903/01/01 -> 4
+3
1905/01/01 -> 0
1906/01/01 -> 1
1907/01/01 -> 2
...
We can see the pattern that (234,012,560,345,123,601,456) forms a group of 21 non-leap years which have 3 x (0123456) for Jan 1st.
It repeated 3 times to cover 21x3 = 63 non leap years.
The rest 12 non leap years are (234,012,560,345)
1904/01/01 -> 6
+5
1908/01/01 -> 4
...
We can see the pattern that (5,3,1,6,4,2,0) forms a group of 7 leap years which have 1 x (0123456) for Jan 1st.
It repeated 3 times to cover 7x3 = 21 leap years.
The rest 4 non leap years are (5,3,1,6)
10 x 12 + (2+1+3+1+2) + 3 x 12 + (1+2+2+1) = 12 x 13 + 9 + 6 = 144 + 12 + 15 = 171
-----------------------------------------------------
Method 2
Tool: Excel Spreadsheet
It is just 12 x 100 = 1200 months. With Excel functions, it is easy to count all Sundays within those 1200 days.
-----------------------------------------------------
Method 3
Tool: A lucky guess
1200 / 7 = 171.4
2 5 6 2 4 0 2 5 1 3 6 1 -> If Jan 1st is Tue, then we have 1 Sundays fell on the 1st of month;
3 6 0 3 5 1 3 6 2 4 0 2 -> If Jan 1st is Wed, then we have 2 Sundays fell on the 1st of month;
4 0 1 4 6 2 4 0 3 5 1 3 -> If Jan 1st is Thur, then we have 2 Sundays fell on the 1st of month;
5 1 2 5 0 3 5 1 4 6 2 4 -> If Jan 1st is Fri, then we have 1 Sundays fell on the 1st of month;
6 2 3 6 1 4 6 2 5 0 3 5 -> If Jan 1st is Sat, then we have 1 Sundays fell on the 1st of month;
0 3 4 0 2 5 0 3 6 1 4 6 -> If Jan 1st is Sun, then we have 3 Sundays fell on the 1st of month;
365 days mod 7 = 1 day
366 days mod 7 = 2 days
1901/01/01 -> 2
1902/01/01 -> 3
1903/01/01 -> 4
+3
1905/01/01 -> 0
1906/01/01 -> 1
1907/01/01 -> 2
...
We can see the pattern that (234,012,560,345,123,601,456) forms a group of 21 non-leap years which have 3 x (0123456) for Jan 1st.
It repeated 3 times to cover 21x3 = 63 non leap years.
The rest 12 non leap years are (234,012,560,345)
1904/01/01 -> 6
+5
1908/01/01 -> 4
...
We can see the pattern that (5,3,1,6,4,2,0) forms a group of 7 leap years which have 1 x (0123456) for Jan 1st.
It repeated 3 times to cover 7x3 = 21 leap years.
The rest 4 non leap years are (5,3,1,6)
10 x 12 + (2+1+3+1+2) + 3 x 12 + (1+2+2+1) = 12 x 13 + 9 + 6 = 144 + 12 + 15 = 171
-----------------------------------------------------
Method 2
Tool: Excel Spreadsheet
It is just 12 x 100 = 1200 months. With Excel functions, it is easy to count all Sundays within those 1200 days.
-----------------------------------------------------
Method 3
Tool: A lucky guess
1200 / 7 = 171.4
---------------------
The opinion above is original and unproven work. Please correct me if I made a mistake.
The opinion above is original and unproven work. Please correct me if I made a mistake.
"It is just 12 x 100 = 1200 days." no it's 12 x 100 = 1200 months.
ReplyDeleteThanks for correction.
Delete