Skip to content

Instantly share code, notes, and snippets.

Created April 22, 2012 03:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/2446843 to your computer and use it in GitHub Desktop.
Save anonymous/2446843 to your computer and use it in GitHub Desktop.
;; ariarule's solution to Prime Sandwich
;; https://4clojure.com/problem/116
(fn [n]
(letfn [(is-prime? [n]
(if (= 2 n)
true
(if (or (= 1 n) (and (even? n) (> 2)))
false
(not (reduce #(or %1 (integer? (/ n %2))) false (range 3 n))))))]
(if (is-prime? n)
(let [primes-l (filter is-prime? (drop 1 (range)))
prev-prime (last (take-while #(< % n) primes-l))
next-prime (first (drop-while #(>= n %) primes-l))]
(if prev-prime
(= n (/ (+ (last (take-while #(< % n) primes-l))
(first (drop-while #(>= n %) primes-l)))
2))
false))
false)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment