Let's write β

プログラミング中にできたことか、思ったこととか

ICPC2006 ProblemA

ディリクレの算術級数定理

(defun primep (num)
  (loop for i from 2 upto (1- num)
       when (zerop (mod num i))
       return nil
     finally (return t)))

(defun find-prime (a d n)
  (labels
      ((%find-prime (a d num n)
	 (let ((prime-p (primep num)))
	   (if (and (zerop n) prime-p)
	       num
	       (if prime-p
		   (%find-prime a d (+ num d) (1- n))
		   (%find-prime a d (+ num d) n))))))
    (%find-prime a d a (1- n))))

(defun main ()
  (loop
     for a = (read)
     for d = (read)
     for n = (read)
     until (and (zerop a) (zerop d) (zerop n))
     do
       (format t "~A~%" (find-prime a d n))))

末尾再帰をつかった素直な実装。