Sunday, November 30, 2014

Project Euler Problem 23 Common Lisp

Project Euler Problem 23

(defun abundantp (num)
  (< num
     (loop for i from 1 to (floor num 2)
           if (zerop (mod num i))
           sum i)))

(defun abundants ()
  (loop for i from 12 to 28123
        if (abundantp i)
        collect i))

(defun sum-abundants ()
  (let ((abun (abundants)) (tmp 0))
    (reduce #'+
            (remove-duplicates
              (loop for i in abun
                    nconc (loop for j in abun
                                when (>= j i)
                                when (<= (setf tmp (+ i j)) 28123)
                                collect tmp))))))

(defun problem23 ()
  (- (loop for i from 1 to 28123 sum i)
     (sum-abundants)))

No comments:

Post a Comment