Created
April 29, 2010 21:01
-
-
Save bhenry/384234 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
;;problem 3 largest prime factor of 600851475143 | |
(defn prime? [x] | |
(if (>= x 2) | |
(zero? | |
(count | |
(filter #(divides? x %) | |
(range 2 (inc (int (sqrt x))))))) | |
false)) | |
(defn get-biggest-prime-factor [x] | |
(if (prime? x) | |
x | |
(let [lower-factors (filter #(zero? (rem x %)) | |
(range 2 (inc (int (sqrt x)))))] | |
(reduce max | |
(concat | |
(filter #(prime? %) lower-factors) | |
(map #(/ x %) (filter #(prime? (/ x %)) lower-factors))))))) | |
(get-biggest-prime-factor 600851475143) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment