Skip to content

Instantly share code, notes, and snippets.

@bhenry
Created April 29, 2010 21:01
Show Gist options
  • Save bhenry/384234 to your computer and use it in GitHub Desktop.
Save bhenry/384234 to your computer and use it in GitHub Desktop.
;;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