euler/lisp/euler003.lisp

16 lines
539 B
Common Lisp

(load "euler.lisp")
(defun eratosthenes-helper (expr list-one list-two)
(if (> (length list-one) 0) (eratosthenes-helper expr (filter (partial expr (first list-one)) (rest list-one)) (filter (partial expr (first list-one)) list-two)) list-two)
)
(defun eratosthenes (n)
(eratosthenes-helper (lambda (x a) (and (eq (mod a x) 0) (/= a x))) (range 2 (sqrt n)) (range 2 n))
)
(defun prime-factors (n)
(select (lambda (a) (eq (mod n a) 0)) (eratosthenes (sqrt n)))
)
(defun solution () (first (reverse (prime-factors 600851475143))))