Repository: exercism/clojure
Pull request #184 (2017-09-19 20:33:20)

adding collatz-conjecture
It was on the Unimplemented exercises of [exercism.io](http://exercism.io) so i decided to implement it.


Patch:
diff --git a/config.json b/config.json
index c4f2750..7828393 100644
--- a/config.json
+++ b/config.json
@@ -65,6 +65,16 @@
 
       ]
     },
+    {
+      "uuid": "c14ef548-c5f6-43a9-84e2-c7248705fc8e",
+      "slug": "collatz-conjecture",
+      "core": false,
+      "unlocked_by": null,
+      "difficulty": 1,
+      "topics": [
+
+      ]
+    },
     {
       "uuid": "66d97ae9-36ac-47c6-8b9f-e77ce498fc70",
       "slug": "nucleotide-count",
diff --git a/exercises/collatz-conjecture/README.md b/exercises/collatz-conjecture/README.md
new file mode 100644
index 0000000..e6f2703
--- /dev/null
+++ b/exercises/collatz-conjecture/README.md
@@ -0,0 +1,34 @@
+## Collatz Conjecture.
+
+The Collatz Conjecture or 3x+1 problem can be summarized as follows:
+
+Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is
+odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely.
+The conjecture states that no matter which number you start with, you will
+always reach 1 eventually.
+
+Given a number n, return the number of steps required to reach 1.
+
+## Examples
+Starting with n = 12, the steps would be as follows:
+
+0. 12
+1. 6
+2. 3
+3. 10
+4. 5
+5. 16
+6. 8
+7. 4
+8. 2
+9. 1
+
+Resulting in 9 steps. So for input n = 12, the return value would be 9.
+
+## Source
+
+An unsolved problem in mathematics named after mathematician Lothar Collatz
+see more at [collatz conjecture wikipedia]("https://en.wikipedia.org/wiki/3x_%2B_1_problem")
+
+## Submitting Incomplete Solutions
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/exercises/collatz-conjecture/project.clj b/exercises/collatz-conjecture/project.clj
new file mode 100644
index 0000000..572e4d3
--- /dev/null
+++ b/exercises/collatz-conjecture/project.clj
@@ -0,0 +1,4 @@
+(defproject collatz-conjecture "0.1.0-SNAPSHOT"
+  :description "collatz-conjecture exercise"
+  :url "https://github.com/exercism/xclojure/tree/master/exercises/collatz-conjecture"
+  :dependencies [[org.clojure/clojure "1.8.0"]])
diff --git a/exercises/collatz-conjecture/src/collatz_conjecture.clj b/exercises/collatz-conjecture/src/collatz_conjecture.clj
new file mode 100644
index 0000000..20ed80d
--- /dev/null
+++ b/exercises/collatz-conjecture/src/collatz_conjecture.clj
@@ -0,0 +1,3 @@
+(ns collatz-conjecture)
+
+(defn collatz [n])
diff --git a/exercises/collatz-conjecture/src/example.clj b/exercises/collatz-conjecture/src/example.clj
new file mode 100644
index 0000000..3bcb59f
--- /dev/null
+++ b/exercises/collatz-conjecture/src/example.clj
@@ -0,0 +1,13 @@
+(ns collatz-conjecture)
+
+(defn collatz-helper [n]
+  (cond (= 1 n)   1
+        (even? n) (/ n 2)
+        :else     (inc (* 3 n))))
+
+(defn collatz [n]
+  (if (> n 0)
+    (count (take-while #(not= 1 %)
+                       (iterate collatz-helper n)))
+    (throw (IllegalArgumentException.
+             "Just defined for numbers greater than 0."))))


Test patch:
diff --git a/exercises/collatz-conjecture/test/collatz_conjecture_test.clj b/exercises/collatz-conjecture/test/collatz_conjecture_test.clj
new file mode 100644
index 0000000..49ebb92
--- /dev/null
+++ b/exercises/collatz-conjecture/test/collatz_conjecture_test.clj
@@ -0,0 +1,29 @@
+(ns collatz-conjecture-test
+  (require [clojure.test :refer [deftest is testing]]
+           [collatz-conjecture :refer [collatz]]))
+
+(deftest steps-for-1
+  (testing "zero steps for one"
+    (is (= 0 (collatz 1)))))
+
+(deftest steps-for-16
+  (testing "divide if even"
+    (is (= 4 (collatz 16)))))
+
+(deftest steps-for-12
+  (testing "even and odd steps"
+    (is (= 9 (collatz 12)))))
+
+(deftest steps-for-1000000
+  (testing "Large number of even and odd steps"
+    (is (= 152 (collatz 1000000)))))
+
+(deftest steps-for-0
+  (testing "zero is an error"
+    (is (thrown? IllegalArgumentException
+                 (collatz 0)))))
+
+(deftest steps-for-negative
+  (testing "negative value is an error"
+    (is (thrown? IllegalArgumentException
+                 (collatz -15)))))