Programming/Project Euler - python
Project Euler 19
_Sudal
2019. 3. 7. 05:00
Question:
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)?
문제:
다음은 달력에 관한 몇 가지 일반적인 정보입니다 (필요한 경우 좀 더 연구를 해 보셔도 좋습니다).
- 1900년 1월 1일은 월요일이다.
- 4월, 6월, 9월, 11월은 30일까지 있고, 1월, 3월, 5월, 7월, 8월, 10월, 12월은 31일까지 있다.
- 2월은 28일이지만, 윤년에는 29일까지 있다.
- 윤년은 연도를 4로 나누어 떨어지는 해를 말한다. 하지만 400으로 나누어 떨어지지 않는 매 100년째는 윤년이 아니며, 400으로 나누어 떨어지면 윤년이다
20세기 (1901년 1월 1일 ~ 2000년 12월 31일) 에서, 매월 1일이 일요일인 경우는 총 몇 번입니까?
Solution:
1 2 3 4 5 6 7 8 9 10 11 | import calendar,time sunday = 0 start = time.time() for year in range(1901,2001): for month in range(1,13): if calendar.weekday(year,month,1) == 6: # 6 = sunday sunday += 1 print("""The number of sundays on the first of the month: {} Found in ...{:.3f}s""".format(sunday,time.time()-start)) | cs |
calendar 모듈을 import 해서 써왔다.
2019/03/01 - [Programming/python] - Module: Calendar
1901년부터 2000년까지,
1일이 일요일일 때 카운트했다.
0.001 s 걸렸다!