Browse Source

solution to problem 003 in lisp

master
Evan Hemsley 5 years ago
parent
commit
1d2e635f05
  1. 12
      lisp/euler.lisp
  2. 15
      lisp/euler003.lisp

12
lisp/euler.lisp

@ -1,3 +1,13 @@
(defun range (min max) (loop for n from min below max by 1 collect n))
(defun range (min max) (loop for n from min below (+ max 1) by 1 collect n))
(defun sum (list) (apply '+ list))
(defun select (expr list) (remove-if-not expr list))
(defun filter (expr list) (remove-if expr list))
(defun partial (func &rest args1)
(lambda (&rest args2)
(apply func (append args1 args2)))
)
(defun prime (n)
(eq (length (member T (mapcar (lambda (a) (eq (mod n a) 0)) (range 2 (sqrt n))))) 0)
)

15
lisp/euler003.lisp

@ -0,0 +1,15 @@
(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))))
Loading…
Cancel
Save