Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Add collatz-conjecture exercise (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 7, 2023
1 parent e7f2725 commit 1a32636
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,19 @@
],
"difficulty": 2
},
{
"slug": "collatz-conjecture",
"name": "Collatz Conjecture",
"uuid": "99c2ede9-23a8-40a1-a54e-f33fddb7d881",
"practices": [
"numbers"
],
"prerequisites": [
"numbers",
"conditionals"
],
"difficulty": 2
},
{
"slug": "grade-school",
"name": "Grade School",
Expand Down
29 changes: 29 additions & 0 deletions exercises/practice/collatz-conjecture/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 19 additions & 0 deletions exercises/practice/collatz-conjecture/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
12 changes: 12 additions & 0 deletions exercises/practice/collatz-conjecture/.meta/src/example.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(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 (new js/Error "Just defined for numbers greater than 0."))))
21 changes: 21 additions & 0 deletions exercises/practice/collatz-conjecture/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
10 changes: 10 additions & 0 deletions exercises/practice/collatz-conjecture/deps.edn
Original file line number Diff line number Diff line change
@@ -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"]}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(ns collatz-conjecture)

(defn collatz [num] ;; <- arglist goes here
;; your code goes here
)
Original file line number Diff line number Diff line change
@@ -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? js/Error
(collatz 0)))))

(deftest steps-for-negative
(testing "negative value is an error"
(is (thrown? js/Error
(collatz -15)))))

0 comments on commit 1a32636

Please sign in to comment.