Skip to content

Commit 42fda2b

Browse files
authored
collatz-conjecture (#7)
1 parent 4e0b3a7 commit 42fda2b

12 files changed

Lines changed: 164 additions & 118 deletions

File tree

.gitignore

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
*.swp
2-
.DS_Store
3-
bin/configlet
41
bin/configlet.exe
5-
bin/latest-configlet.tar.gz
6-
bin/latest-configlet.zip
7-
bin/configlet.zip
2+
bin/configlet
3+
canonical-data/

canonical-data/armstrong-numbers.json

Lines changed: 0 additions & 112 deletions
This file was deleted.

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@
4646
"practices": [],
4747
"prerequisites": [],
4848
"difficulty": 2
49+
},
50+
{
51+
"slug": "collatz-conjecture",
52+
"name": "Collatz Conjecture",
53+
"uuid": "0c6713d7-d0d8-4724-8c41-cbec5202689f",
54+
"practices": [],
55+
"prerequisites": [],
56+
"difficulty": 2
4957
}
5058
]
5159
},
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Instructions
2+
3+
Given a positive integer, return the number of steps it takes to reach 1 according to the rules of the Collatz Conjecture.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Introduction
2+
3+
One evening, you stumbled upon an old notebook filled with cryptic scribbles, as though someone had been obsessively chasing an idea.
4+
On one page, a single question stood out: **Can every number find its way to 1?**
5+
It was tied to something called the **Collatz Conjecture**, a puzzle that has baffled thinkers for decades.
6+
7+
The rules were deceptively simple.
8+
Pick any positive integer.
9+
10+
- If it's even, divide it by 2.
11+
- If it's odd, multiply it by 3 and add 1.
12+
13+
Then, repeat these steps with the result, continuing indefinitely.
14+
15+
Curious, you picked number 12 to test and began the journey:
16+
17+
12 ➜ 6 ➜ 3 ➜ 10 ➜ 5 ➜ 16 ➜ 8 ➜ 4 ➜ 2 ➜ 1
18+
19+
Counting from the second number (6), it took 9 steps to reach 1, and each time the rules repeated, the number kept changing.
20+
At first, the sequence seemed unpredictable — jumping up, down, and all over.
21+
Yet, the conjecture claims that no matter the starting number, we'll always end at 1.
22+
23+
It was fascinating, but also puzzling.
24+
Why does this always seem to work?
25+
Could there be a number where the process breaks down, looping forever or escaping into infinity?
26+
The notebook suggested solving this could reveal something profound — and with it, fame, [fortune][collatz-prize], and a place in history awaits whoever could unlock its secrets.
27+
28+
[collatz-prize]: https://mathprize.net/posts/collatz-conjecture/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"collatz_conjecture.moon"
8+
],
9+
"test": [
10+
"collatz_conjecture_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Collatz_conjecture"
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
collatz_steps = (n) ->
2+
assert n > 0, 'Only positive integers are allowed'
3+
4+
steps = 0
5+
while n > 1
6+
if n & 1 == 0
7+
n >>= 1
8+
else
9+
n = 3 * n + 1
10+
steps += 1
11+
12+
steps
13+
14+
{ steps: collatz_steps }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
module_name: 'CollatzConjecture',
3+
generate_test: (case, level) ->
4+
local lines
5+
if type(case.expected) == 'number'
6+
lines = {
7+
"result = CollatzConjecture.steps #{case.input.number}",
8+
"assert.are.equal #{case.expected}, result"
9+
}
10+
else
11+
lines = {
12+
"f = -> CollatzConjecture.steps #{case.input.number}",
13+
"assert.has.errors f, '#{case.expected.error}'"
14+
}
15+
16+
table.concat [indent line, level for line in *lines], '\n'
17+
}
Lines changed: 38 additions & 0 deletions
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"

0 commit comments

Comments
 (0)