diff options
| author | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2020-09-07 10:31:19 -0700 |
|---|---|---|
| committer | Tyler Wardhaugh <tyler.wardhaugh@gmail.com> | 2020-09-08 08:58:08 -0700 |
| commit | 6897242ba14084a2b9dfec62122aff71c90015c8 (patch) | |
| tree | 9e388ade57fcfbfdad4492019b40dc4b282c1fb2 | |
| parent | 9bff867e95b2e505122617b098fde4fa6362ac6d (diff) | |
| download | perlweeklychallenge-club-6897242ba14084a2b9dfec62122aff71c90015c8.tar.gz perlweeklychallenge-club-6897242ba14084a2b9dfec62122aff71c90015c8.tar.bz2 perlweeklychallenge-club-6897242ba14084a2b9dfec62122aff71c90015c8.zip | |
Task 1
3 files changed, 43 insertions, 2 deletions
diff --git a/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj b/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj new file mode 120000 index 0000000000..924a7a086e --- /dev/null +++ b/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch-1.clj @@ -0,0 +1 @@ +ch_1.clj
\ No newline at end of file diff --git a/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj b/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj new file mode 100644 index 0000000000..4ab609b3d8 --- /dev/null +++ b/challenge-077/tyler-wardhaugh/clojure/src/tw/weekly/ch_1.clj @@ -0,0 +1,39 @@ +(ns tw.weekly.ch-1 + (:require [clojure.edn :as edn]) + (:require [clojure.math.combinatorics :as combo])) + +;;; Task description (as of UPDATE: 2020-09-07 09:00:00) +; You are given a positive integer $N. +; +; Write a script to find out all possible combination of Fibonacci Numbers required to get $N on addition. +; +; You are NOT allowed to repeat a number. Print 0 if none found. +;;; + +; source: [Clojure High Performance Fibonacci – Deque](https://deque.blog/2017/06/01/clojure-high-performance-fibonacci/)] +(defn fibo-lazy-seq + "Generate the nth Fibonacci Number, starting from 0, 1, 1, ..." + [] + (letfn [(fibs [a b] (cons a (lazy-seq (fibs b (+ a b)))))] + (fibs 0N 1N))) + +(defn fibo-sum + "Find all combinations of Fibonacci Numbers that sum to n, returning nil if none are found." + [n] + (let [fibs (drop 2 (take n (fibo-lazy-seq))) + results (->> fibs + combo/subsets + (drop 1) ; remove the empty subset, which is always first + (filter #(= n (reduce + 0N %))))] + (seq results))) + +(defn -main + "Run Task 1 with a number N, defaulting to the number given in the task example, 6." + [& args] + (let [N (or (some-> args first edn/read-string) 6) + failure-result 0] + (if-let [results (fibo-sum N)] + (do + (printf "Found Fibonacci numbers that sum to %d:\n" N) + (dorun (map #(println (apply str (interpose " + " %))) results))) + (printf "No Fibonacci numbers sum to %d:\n%d\n" N failure-result)))) diff --git a/challenge-077/tyler-wardhaugh/clojure/test/tw/weekly/c77_test.clj b/challenge-077/tyler-wardhaugh/clojure/test/tw/weekly/c77_test.clj index 8416dbe995..7ab39d517a 100644 --- a/challenge-077/tyler-wardhaugh/clojure/test/tw/weekly/c77_test.clj +++ b/challenge-077/tyler-wardhaugh/clojure/test/tw/weekly/c77_test.clj @@ -1,12 +1,13 @@ (ns tw.weekly.c77-test (:require [clojure.test :refer :all] [clojure.java.io :as io] - [tw.weekly.ch-1 :refer []] + [tw.weekly.ch-1 :refer [fibo-sum]] [tw.weekly.ch-2 :refer []])) (deftest ch-1 (testing "Task 1" - )) + (is (= #{(list 1N 2N 3N) (list 1N 5N)} (into #{} (fibo-sum 6)))) + (is (= #{(list 1N 3N 5N) (list 1N 8N)} (into #{} (fibo-sum 9)))))) (deftest ch-2 (testing "Task 2" |
