Skip to content

Commit 51f1341

Browse files
kgenglerpetertseng
authored andcommitted
add collatz conjecture exercise (#804)
Closes #803.
1 parent 1bd8529 commit 51f1341

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"exercise": "collatz-conjecture",
3+
"version": "1.0.0",
4+
"cases": [
5+
{
6+
"description": "zero steps for one",
7+
"property": "steps",
8+
"number": 1,
9+
"expected": 0
10+
},
11+
{
12+
"description": "divide if even",
13+
"property": "steps",
14+
"number": 16,
15+
"expected": 4
16+
},
17+
{
18+
"description": "even and odd steps",
19+
"property": "steps",
20+
"number": 12,
21+
"expected": 9
22+
},
23+
{
24+
"description": "zero is an error",
25+
"property": "steps",
26+
"number": 0,
27+
"expected": { "error": "Only positive numbers are allowed" }
28+
},
29+
{
30+
"description": "negative value is an error ",
31+
"property": "steps",
32+
"number": -15,
33+
"expected": { "error": "Only positive numbers are allowed" }
34+
}
35+
]
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
The Collatz Conjecture or 3x+1 problem can be summarized as follows:
2+
3+
Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is
4+
odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely.
5+
The conjecture states that no matter which number you start with, you will
6+
always reach 1 eventually.
7+
8+
Given a number n, return the number of steps required to reach 1.
9+
10+
## Examples
11+
Starting with n = 12, the steps would be as follows:
12+
13+
0. 12
14+
1. 6
15+
2. 3
16+
3. 10
17+
4. 5
18+
5. 16
19+
6. 8
20+
7. 4
21+
8. 2
22+
9. 1
23+
24+
Resulting in 9 steps. So for input n = 12, the return value would be 9.
25+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
blurb: "Calculate the number of steps to reach 1 using the Collatz conjecture"
3+
source: "An unsolved problem in mathematics named after mathematician Lothar Collatz"
4+
source_url: "https://en.wikipedia.org/wiki/3x_%2B_1_problem"

0 commit comments

Comments
 (0)