Jump to Table of Contents Pop Out Sidebar

Problem 19

More details about this document
Create Date:
Publish Date:
Update Date:
2023-11-06 21:20
Creator:
Emacs 29.2 (Org mode 9.6.15)
License:
This work is licensed under CC BY-SA 4.0

1. Problem

Counting Sundays

You are given the following information, but you may prefer to do some research for yourself.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

周日计数

下列信息是已知的,当然你也不妨自己再验证一下:

在二十世纪(1901 年 1 月 1 日到 2000 年 12 月 31 日)中,有多少个月的 1 号是周日?

2. Solution

同样,没什么好说的:

(defun eu19-leap-p (y)
  (or (and (zerop (% y 4)) (not (zerop (% y 100))))
      (zerop (% y 400))))

(defvar eu19-month
  [31 28 31 30 31 30 31 31 30 31 30 31])
(defvar eu19-leapm
  [31 29 31 30 31 30 31 31 30 31 30 31])

(named-let f ((year 1901) (day 366) (cnt 0))
  (cond
   ((= year 2001) cnt)
   (t (cl-loop for a across (if (eu19-leap-p year) eu19-leapm eu19-month)
	       do (if (= (% day 7) 0)
		      (cl-incf cnt))
	       do (cl-incf day a))
      (f (1+ year) day cnt))))
;; 171