Skip to content

Commit 7b1b33a

Browse files
Solution to Problem 19
1 parent eb40f43 commit 7b1b33a

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

Project Euler/Problem 19/sol1.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from __future__ import print_function
2+
'''
3+
Counting Sundays
4+
Problem 19
5+
6+
You are given the following information, but you may prefer to do some research for yourself.
7+
8+
1 Jan 1900 was a Monday.
9+
Thirty days has September,
10+
April, June and November.
11+
All the rest have thirty-one,
12+
Saving February alone,
13+
Which has twenty-eight, rain or shine.
14+
And on leap years, twenty-nine.
15+
16+
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
17+
18+
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
19+
'''
20+
21+
days_per_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
22+
23+
day = 6
24+
month = 1
25+
year = 1901
26+
27+
sundays = 0
28+
29+
while year < 2001:
30+
day += 7
31+
32+
if (year%4 == 0 and not year%100 == 0) or (year%400 == 0):
33+
if day > days_per_month[month-1] and month is not 2:
34+
month += 1
35+
day = day-days_per_month[month-2]
36+
elif day > 29 and month is 2:
37+
month += 1
38+
day = day-29
39+
else:
40+
if day > days_per_month[month-1]:
41+
month += 1
42+
day = day-days_per_month[month-2]
43+
44+
if month > 12:
45+
year += 1
46+
month = 1
47+
48+
if year < 2001 and day is 1:
49+
sundays += 1
50+
51+
print(sundays)

0 commit comments

Comments
 (0)