Skip to content

Commit c799d90

Browse files
authored
Add collatz-conjecture (#206)
1 parent 15e39bd commit c799d90

File tree

7 files changed

+161
-0
lines changed

7 files changed

+161
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@
8282
"prerequisites": [],
8383
"difficulty": 3
8484
},
85+
{
86+
"slug": "collatz-conjecture",
87+
"name": "Collatz Conjecture",
88+
"uuid": "4eed1dde-4e87-4cbf-834e-71da55128215",
89+
"practices": [],
90+
"prerequisites": [],
91+
"difficulty": 2
92+
},
8593
{
8694
"slug": "difference-of-squares",
8795
"name": "Difference of Squares",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Instructions
2+
3+
The Collatz Conjecture or 3x+1 problem can be summarized as follows:
4+
5+
Take any positive integer n.
6+
If n is even, divide n by 2 to get n / 2.
7+
If n is odd, multiply n by 3 and add 1 to get 3n + 1.
8+
Repeat the process indefinitely.
9+
The conjecture states that no matter which number you start with, you will always reach 1 eventually.
10+
11+
Given a number n, return the number of steps required to reach 1.
12+
13+
## Examples
14+
15+
Starting with n = 12, the steps would be as follows:
16+
17+
0. 12
18+
1. 6
19+
2. 3
20+
3. 10
21+
4. 5
22+
5. 16
23+
6. 8
24+
7. 4
25+
8. 2
26+
9. 1
27+
28+
Resulting in 9 steps.
29+
So for input n = 12, the return value would be 9.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"collatz_conjecture.vim"
8+
],
9+
"test": [
10+
"collatz_conjecture.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
14+
]
15+
},
16+
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.",
17+
"source": "An unsolved problem in mathematics named after mathematician Lothar Collatz",
18+
"source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function! Steps(number) abort
2+
if a:number < 1
3+
throw 'Only positive integers are allowed'
4+
endif
5+
6+
let l:step = 0
7+
let l:working = a:number
8+
while l:working != 1
9+
if l:working % 2 == 0
10+
let l:working /= 2
11+
else
12+
let l:working = 3 * l:working + 1
13+
endif
14+
15+
let l:step += 1
16+
endwhile
17+
18+
return l:step
19+
endfunction
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[540a3d51-e7a6-47a5-92a3-4ad1838f0bfd]
13+
description = "zero steps for one"
14+
15+
[3d76a0a6-ea84-444a-821a-f7857c2c1859]
16+
description = "divide if even"
17+
18+
[754dea81-123c-429e-b8bc-db20b05a87b9]
19+
description = "even and odd steps"
20+
21+
[ecfd0210-6f85-44f6-8280-f65534892ff6]
22+
description = "large number of even and odd steps"
23+
24+
[7d4750e6-def9-4b86-aec7-9f7eb44f95a3]
25+
description = "zero is an error"
26+
include = false
27+
28+
[2187673d-77d6-4543-975e-66df6c50e2da]
29+
description = "zero is an error"
30+
reimplements = "7d4750e6-def9-4b86-aec7-9f7eb44f95a3"
31+
32+
[c6c795bf-a288-45e9-86a1-841359ad426d]
33+
description = "negative value is an error"
34+
include = false
35+
36+
[ec11f479-56bc-47fd-a434-bcd7a31a7a2e]
37+
description = "negative value is an error"
38+
reimplements = "c6c795bf-a288-45e9-86a1-841359ad426d"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
Execute (zero steps for one):
3+
let g:number = 1
4+
let g:expected = 0
5+
AssertEqual g:expected, Steps(g:number)
6+
7+
Execute (divide if even):
8+
let g:number = 16
9+
let g:expected = 4
10+
AssertEqual g:expected, Steps(g:number)
11+
12+
Execute (even and odd steps):
13+
let g:number = 12
14+
let g:expected = 9
15+
AssertEqual g:expected, Steps(g:number)
16+
17+
Execute (large number of even and odd steps):
18+
let g:number = 1000000
19+
let g:expected = 152
20+
AssertEqual g:expected, Steps(g:number)
21+
22+
Execute (zero is an error):
23+
let g:number = 0
24+
let g:expected = "Only positive integers are allowed"
25+
AssertThrows call Steps(g:number)
26+
AssertEqual g:expected, g:vader_exception
27+
28+
Execute (negative value is an error):
29+
let g:number = -15
30+
let g:expected = "Only positive integers are allowed"
31+
AssertThrows call Steps(g:number)
32+
AssertEqual g:expected, g:vader_exception
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"
2+
" Returns the number of steps to reach 1 for a given number
3+
" using the Collatz Conjecture or 3x+1 problem.
4+
" Throws an error if input is less than 1.
5+
"
6+
" Example:
7+
"
8+
" :echo Steps(16)
9+
" 4
10+
"
11+
" :echo Steps(-1)
12+
" E605: Exception not caught: Only positive integers are allowed
13+
"
14+
function! Steps(number) abort
15+
" your solution goes here
16+
endfunction

0 commit comments

Comments
 (0)