This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add collatz-conjecture exercise (#118)
- Loading branch information
1 parent
e7f2725
commit 1a32636
Showing
8 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
exercises/practice/collatz-conjecture/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
exercises/practice/collatz-conjecture/.meta/src/example.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.")))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]}}} |
5 changes: 5 additions & 0 deletions
5
exercises/practice/collatz-conjecture/src/collatz_conjecture.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
29 changes: 29 additions & 0 deletions
29
exercises/practice/collatz-conjecture/test/collatz_conjecture_test.cljs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))))) |