Euler problem 21.1

(defmacro sum (lst)
  `(reduce #'+ ,lst))

(defun proper-divisors (n)
  (when (> n 1)  
    (let ((divisors '())
          (limit (floor (sqrt n))))  
      (loop :for i :from 1 :to limit
            :when (zerop (mod n i))  
              :do (progn
                    (push i divisors)  
                    (when (/= n (floor n i))  
                      (push (floor n i)
                            divisors))))  
      (remove-duplicates (sort divisors #'<)
                         :test
                         #'equal))))

(defmacro sum-proper-divisors (n)
  `(sum (proper-divisors ,n)))

(defun amicable-numbers (limit)
  (let ((amicable-pairs '()))
    (loop :for a :from 2 :below limit
          :do (let* ((b (sum-proper-divisors a))
                     (c (sum-proper-divisors b)))
                (when (and (or (< b a)
                               (>= b limit))
                           (= a c))                      
                  (push a amicable-pairs)                 
                  (when (< b limit)
                    (push b amicable-pairs)))))
    (remove-duplicates amicable-pairs
                       :test
                       #'equal)))

(sum (amicable-numbers 10000))

 
CL-USER> (sum (amicable-numbers 10000))
31626
CL-USER> 

— Me@2024-08-06 03:47:01 PM

.

.

2024.08.06 Tuesday (c) All rights reserved by ACHK