diff options
8 files changed, 112 insertions, 33 deletions
diff --git a/challenge-147/tyler-wardhaugh/clojure/README.md b/challenge-147/tyler-wardhaugh/clojure/README.md index ea5ea9467a..6d6c624fdc 100644 --- a/challenge-147/tyler-wardhaugh/clojure/README.md +++ b/challenge-147/tyler-wardhaugh/clojure/README.md @@ -1,37 +1,22 @@ -# c146 +# c147 -The Weekly Challenge — #146 — Tyler Wardhaugh +The Weekly Challenge — #147 — Tyler Wardhaugh ## Usage Clojure ([installation instructions](https://clojure.org/guides/getting_started#_clojure_installer_and_cli_tools)) required for `clojure` commands; Babashka ([installation instructions](https://github.com/babashka/babashka#quickstart)) required for the `bb` commands. -Run Task #1 (implemented with a sieve): +Run Task #1: $ clojure -M:t1 # ... or ... $ bb run task-1 - # Alternatively, to run it via Babashka: - $ bb run task-1-bb +Run Task #2: -Run Task #1b (implemented using JVM's BigInteger library): - - $ clojure -M:t1b - # ... or ... - $ bb run task-1b - - # Alternatively, to run it via Babashka: - $ bb run task-1b-bb - -Run Task #2 with input: - - $ clojure -M:t2 M + $ clojure -M:t2 # ... or ... - $ bb run task-2 M - - # Alternatively, to run it via Babashka: - $ bb run task-2-bb + $ bb run task-2 Run the project's tests (which are samples from the task descriptions): diff --git a/challenge-147/tyler-wardhaugh/clojure/bb.edn b/challenge-147/tyler-wardhaugh/clojure/bb.edn index fdbac4a730..fd8889d50b 100644 --- a/challenge-147/tyler-wardhaugh/clojure/bb.edn +++ b/challenge-147/tyler-wardhaugh/clojure/bb.edn @@ -72,18 +72,12 @@ :task (run-task-clj :t1 *command-line-args*)} task-1-bb {:doc "Run Task 1 (via Babashka)" - :task (run-task-bb :t1 *command-line-args*)} - - task-1b {:doc "Run Task 1 (via clojure)" - :task (run-task-clj :t1b *command-line-args*)} - - task-1b-bb {:doc "Run Task 1 (via Babashka)" - :task (run-task-bb :t1b *command-line-args*)} + :task (bb-no-go :t1 *command-line-args*)} task-2 {:doc "Run Task 2 (via clojure)" :task (run-task-clj :t2 *command-line-args*)} task-2-bb {:doc "Run Task 2 (via Babashka)" - :task (run-task-bb :t2 *command-line-args*)} + :task (bb-no-go :t2 *command-line-args*)} } } diff --git a/challenge-147/tyler-wardhaugh/clojure/build.clj b/challenge-147/tyler-wardhaugh/clojure/build.clj new file mode 100644 index 0000000000..d38810e3b5 --- /dev/null +++ b/challenge-147/tyler-wardhaugh/clojure/build.clj @@ -0,0 +1,19 @@ +(ns build + (:refer-clojure :exclude [test]) + (:require [org.corfield.build :as bb])) + +(def lib 'net.clojars.c147/c147) +(def version "0.1.0-SNAPSHOT") +(def main 'c147.c147) + +(defn test "Run the tests." [opts] + (bb/run-tests opts)) + +(def clean bb/clean) + +(defn ci "Run the CI pipeline of tests (and build the uberjar)." [opts] + (-> opts + (assoc :lib lib :version version :main main) + (bb/run-tests) + (bb/clean) + (bb/uber))) diff --git a/challenge-147/tyler-wardhaugh/clojure/deps.edn b/challenge-147/tyler-wardhaugh/clojure/deps.edn index 247ad7b44d..ebc13b0949 100644 --- a/challenge-147/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-147/tyler-wardhaugh/clojure/deps.edn @@ -1,9 +1,9 @@ {:paths ["src" "resources"] - :deps {org.clojure/clojure {:mvn/version "1.10.3"}} + :deps {org.clojure/clojure {:mvn/version "1.11.0-alpha4"} + com.hypirion/primes {:mvn/version "0.2.2"}} :aliases - {:t1 {:main-opts ["-m" "c146.t1"]} - :t1b {:main-opts ["-m" "c146.t1b"]} - :t2 {:main-opts ["-m" "c146.t2"]} + {:t1 {:main-opts ["-m" "c147.t1"]} + :t2 {:main-opts ["-m" "c147.t2"]} :build {:deps {io.github.seancorfield/build-clj {:git/tag "v0.6.3" :git/sha "9b8e09b" ;; since we're building an app uberjar, we do not diff --git a/challenge-147/tyler-wardhaugh/clojure/src/c147/t1.clj b/challenge-147/tyler-wardhaugh/clojure/src/c147/t1.clj new file mode 100644 index 0000000000..dd9e87c6c0 --- /dev/null +++ b/challenge-147/tyler-wardhaugh/clojure/src/c147/t1.clj @@ -0,0 +1,29 @@ +(ns c147.t1 + (:require [com.hypirion.primes :as p])) + +(def TARGET 20) + +(defn digits->n + [digits] + (parse-long (apply str digits))) + +(defn left-truncatable-prime? + [n] + (let [digits (-> n str seq)] + (when (not-any? #(= \0 %) digits) + (->> (iterate rest digits) + (drop 1) + (take-while seq) + (map digits->n) + (every? p/prime?))))) + +(defn left-truncatable-primes + [n] + (->> (p/primes) + (filter left-truncatable-prime?) + (take n))) + + +(defn -main + [& _] + (println (left-truncatable-primes TARGET))) diff --git a/challenge-147/tyler-wardhaugh/clojure/src/c147/t2.clj b/challenge-147/tyler-wardhaugh/clojure/src/c147/t2.clj new file mode 100644 index 0000000000..6dc9b4ce63 --- /dev/null +++ b/challenge-147/tyler-wardhaugh/clojure/src/c147/t2.clj @@ -0,0 +1,34 @@ +(ns c147.t2 + (:import [java.util Collections])) + +(def MAX 5000) + +; source: https://oeis.org/A000326 +; definition: +; a(0) = 0, a(1) = 1; for n >= 2, a(n) = 2*a(n-1) - a(n-2) + 3 +; Miklos Kristof, Mar 09 2005 +(defn pentagon-numbers + [] + (let [pents (promise) + f (fn [n-2 n-1] (+ (- (* 2 n-1) n-2) 3))] + @(doto pents + (deliver (list* 0 1 (lazy-seq (map f @pents (rest @pents)))))))) + +(defn in? + [n coll] + (pos? (Collections/binarySearch coll n))) + +(defn suitable-pair? + "Are both n+m and |n-m| pentagonal numbers?" + [n m pents] + (and (in? (+ n m) pents) (in? (abs (- n m)) pents) [n m])) + +(defn find-suitable-pair + [] + (let [pents (into [] (comp (drop 1) (take MAX)) (pentagon-numbers))] + (-> (fn [n] (->> pents (some #(suitable-pair? n % pents)))) + (some pents)))) + +(defn -main + [& _] + (println (find-suitable-pair))) diff --git a/challenge-147/tyler-wardhaugh/clojure/test/c147/t1_test.clj b/challenge-147/tyler-wardhaugh/clojure/test/c147/t1_test.clj new file mode 100644 index 0000000000..fc527438d0 --- /dev/null +++ b/challenge-147/tyler-wardhaugh/clojure/test/c147/t1_test.clj @@ -0,0 +1,11 @@ +(ns c147.t1-test + (:require [clojure.test :refer [deftest is testing]] + [c147.t1 :as t1])) + +; source: https://oeis.org/A024785 +(def FIRST-20-LEFT-TRUNCATABLE-PRIMES + [2, 3, 5, 7, 13, 17, 23, 37, 43, 47, 53, 67, 73, 83, 97, 113, 137, 167, 173, 197]) + +(deftest target + (testing "Target identified in task description" + (is (= FIRST-20-LEFT-TRUNCATABLE-PRIMES (t1/left-truncatable-primes t1/TARGET))))) diff --git a/challenge-147/tyler-wardhaugh/clojure/test/c147/t2_test.clj b/challenge-147/tyler-wardhaugh/clojure/test/c147/t2_test.clj new file mode 100644 index 0000000000..0670ed643c --- /dev/null +++ b/challenge-147/tyler-wardhaugh/clojure/test/c147/t2_test.clj @@ -0,0 +1,7 @@ +(ns c147.t2-test + (:require [clojure.test :refer [deftest is testing]] + [c147.t2 :refer [find-suitable-pair]])) + +(deftest target + (testing "Target identified in task description" + (is (= [1560090 7042750] (find-suitable-pair))))) |
