Created
March 14, 2015 13:30
-
-
Save zaneli/657d7fb4a34cb0486dfb to your computer and use it in GitHub Desktop.
「ClojureでNinety-Nine Lisp Problems(P40, 41)」ブログ用
This file contains hidden or 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
(declare my-primes) | |
(defn my-goldbach | |
"Goldbach's conjecture." | |
[n] | |
(if (or (odd? n) (== n 2)) | |
nil | |
(loop [ps (my-primes n)] | |
(let [[p' & ps'] ps, m (first (drop-while #(> n (+ % p')) ps))] | |
(if (and (not (nil? m)) (== (+ p' m) n)) | |
(list p' m) | |
(recur ps')))))) | |
(defn my-primes | |
[n] | |
(loop [n n, [m & ms] (cons 2 (iterate #(+ 2 %) 3)), ps nil] | |
(if (> m n) | |
(reverse ps) | |
(let [ps' (drop-while #(> (Math/pow % 2) m) ps), | |
ps'' (if (every? #(not= (rem m %) 0) ps') (cons m ps) ps)] | |
(recur n ms ps''))))) |
This file contains hidden or 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
(declare my-primes my-goldbach) | |
(defn my-goldbach-list | |
"A list of Goldbach compositions." | |
([n m] | |
(my-goldbach-list n m 0)) | |
([n m l] | |
(filter (fn [[_ g]] (and (not (nil? g)) (>= (first g) l))) | |
(map #(list % (my-goldbach %)) (range n (inc m)))))) | |
(defn my-goldbach | |
"Goldbach's conjecture." | |
[n] | |
(if (or (odd? n) (== n 2)) | |
nil | |
(loop [ps (my-primes n)] | |
(let [[p' & ps'] ps, m (first (drop-while #(> n (+ % p')) ps))] | |
(if (and (not (nil? m)) (== (+ p' m) n)) | |
(list p' m) | |
(recur ps')))))) | |
(defn my-primes | |
[n] | |
(loop [n n, [m & ms] (cons 2 (iterate #(+ 2 %) 3)), ps nil] | |
(if (> m n) | |
(reverse ps) | |
(let [ps' (drop-while #(> (Math/pow % 2) m) ps), | |
ps'' (if (every? #(not= (rem m %) 0) ps') (cons m ps) ps)] | |
(recur n ms ps''))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment