Skip to content

Instantly share code, notes, and snippets.

@danielsz
Last active January 3, 2016 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielsz/8535599 to your computer and use it in GitHub Desktop.
Save danielsz/8535599 to your computer and use it in GitHub Desktop.
Sophie Germain primes
(require '[clojure.math.numeric-tower :refer [sqrt]])
(defn smallest-divisor [n]
(find-divisor n 2))
(defn divides? [a b]
(= (mod b a) 0))
(defn find-divisor [n test-divisor]
(cond (> (sqrt test-divisor) n) n
(divides? test-divisor n) test-divisor
:else (find-divisor n (+ test-divisor 1))))
(defn prime? [n]
(= n (smallest-divisor n)))
(defn germain? [n]
(prime? (inc (* 2 n))))
(filter (every-pred germain? prime?) (range 2 100))
=> (2 3 5 11 23 29 41 53 83 89)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment