Thursday, October 9, 2014

Project Euler Problem 7 Common Lisp

Project Euler Problem 7

(defun prime? (num)
  (flet ((multiple? (a b) (zerop (mod a b))))
    (cond ((evenp num) (= 2 num))
          ((< num 2) nil)
          ((multiple? num 3) (= 3 num))
          ((loop for i from 5 to (isqrt num) by 6
                 never (or (multiple? num i)
                           (multiple? num (+ i 2))))))))


(defun problem7 ()
  (loop for i upfrom 0
        when (prime? i) count i into prime-tally
        when (= prime-tally 10001) return i))

No comments:

Post a Comment