Project Euler Problem 019
http://projecteuler.net/index.php?section=problems&id=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)?
20世紀に日曜日から始まる月は何回あるか?
地道に数えた。
import datetime def is_leapyear(n): return n % 4 == 0 and (n % 400 == 0 or n % 100) def euler019(): # 1 2 3 4 5 6 7 8 9 10 11 12 L = [ [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ], [ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ] ] a = sum(L[0]) cnt = 0 for x in xrange(1901, 2001): l = L[1] if is_leapyear(x) else L[0] for m in l: if a % 7 == 6: # 0 is Monday, 6 is Sunday cnt += 1 a += m print cnt begin = datetime.datetime.now() euler019() end = datetime.datetime.now() print end - begin
答え: 171
実行時間: 0.000951秒くらい