diff options
| author | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2022-01-08 12:40:06 -0800 |
|---|---|---|
| committer | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2022-01-08 16:52:31 -0800 |
| commit | fb357b920a95257266a0063ad44d0480b66fbff2 (patch) | |
| tree | e88f1c07d17bf08333336ae43e1bcaac3b205715 /challenge-146 | |
| parent | fb0789c3ddec9b910e380206faf65c4510e2c39d (diff) | |
| download | perlweeklychallenge-club-fb357b920a95257266a0063ad44d0480b66fbff2.tar.gz perlweeklychallenge-club-fb357b920a95257266a0063ad44d0480b66fbff2.tar.bz2 perlweeklychallenge-club-fb357b920a95257266a0063ad44d0480b66fbff2.zip | |
Ch146 (Clojure): Task 1
Diffstat (limited to 'challenge-146')
3 files changed, 36 insertions, 0 deletions
diff --git a/challenge-146/tyler-wardhaugh/clojure/src/c146/t1.clj b/challenge-146/tyler-wardhaugh/clojure/src/c146/t1.clj new file mode 100644 index 0000000000..f7e965c92b --- /dev/null +++ b/challenge-146/tyler-wardhaugh/clojure/src/c146/t1.clj @@ -0,0 +1,17 @@ +(ns c146.t1) + +(def TARGET 10001) + +(defn nth-prime + [n] + (let [p (promise) + sieve (fn [n] + (not-any? #(zero? (rem n %)) + (take-while #(<= (* % %) n) @p)))] + (->> @(deliver p (filter sieve (iterate inc 2))) + (drop (dec n)) + first))) + +(defn -main + [& _] + (println (nth-prime TARGET))) diff --git a/challenge-146/tyler-wardhaugh/clojure/src/c146/t1b.clj b/challenge-146/tyler-wardhaugh/clojure/src/c146/t1b.clj new file mode 100644 index 0000000000..0532597b87 --- /dev/null +++ b/challenge-146/tyler-wardhaugh/clojure/src/c146/t1b.clj @@ -0,0 +1,11 @@ +(ns c146.t1b) + +(def TARGET 10001) + +(defn nth-prime + [n] + (nth (iterate #(.nextProbablePrime %) BigInteger/ONE) n)) + +(defn -main + [& _] + (println (nth-prime TARGET))) diff --git a/challenge-146/tyler-wardhaugh/clojure/test/c146/t1_test.clj b/challenge-146/tyler-wardhaugh/clojure/test/c146/t1_test.clj new file mode 100644 index 0000000000..dcfbb9fb6c --- /dev/null +++ b/challenge-146/tyler-wardhaugh/clojure/test/c146/t1_test.clj @@ -0,0 +1,8 @@ +(ns c146.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c146.t1 :as t1] + [c146.t1b :as t1b])) + +(deftest target + (testing "Target identified in task description" + (is (= 104743 (t1/nth-prime t1/TARGET) (t1b/nth-prime t1b/TARGET))))) |
