diff --git a/config.json b/config.json index 6e429942..5cdbc4d6 100644 --- a/config.json +++ b/config.json @@ -395,6 +395,19 @@ "vectors" ], "difficulty": 3 + }, + { + "slug": "collatz-conjecture", + "name": "Collatz Conjecture", + "uuid": "2bfa722f-7096-4c0c-bd03-715628fbd314", + "practices": [ + "numbers" + ], + "prerequisites": [ + "numbers", + "conditionals" + ], + "difficulty": 2 } ] }, diff --git a/exercises/practice/collatz-conjecture/.docs/instructions.md b/exercises/practice/collatz-conjecture/.docs/instructions.md new file mode 100644 index 00000000..ba060483 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.docs/instructions.md @@ -0,0 +1,29 @@ +# Instructions + +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. diff --git a/exercises/practice/collatz-conjecture/.meta/config.json b/exercises/practice/collatz-conjecture/.meta/config.json new file mode 100644 index 00000000..886e30e4 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "ErikSchierboom" + ], + "files": { + "solution": [ + "src/collatz_conjecture.cljs" + ], + "test": [ + "test/collatz_conjecture_test.cljs" + ], + "example": [ + ".meta/src/example.cljs" + ] + }, + "blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.", + "source": "An unsolved problem in mathematics named after mathematician Lothar Collatz", + "source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem" +} diff --git a/exercises/practice/collatz-conjecture/.meta/src/example.cljs b/exercises/practice/collatz-conjecture/.meta/src/example.cljs new file mode 100644 index 00000000..72ad1c79 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/src/example.cljs @@ -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.")))) diff --git a/exercises/practice/collatz-conjecture/.meta/tests.toml b/exercises/practice/collatz-conjecture/.meta/tests.toml new file mode 100644 index 00000000..04187f60 --- /dev/null +++ b/exercises/practice/collatz-conjecture/.meta/tests.toml @@ -0,0 +1,21 @@ +# This is an auto-generated file. Regular comments will be removed when this +# file is regenerated. Regenerating will not touch any manually added keys, +# so comments can be added in a "comment" key. + +[540a3d51-e7a6-47a5-92a3-4ad1838f0bfd] +description = "zero steps for one" + +[3d76a0a6-ea84-444a-821a-f7857c2c1859] +description = "divide if even" + +[754dea81-123c-429e-b8bc-db20b05a87b9] +description = "even and odd steps" + +[ecfd0210-6f85-44f6-8280-f65534892ff6] +description = "large number of even and odd steps" + +[7d4750e6-def9-4b86-aec7-9f7eb44f95a3] +description = "zero is an error" + +[c6c795bf-a288-45e9-86a1-841359ad426d] +description = "negative value is an error" diff --git a/exercises/practice/collatz-conjecture/deps.edn b/exercises/practice/collatz-conjecture/deps.edn new file mode 100644 index 00000000..5c65da55 --- /dev/null +++ b/exercises/practice/collatz-conjecture/deps.edn @@ -0,0 +1,10 @@ +{:deps + {org.clojure/clojure {:mvn/version "1.10.1"} + org.clojure/clojurescript {:mvn/version "1.10.773"}} + + :aliases + {:test + {:extra-paths ["test"] + :extra-deps + {olical/cljs-test-runner {:mvn/version "3.8.0"}} + :main-opts ["-m" "cljs-test-runner.main"]}}} diff --git a/exercises/practice/collatz-conjecture/src/collatz_conjecture.cljs b/exercises/practice/collatz-conjecture/src/collatz_conjecture.cljs new file mode 100644 index 00000000..e6a862be --- /dev/null +++ b/exercises/practice/collatz-conjecture/src/collatz_conjecture.cljs @@ -0,0 +1,5 @@ +(ns collatz-conjecture) + +(defn collatz [num] ;; <- arglist goes here + ;; your code goes here +) diff --git a/exercises/practice/collatz-conjecture/test/collatz_conjecture_test.cljs b/exercises/practice/collatz-conjecture/test/collatz_conjecture_test.cljs new file mode 100644 index 00000000..d25234f3 --- /dev/null +++ b/exercises/practice/collatz-conjecture/test/collatz_conjecture_test.cljs @@ -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? Throwable + (collatz 0))))) + +(deftest steps-for-negative + (testing "negative value is an error" + (is (thrown? Throwable + (collatz -15)))))