Skip to content

Commit 20c62f0

Browse files
Add collatz conjecture exercise (#545)
This commit also updates the test generator to honor the "reimplements" key in the canonical data. [no important files changed]
1 parent c93e6bd commit 20c62f0

File tree

20 files changed

+220
-51
lines changed

20 files changed

+220
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ bin/configlet
1010
.psc-ide-port
1111

1212
.merlin
13+
_opam

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,14 @@
590590
"practices": [],
591591
"prerequisites": [],
592592
"difficulty": 2
593+
},
594+
{
595+
"slug": "collatz-conjecture",
596+
"name": "Collatz Conjecture",
597+
"uuid": "eedc9471-326b-4498-b763-5b1c8eedca88",
598+
"practices": [],
599+
"prerequisites": [],
600+
"difficulty": 3
593601
}
594602
]
595603
},

exercises/practice/anagram/test.ml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ let tests = [
1010
"no matches" >::
1111
ae [] (anagrams "diaper" ["hello"; "world"; "zombies"; "pants"]);
1212
"detects two anagrams" >::
13-
ae ["stream"; "maters"] (anagrams "master" ["stream"; "pigeon"; "maters"]);
14-
"detects two anagrams" >::
1513
ae ["lemons"; "melons"] (anagrams "solemn" ["lemons"; "cherry"; "melons"]);
1614
"does not detect anagram subsets" >::
1715
ae [] (anagrams "good" ["dog"; "goody"]);
@@ -33,17 +31,13 @@ let tests = [
3331
ae [] (anagrams "go" ["go Go GO"]);
3432
"anagrams must use all letters exactly once" >::
3533
ae [] (anagrams "tapper" ["patter"]);
36-
"words are not anagrams of themselves (case-insensitive)" >::
37-
ae [] (anagrams "BANANA" ["BANANA"; "Banana"; "banana"]);
3834
"words are not anagrams of themselves" >::
3935
ae [] (anagrams "BANANA" ["BANANA"]);
4036
"words are not anagrams of themselves even if letter case is partially different" >::
4137
ae [] (anagrams "BANANA" ["Banana"]);
4238
"words are not anagrams of themselves even if letter case is completely different" >::
4339
ae [] (anagrams "BANANA" ["banana"]);
4440
"words other than themselves can be anagrams" >::
45-
ae ["Silent"] (anagrams "LISTEN" ["Listen"; "Silent"; "LISTEN"]);
46-
"words other than themselves can be anagrams" >::
4741
ae ["Silent"] (anagrams "LISTEN" ["LISTEN"; "Silent"]);
4842
]
4943

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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"authors": [
3+
"therealowenrees"
4+
],
5+
"files": {
6+
"solution": [
7+
"collatz_conjecture.ml"
8+
],
9+
"test": [
10+
"test.ml"
11+
],
12+
"example": [
13+
".meta/example.ml"
14+
],
15+
"editor": [
16+
"collatz_conjecture.mli"
17+
]
18+
},
19+
"blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.",
20+
"source": "Wikipedia",
21+
"source_url": "https://en.wikipedia.org/wiki/Collatz_conjecture"
22+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let collatz_conjecture n =
2+
let rec aux n count =
3+
match n with
4+
| 1 -> Ok count
5+
| _ ->
6+
(match n mod 2 with
7+
| 0 -> aux (n / 2) (count + 1)
8+
| _ -> aux (n * 3 + 1) (count + 1)
9+
)
10+
in
11+
if n < 1 then Error "Only positive integers are allowed"
12+
else aux n 0
13+
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"
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
default: clean test
2+
3+
test:
4+
dune runtest
5+
6+
clean:
7+
dune clean
8+
9+
.PHONY: clean
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let collatz_conjecture _ =
2+
failwith "'collatz_conjecture' is missing"

0 commit comments

Comments
 (0)