Project-Euler-019

Problem

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

Answer

1
171

Python

1
2
3
#!/usr/bin/env python
from calendar import monthrange; from itertools import product
print(len([(year, month) for year, month in product(list(range(1901, 2001)), list(range(1, 13))) if monthrange(year, month)[0] == 6]))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
months = [
31,
28,
31,
30,
31,
30,
31,
31,
30,
31,
30,
31
]

week = ['sun',
'mon',
'tue',
'wed',
'thu',
'fri',
'sat'
]

leapyears = [year for year in range(1, 101) if year % 4 == 0]

day = 0 # 1 Jan 1901
month = 0 # January
year = 1 # 1901
weekday = 2 # Tuesday

days = {day: week[weekday]}

since_last_month = 0
since_last_year = 0

first_of_months = {}
while year <= 100:
#print "%s/%s/%s - %s" % (month+1, since_last_month+1, year+1900, week[weekday])
# if it's the first of the month, make a note
# of the weekday.
if since_last_month == 0:
first_of_months["%s/%s/%s" % (month+1, since_last_month+1, year+1900)] = week[weekday]

# increment the counters..
day += 1
since_last_month += 1
since_last_year += 1

# check what year it is
days_of_year = 365
if year in leapyears:
days_of_year += 1
if since_last_year >= days_of_year:
year += 1
since_last_year = 0

# check what month it is
days_of_month = months[month]
if month == 1 and year in leapyears: # february
days_of_month += 1
if since_last_month >= days_of_month:
month += 1
since_last_month = 0
if month > len(months)-1:
month = 0

# check what day of the week it is
weekday += 1
if weekday > len(week)-1:
weekday = 0
days[day] = week[weekday]

def date_sort(date):
date_str = date[0]
month,day,year = date_str.split('/')
return int(month)*int(day)*int(year)

print(len([weekday for weekday in list(first_of_months.values()) if weekday == 'sun']))

Ruby

1
2
3
#!/usr/bin/env ruby
require 'date'
puts Date.new(1901,1,1).upto(Date.new(2000,12,31)).find_all { |d| d.mday == 1 && d.wday == 0 }.count


Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public final class p019 implements EulerSolution {

public static void main(String[] args) {
System.out.println(new p019().run());
}


/*
* We use Zeller's congruence to compute the day of week when given the year, month, and day.
* Then we simply check the first day of all the months in the given range by brute force.
*
* Zeller's congruence is well-known and a bit long to explain.
* See: https://en.wikipedia.org/wiki/Zeller%27s_congruence
*/

public String run() {
int count = 0;
for (int y = 1901; y <= 2000; y++) {
for (int m = 1; m <= 12; m++) {
if (dayOfWeek(y, m, 1) == 0) // Sunday
count++;
}
}
return Integer.toString(count);
}


// Return value: 0 = Sunday, 1 = Monday, ..., 6 = Saturday.
private static int dayOfWeek(int year, int month, int day) {
if (year < 0 || year > 10000 || month < 1 || month > 12 || day < 1 || day > 31)
throw new IllegalArgumentException();

// Zeller's congruence algorithm
int m = (month - 3 + 4800) % 4800;
int y = (year + m / 12) % 400;
m %= 12;
return (y + y/4 - y/100 + (13 * m + 2) / 5 + day + 2) % 7;
}

}


Mathematica

1
2
3
4
5
(* 
* We simply use Mathematica's built-in date library to compute the answer by brute force.
*)
<< Miscellaneous`Calendar`
Sum[Boole[DayOfWeek[{y, m, 1}] === Sunday], {y, 1901, 2000}, {m, 1, 12}]

文章作者: Monad Kai
文章链接: onlookerliu.github.io/2018/03/13/Project-Euler-019/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Code@浮生记
支付宝打赏
微信打赏