-
-
Notifications
You must be signed in to change notification settings - Fork 55
Collatz conjecture #545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Collatz conjecture #545
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3341abe
add collatz-conjecture slug via newly added add-practice-exercise script
TheRealOwenRees 6d4753e
project files setup
TheRealOwenRees 5473b7e
added exercise with tests
TheRealOwenRees 894e080
exercise uuid added
TheRealOwenRees 44b02cd
delete mistakenly added files
TheRealOwenRees 1beea81
remove commented out code in test
TheRealOwenRees 26df8af
ignore node_modules that appear from time to time
TheRealOwenRees 5250a96
test generator template added
TheRealOwenRees 0f5265b
test template added
TheRealOwenRees aa31c68
filter original test cases whose UUID is referenced in reimplements i…
TheRealOwenRees 1bee45a
original test cases referenced by reimplements are deleted
TheRealOwenRees e4a1a9f
Merge branch 'main' into collatz-conjecture
TheRealOwenRees d47631e
does not need a set for filtering UUIDs
TheRealOwenRees 9e2cb7f
formatting fixes
TheRealOwenRees 9918b00
redelete node modules debug file
TheRealOwenRees File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ bin/configlet | |
| .psc-ide-port | ||
|
|
||
| .merlin | ||
| _opam | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
28
exercises/practice/collatz-conjecture/.docs/introduction.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "authors": [ | ||
| "therealowenrees" | ||
| ], | ||
| "files": { | ||
| "solution": [ | ||
| "collatz_conjecture.ml" | ||
| ], | ||
| "test": [ | ||
| "test.ml" | ||
| ], | ||
| "example": [ | ||
| ".meta/example.ml" | ||
| ], | ||
| "editor": [ | ||
| "collatz_conjecture.mli" | ||
| ] | ||
| }, | ||
| "blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.", | ||
| "source": "Wikipedia", | ||
| "source_url": "https://en.wikipedia.org/wiki/Collatz_conjecture" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| let collatz_conjecture n = | ||
| let rec aux n count = | ||
| match n with | ||
| | 1 -> Ok count | ||
| | _ -> | ||
| (match n mod 2 with | ||
| | 0 -> aux (n / 2) (count + 1) | ||
| | _ -> aux (n * 3 + 1) (count + 1) | ||
| ) | ||
| in | ||
| if n < 1 then Error "Only positive integers are allowed" | ||
| else aux n 0 | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| default: clean test | ||
|
|
||
| test: | ||
| dune runtest | ||
|
|
||
| clean: | ||
| dune clean | ||
|
|
||
| .PHONY: clean |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| let collatz_conjecture _ = | ||
| failwith "'collatz_conjecture' is missing" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| val collatz_conjecture : int -> (int, string) result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| (executable | ||
| (name test) | ||
| (libraries base ounit2)) | ||
|
|
||
| (alias | ||
| (name runtest) | ||
| (deps | ||
| (:x test.exe)) | ||
| (action | ||
| (run %{x}))) | ||
|
|
||
| (alias | ||
| (name buildtest) | ||
| (deps | ||
| (:x test.exe))) | ||
|
|
||
| (env | ||
| (dev | ||
| (flags | ||
| (:standard -warn-error -A)))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| (lang dune 1.1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| open OUnit2 | ||
| open Collatz_conjecture | ||
|
|
||
| let option_printer = function | ||
| | Error m -> Printf.sprintf "Error \"%s\"" m | ||
| | Ok n -> Printf.sprintf "Ok %d" n | ||
|
|
||
| let ae exp got _test_ctxt = assert_equal ~printer:option_printer exp got | ||
|
|
||
| let tests = [ | ||
| "zero steps for one" >:: | ||
| ae (Ok 0) (collatz_conjecture (1)); | ||
| "divide if even" >:: | ||
| ae (Ok 4) (collatz_conjecture (16)); | ||
| "even and odd steps" >:: | ||
| ae (Ok 9) (collatz_conjecture (12)); | ||
| "large number of even and odd steps" >:: | ||
| ae (Ok 152) (collatz_conjecture (1000000)); | ||
| "zero is an error" >:: | ||
| ae (Error "Only positive integers are allowed") (collatz_conjecture (0)); | ||
| "negative value is an error" >:: | ||
| ae (Error "Only positive integers are allowed") (collatz_conjecture (-15)); | ||
| ] | ||
|
|
||
| let () = | ||
| run_test_tt_main ("collatz-conjecture tests" >::: tests) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| (lang dune 1.1) | ||
| (version 1.0.1) | ||
| (version 1.0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.