Common Lisp

;; ユーティリティ
(defun find-num (x num)
  (unless (zerop num)
    (if (= (mod num 10) x)
	t
	(find-num x (truncate num 10)))))

(defun divisible-p (num div)
  (zerop (mod num div)))

(defun say (x)
  (format t "~a~%" x))

(defun make-cycle (lst)
  (let ((backup-list (copy-list lst)))
    #'(lambda ()
	(or (pop lst)
	    (and (setq lst backup-list) nil)))))

;; ここからナベアツ部分
(defun aho-p (n)
  (or (divisible-p n 3)
      (find-num 3 n)))

(defun make-nabeatsu (max)
  (loop for n from 1 to max
     collect (if (aho-p n) 'aho n)))

(defun make-nabeatsu-closure (max)
  (make-cycle (make-nabeatsu max)))

(defun nabeatsu (max)
  (let ((c (make-nabeatsu-closure max)))
    (loop (let ((x (funcall c)))
	    (if x (say x) (return))))))

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2023-02-23 (木) 23:33:35