diff options
| -rw-r--r-- | challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj | 27 | ||||
| -rw-r--r-- | challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj | 8 |
2 files changed, 20 insertions, 15 deletions
diff --git a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj index 300b89c7ff..7e6fb06990 100644 --- a/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj +++ b/challenge-076/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj @@ -7,14 +7,15 @@ (defn find-min-primes-to-sum-brute-force "Find the minimum number of prime numbers required, whose summation gives you the argument provided, by brute force." [n] - (if (p/prime? n) [n] - (as-> n x - (p/take-below x) - (combo/partitions x :min 2 :max 3) - (mapcat identity x) - (filter #(= n (reduce + %)) x) - (sort-by count x) - (first x)))) + (if (p/prime? n) + [n] + (->> n + p/take-below + ((juxt #(combo/selections % 2) #(combo/selections % 3))) + (apply concat) + (filter #(= n (reduce + %))) + (sort-by count) + first))) ; basis for algorithm: ; https://stackoverflow.com/a/35756072 @@ -26,12 +27,12 @@ (p/prime? n) [n] (even? n) (let [f (juxt #(- n %) identity)] (->> (p/primes) (filter #(p/prime? (- n %))) first f)) (and (odd? n) (p/prime? (- n 2))) [2 (- n 2)] - :else (conj 3 (find-min-primes-to-sum (- n 3))))) + :else (conj (find-min-primes-to-sum (- n 3)) 3))) (defn -main "Run Task 1 with a number N, defaulting to the number given in the task example, 9." [& args] - (let [N (or (some-> args first edn/read-string) 9)] - (let [solution (find-min-primes-to-sum N)] - (printf "%d prime number(s) are the minimum number required to sum to %d.\n" (count solution) N) - (println solution)))) + (let [N (or (some-> args first edn/read-string) 9) + solution (find-min-primes-to-sum N)] + (printf "%d prime number(s) are the minimum number required to sum to %d:\n" (count solution) N) + (printf "%d = %s" N (apply str (interpose " + " solution))))) diff --git a/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj b/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj index 003ce81591..d6f01cb433 100644 --- a/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj +++ b/challenge-076/tyler-wardhaugh/clojure/test/tw/weekly/c76_test.clj @@ -6,8 +6,12 @@ (deftest ch-1 (testing "Task 1" - (is (= 2 (count (find-min-primes-to-sum 9)) (count (find-min-primes-to-sum-brute-force 9)))) - (is (= 2 (count (find-min-primes-to-sum 34)) (count (find-min-primes-to-sum-brute-force 34)))))) + (let [n-and-results [[6 2] [9 2] [11 1] [12 2] [51 3]] + fns (juxt second (comp count find-min-primes-to-sum first) (comp count find-min-primes-to-sum-brute-force first)) + results (->> n-and-results + (map fns) + (map #(is (apply = %))))] + (dorun results)))) (deftest ch-2 (testing "Task 2" |
