diff options
4 files changed, 43 insertions, 3 deletions
diff --git a/challenge-085/tyler-wardhaugh/clojure/deps.edn b/challenge-085/tyler-wardhaugh/clojure/deps.edn index cdbf046c18..71a0090da5 100644 --- a/challenge-085/tyler-wardhaugh/clojure/deps.edn +++ b/challenge-085/tyler-wardhaugh/clojure/deps.edn @@ -1,6 +1,7 @@ {:paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.10.1"} - org.clojure/math.combinatorics {:mvn/version "0.1.6"}} + org.clojure/math.combinatorics {:mvn/version "0.1.6"} + org.clojure/math.numeric-tower {:mvn/version "0.0.4"}} :aliases {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.0.0"}}} diff --git a/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj index 26b82079ac..5e6b7e6dfe 100644 --- a/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj +++ b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/core.clj @@ -1,9 +1,12 @@ (ns tw.weekly.c85.core (:require [tw.weekly.c85.t1 :as t1]) + (:require [tw.weekly.c85.t2 :as t2]) (:gen-class)) (defn -main "Run all tasks" [& _] (println "Task #1:") - (t1/-main)) + (t1/-main) + (println "\nTask #2:") + (t2/-main)) diff --git a/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t2.clj b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t2.clj new file mode 100644 index 0000000000..033f493329 --- /dev/null +++ b/challenge-085/tyler-wardhaugh/clojure/src/tw/weekly/c85/t2.clj @@ -0,0 +1,29 @@ +(ns tw.weekly.c85.t2 + (:require [clojure.edn :as edn]) + (:require [clojure.math.numeric-tower :as math])) + +;;; +; Task description for TASK #2 › Power of Two Integers +;;; + +(defn logN + "Compute the log of x in base y." + [x y] + (/ (Math/log x) (Math/log y))) + +(defn has-power-expr + "Find one or more [a b] such that n = a ^ b where a > 0 and b > 1. If no + expression is possible, nil is returned." + [n] + (let [endpoint (fn [x] (inc (math/floor (logN n x)))) + combos (for [a (range 2 (endpoint 2)) + b (range 2 (endpoint a))] + (math/expt a b))] + (->> combos (filter (partial = n)) first))) + +(defn -main + "Run Task 2 with a positive integer, defaulting to the example given in the task description." + [& args] + (let [N (or (some-> args first edn/read-string) 8) + result (has-power-expr N)] + (println (if result 1 0)))) diff --git a/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj b/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj index 49c495a2cb..381c60475e 100644 --- a/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj +++ b/challenge-085/tyler-wardhaugh/clojure/test/tw/weekly/c85_test.clj @@ -1,6 +1,7 @@ (ns tw.weekly.c85-test (:require [clojure.test :refer [deftest is testing]] - [tw.weekly.c85.t1 :refer [find-triplet-sum]])) + [tw.weekly.c85.t1 :refer [find-triplet-sum]] + [tw.weekly.c85.t2 :refer [has-power-expr]])) (deftest task-1 (testing "Task 1, Triplet Sum" @@ -13,3 +14,9 @@ (is (nil? (find-triplet-sum [1.1 0.1]))) (is (some? (find-triplet-sum [0.2 0.5 0.5 0.5 0.8 0.8]))) (is (some? (find-triplet-sum (concat [0.9 0.8 0.05] (repeat 100 (rand-int 1000)))))))) + +(deftest task-2 + (testing "Task 2, Power of Two Integers" + (is (some? (has-power-expr 8))) + (is (nil? (has-power-expr 15))) + (is (some? (has-power-expr 125))))) |
