File tree Expand file tree Collapse file tree 3 files changed +65
-0
lines changed
exercises/collatz-conjecture Expand file tree Collapse file tree 3 files changed +65
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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"
You can’t perform that action at this time.
0 commit comments