Created
January 15, 2014 20:26
-
-
Save wildermuthn/8443888 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; Largest prime factor | |
;;; Problem 3 | |
;;; The prime factors of 13195 are 5, 7, 13 and 29. | |
;;; | |
;;; What is the largest prime factor of the number 600851475143 ? | |
;;; | |
;;; | |
(defun prime-p (x) | |
(cond ((eq 1 x) 1) | |
((eq 2 x) nil) | |
((eq 3 x) 3) | |
(t (if (zerop (mod x 2)) nil | |
(if (zerop (do ((half (floor (/ x 2))) | |
(n 2 (1+ n))) | |
((or (zerop (mod x n)) (eq half n)) (mod x n)))) | |
nil x))))) | |
(defun find-factors (x) | |
(loop for n from 1 to (sqrt x) | |
if (zerop (mod x n)) | |
collect n into f-list | |
finally (return f-list))) | |
(defun reduce-to-primes (lst) | |
(remove-if #'(lambda (x) (if (not x) t nil)) (mapcar #'prime-p lst))) | |
(defun answer () | |
(reduce #'max (reduce-to-primes (find-factors 600851475143)))) ; 6857 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment