Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. frey@garden:~/lein$ cat euler003.clj
  2. #!/usr/bin/clojure
  3.  
  4. (defn divisible? [a b] (= 0 (mod a b)))
  5.  
  6. (defn sqrt-rounded-up [x] (inc (int (Math/sqrt x))))
  7.  
  8. (defn potential-divisors [n] (range (sqrt-rounded-up n) 1 -1))
  9.  
  10. (defn divisors [n] (filter (fn [x] (divisible? n x)) (potential-divisors n)))
  11.  
  12. (defn vector-or [coll] (reduce (fn [a b] (or a b)) false coll))
  13.  
  14. (defn divisible-by-any? [n divisors] (vector-or (map (fn [divisor] (divisible? n divisor)) divisors)))
  15.  
  16. (defn prime? [n] (not (divisible-by-any? n (potential-divisors n))))
  17.  
  18. (println (apply max (filter prime? (divisors 600851475143))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement