diff options
| -rw-r--r-- | challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/t1.clj | 14 | ||||
| -rw-r--r-- | challenge-136/tyler-wardhaugh/clojure/test/tw/weekly/c136/t1_test.clj | 6 |
2 files changed, 17 insertions, 3 deletions
diff --git a/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/t1.clj b/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/t1.clj index 1dfc2ecfb5..dddde5d12f 100644 --- a/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/t1.clj +++ b/challenge-136/tyler-wardhaugh/clojure/src/tw/weekly/c136/t1.clj @@ -6,9 +6,21 @@ ;;; (def DEFAULT-INPUT [8 24]) +(defn- power-of-2? + "Assuming n is a positive BigInteger, is it a power of 2?" + [n] + (= (.getLowestSetBit n) (dec (.bitLength n)))) + +(defn two-friendly? + "Determine if two positive integers m and n are two friendly, that is: + gcd(m, n) = 2^p where p > 0." + [m n] + (when-let [gcd (.gcd (biginteger m) (biginteger n))] + (and (< 1 gcd) (power-of-2? gcd)))) + (defn -main "Run Task 1 with a given input M and N, defaulting to the first example from the task description." [& args] (let [[M N] (or (some->> args (map edn/read-string)) DEFAULT-INPUT)] - )) + (println (if (two-friendly? M N) 1 0)))) diff --git a/challenge-136/tyler-wardhaugh/clojure/test/tw/weekly/c136/t1_test.clj b/challenge-136/tyler-wardhaugh/clojure/test/tw/weekly/c136/t1_test.clj index c223c2e4fa..cf5b6ea6e0 100644 --- a/challenge-136/tyler-wardhaugh/clojure/test/tw/weekly/c136/t1_test.clj +++ b/challenge-136/tyler-wardhaugh/clojure/test/tw/weekly/c136/t1_test.clj @@ -1,7 +1,9 @@ (ns tw.weekly.c136.t1-test (:require [clojure.test :refer [deftest is testing]] - [tw.weekly.c136.t1 :refer []])) + [tw.weekly.c136.t1 :refer [two-friendly?]])) (deftest examples (testing "Examples from description" - )) + (is (true? (two-friendly? 8 24))) + (is (false? (two-friendly? 26 39))) + (is (true? (two-friendly? 4 10))))) |
