Từ 1/1/1901 đến 31/12/2000 có bao nhiêu ngày chủ nhật là ngày đầu tiên trong tháng.

Problem PE019-Counting-Sundays

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

Đề bài

Từ 1/1/1901 đến 31/12/2000 có bao nhiêu ngày chủ nhật là ngày đầu tiên trong tháng.

Phân tích đề bài:

Để xác định ngày đầu tiên trong tháng có phải là ngày chủ nhật hay không có giải thuật khá hay của Judas nhưng … tôi thấy không cần thiết phải bỏ ra quá nhiều thời gian để làm vậy (đúng hơn là sau khi làm project lịch âm lịch Việt nam xong tôi thấy mệt mỏi với mấy cái “thời gian”).

Với Python hoặc với bất kỳ ngôn ngữ lập trình nào đều có các function so sánh ngày tháng năm. Vậy chúng ta có thể “trâu bò” mà dùng.


import datetime


def count_sundays():
    return sum(1 for y in range(1901, 2001)
               for m in range(1, 13)
               if datetime.date(y, m, 1).weekday() == 6)


if __name__ == "__main__":
    import time
    start = time.time()
    result = count_sundays()
    done = time.time()
    print("Have {} sundays on the 1st of a month from 1901 to 2001".format(result))
    elapsed = done - start
    print("elapsed time: {}s".format(elapsed))


Thời gian chạy bài toán

Have 171 sundays on the 1st of a month from 1901 to 2001
elapsed time: 0.0005667209625244141s

Source-code: PE-019