Created
May 9, 2019 05:05
-
-
Save varokas/ea6f733ccf8783a1104d2b8b55402d03 to your computer and use it in GitHub Desktop.
Primes
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
(ns clojure-kata.prime) | |
(defn divisible [x y] (zero? (mod x y))) | |
(defn is-prime | |
([primes-before n] (not-any? (partial divisible n) primes-before))) | |
(defn next-prime | |
([primes-before n] (first (filter (partial is-prime primes-before) (iterate inc n))))) | |
(defn primes | |
([] (primes[] 2)) | |
([primes-before n] | |
(lazy-seq | |
(let [new-prime (next-prime primes-before n) | |
new-primes-before (conj primes-before new-prime)] | |
(cons new-prime (primes new-primes-before (inc new-prime))))))) | |
; => (take 10 (drop 200 (primes))) | |
; (1229 1231 1237 1249 1259 1277 1279 1283 1289 1291) |
ขอบคุณสำหรับท่า anonymous function ครับ นี่คือสิ่งที่สงสัยว่า ทำไม partial มันเยอะ
เขียนตาม Veerapat Boonvanich
(defn find-prime-numbers
([] (find-prime-numbers (drop 2 (range))))
([prime-numbers]
(lazy-seq (cons (first prime-numbers)
(remove #(zero? (mod % (first prime-numbers)))
(rest prime-numbers))))))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.