diff options
| author | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2020-09-22 16:12:16 -0700 |
|---|---|---|
| committer | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2020-09-22 20:54:25 -0700 |
| commit | 9dad8eb5c718146e406109092dc12aefc829886f (patch) | |
| tree | 588440ec3b1f410e21de5eb20f5bfc9a7c578373 | |
| parent | 169f373f77d14b506191ab2c2e65b1ee6d8404fc (diff) | |
| download | perlweeklychallenge-club-9dad8eb5c718146e406109092dc12aefc829886f.tar.gz perlweeklychallenge-club-9dad8eb5c718146e406109092dc12aefc829886f.tar.bz2 perlweeklychallenge-club-9dad8eb5c718146e406109092dc12aefc829886f.zip | |
Task 1
| -rw-r--r-- | challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/t1.clj | 25 | ||||
| -rw-r--r-- | challenge-079/tyler-wardhaugh/clojure/test/tw/weekly/c79_test.clj | 7 |
2 files changed, 25 insertions, 7 deletions
diff --git a/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/t1.clj b/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/t1.clj index 5ff02af729..47140780d4 100644 --- a/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/t1.clj +++ b/challenge-079/tyler-wardhaugh/clojure/src/tw/weekly/c79/t1.clj @@ -1,12 +1,29 @@ (ns tw.weekly.c79.t1 - (:require [clojure.pprint :refer [cl-format]]) (:require [clojure.edn :as edn])) -;;; Task description for TASK #1 › -;;; +;;; Task description for TASK #1 › Count Set Bits +;You are given a positive number $N. +; +;Write a script to count the total numbrer of set bits of the binary representations of all numbers from 1 to $N and return $total_count_set_bit % 1000000007. +;;;; +(defn count-bits-to-n + "Count the '1' bits in the binary representation of all the integers [1..n]" + [n] + (let [xf (map #(Integer/bitCount %)) + source (range 1 (inc n))] + (transduce xf + source))) +(def big-num "The large number given in the task description to modulo against" + 1000000007N) + +(defn run + "Count the 1s and modulo" + [n] + (mod (count-bits-to-n n) big-num)) (defn -main + "Run Task 1 with a number N, defaulting to the first one given in the examples" [& args] - ) + (let [N (or (-> args first edn/read-string) 4)] + (format "%d" (biginteger (run N))))) diff --git a/challenge-079/tyler-wardhaugh/clojure/test/tw/weekly/c79_test.clj b/challenge-079/tyler-wardhaugh/clojure/test/tw/weekly/c79_test.clj index 6a3687df9f..30f26ba18c 100644 --- a/challenge-079/tyler-wardhaugh/clojure/test/tw/weekly/c79_test.clj +++ b/challenge-079/tyler-wardhaugh/clojure/test/tw/weekly/c79_test.clj @@ -1,11 +1,12 @@ (ns tw.weekly.c79-test (:require [clojure.test :refer [deftest is testing]] - [tw.weekly.c79.t1 :refer []] [tw.weekly.c79.t2 :refer []])) + [tw.weekly.c79.t1 :refer [run]] (deftest task-1 - (testing "" - )) + (testing "Task 1, Count Set Bits" + (is (= (run 4) 5) + (is (= (run 3) 4))))) (deftest task-2 (testing "" |
