Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
*.swp
.DS_Store
bin/configlet
bin/configlet.exe
bin/latest-configlet.tar.gz
bin/latest-configlet.zip
bin/configlet.zip
bin/configlet
canonical-data/
112 changes: 0 additions & 112 deletions canonical-data/armstrong-numbers.json

This file was deleted.

8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "collatz-conjecture",
"name": "Collatz Conjecture",
"uuid": "0c6713d7-d0d8-4724-8c41-cbec5202689f",
"practices": [],
"prerequisites": [],
"difficulty": 2
}
]
},
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/collatz-conjecture/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
3 changes: 3 additions & 0 deletions exercises/practice/collatz-conjecture/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Instructions

Given a positive integer, return the number of steps it takes to reach 1 according to the rules of the Collatz Conjecture.
28 changes: 28 additions & 0 deletions exercises/practice/collatz-conjecture/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Introduction

One evening, you stumbled upon an old notebook filled with cryptic scribbles, as though someone had been obsessively chasing an idea.
On one page, a single question stood out: **Can every number find its way to 1?**
It was tied to something called the **Collatz Conjecture**, a puzzle that has baffled thinkers for decades.

The rules were deceptively simple.
Pick any positive integer.

- If it's even, divide it by 2.
- If it's odd, multiply it by 3 and add 1.

Then, repeat these steps with the result, continuing indefinitely.

Curious, you picked number 12 to test and began the journey:

12 ➜ 6 ➜ 3 ➜ 10 ➜ 5 ➜ 16 ➜ 8 ➜ 4 ➜ 2 ➜ 1

Counting from the second number (6), it took 9 steps to reach 1, and each time the rules repeated, the number kept changing.
At first, the sequence seemed unpredictable — jumping up, down, and all over.
Yet, the conjecture claims that no matter the starting number, we'll always end at 1.

It was fascinating, but also puzzling.
Why does this always seem to work?
Could there be a number where the process breaks down, looping forever or escaping into infinity?
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.

[collatz-prize]: https://mathprize.net/posts/collatz-conjecture/
19 changes: 19 additions & 0 deletions exercises/practice/collatz-conjecture/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"collatz_conjecture.moon"
],
"test": [
"collatz_conjecture_spec.moon"
],
"example": [
".meta/example.moon"
]
},
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Collatz_conjecture"
}
14 changes: 14 additions & 0 deletions exercises/practice/collatz-conjecture/.meta/example.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
collatz_steps = (n) ->
assert n > 0, 'Only positive integers are allowed'

steps = 0
while n > 1
if n & 1 == 0
n >>= 1
else
n = 3 * n + 1
steps += 1

steps

{ steps: collatz_steps }
17 changes: 17 additions & 0 deletions exercises/practice/collatz-conjecture/.meta/spec_generator.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
module_name: 'CollatzConjecture',
generate_test: (case, level) ->
local lines
if type(case.expected) == 'number'
lines = {
"result = CollatzConjecture.steps #{case.input.number}",
"assert.are.equal #{case.expected}, result"
}
else
lines = {
"f = -> CollatzConjecture.steps #{case.input.number}",
"assert.has.errors f, '#{case.expected.error}'"
}

table.concat [indent line, level for line in *lines], '\n'
}
38 changes: 38 additions & 0 deletions exercises/practice/collatz-conjecture/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via 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"
include = false

[2187673d-77d6-4543-975e-66df6c50e2da]
description = "zero is an error"
reimplements = "7d4750e6-def9-4b86-aec7-9f7eb44f95a3"

[c6c795bf-a288-45e9-86a1-841359ad426d]
description = "negative value is an error"
include = false

[ec11f479-56bc-47fd-a434-bcd7a31a7a2e]
description = "negative value is an error"
reimplements = "c6c795bf-a288-45e9-86a1-841359ad426d"
4 changes: 4 additions & 0 deletions exercises/practice/collatz-conjecture/collatz_conjecture.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
steps = (number) ->
error "Implement me!"

{ :steps }
26 changes: 26 additions & 0 deletions exercises/practice/collatz-conjecture/collatz_conjecture_spec.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
CollatzConjecture = require 'collatz_conjecture'

describe 'collatz-conjecture', ->
it 'zero steps for one', ->
result = CollatzConjecture.steps 1
assert.are.equal 0, result

pending 'divide if even', ->
result = CollatzConjecture.steps 16
assert.are.equal 4, result

pending 'even and odd steps', ->
result = CollatzConjecture.steps 12
assert.are.equal 9, result

pending 'large number of even and odd steps', ->
result = CollatzConjecture.steps 1000000
assert.are.equal 152, result

pending 'zero is an error', ->
f = -> CollatzConjecture.steps 0
assert.has.errors f, 'Only positive integers are allowed'

pending 'negative value is an error', ->
f = -> CollatzConjecture.steps -15
assert.has.errors f, 'Only positive integers are allowed'
Loading