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: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "collatz-conjecture",
"name": "Collatz Conjecture",
"uuid": "dc42f98e-d0fe-4c0f-ab4d-1954cf839ea8",
"practices": [],
"prerequisites": [],
"difficulty": 2
}
]
},
Expand Down
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": [
"quintuple-mallard"
],
"files": {
"solution": [
"collatz-conjecture.nu"
],
"test": [
"tests.nu"
],
"example": [
".meta/example.nu"
]
},
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Collatz_conjecture"
}
17 changes: 17 additions & 0 deletions exercises/practice/collatz-conjecture/.meta/example.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export def steps [number] {
if $number <= 0 {
error make {msg: "Only positive integers are allowed"}
}
mut number = $number
mut steps = 0
while $number != 1 {
$steps += 1
if ($number mod 2) == 1 {
$number *= 3
$number += 1
} else {
$number /= 2
}
}
$steps
}
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"
3 changes: 3 additions & 0 deletions exercises/practice/collatz-conjecture/collatz-conjecture.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export def steps [number] {
error make {msg: "Please implement steps"}
}
18 changes: 18 additions & 0 deletions exercises/practice/collatz-conjecture/tests.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use collatz-conjecture.nu steps
use std/assert
# zero steps for one
assert equal (steps 1) 0
# divide if even
assert equal (steps 16) 4
# even and odd steps
assert equal (steps 12) 9
# large number of even and odd steps
assert equal (steps 1000000) 152
# zero is an error
assert equal (try { steps 0 } catch {|e|
$e.msg
}) "Only positive integers are allowed"
# negative value is an error
assert equal (try { steps (-15) } catch {|e|
$e.msg
}) "Only positive integers are allowed"
3 changes: 1 addition & 2 deletions exercises/practice/resistor-color-duo/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@
},
"blurb": "Convert color codes, as used on resistors, to a numeric value.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1464",
"difficulty": 2
"source_url": "https://github.com/exercism/problem-specifications/issues/1464"
}
3 changes: 1 addition & 2 deletions exercises/practice/resistor-color-trio/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@
},
"blurb": "Convert color codes, as used on resistors, to a human-readable label.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1549",
"difficulty": 4
"source_url": "https://github.com/exercism/problem-specifications/issues/1549"
}
3 changes: 1 addition & 2 deletions exercises/practice/resistor-color/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@
},
"blurb": "Convert a resistor band's color to its numeric representation.",
"source": "Maud de Vries, Erik Schierboom",
"source_url": "https://github.com/exercism/problem-specifications/issues/1458",
"difficulty": 1
"source_url": "https://github.com/exercism/problem-specifications/issues/1458"
}
3 changes: 1 addition & 2 deletions exercises/practice/strain/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@
},
"blurb": "Implement the `keep` and `discard` operation on collections.",
"source": "Conversation with James Edward Gray II",
"source_url": "http://graysoftinc.com/",
"difficulty": 2
"source_url": "http://graysoftinc.com/"
}