summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <sokolyuk@gmail.com>2024-06-01 00:00:51 +0200
committerDimitri Sokolyuk <sokolyuk@gmail.com>2024-06-01 00:00:51 +0200
commitb8b9af351463813dc4469a0744e83afbc302c72e (patch)
treeec6a9b47db05072044efcf729d72488c4b9cabbd
parent4b71b6983ae805d7ea39efe27f570444a2b60128 (diff)
Add collatz-conjecture
-rw-r--r--rust/collatz-conjecture/.exercism/config.json34
-rw-r--r--rust/collatz-conjecture/.exercism/metadata.json1
-rw-r--r--rust/collatz-conjecture/.gitignore8
-rw-r--r--rust/collatz-conjecture/Cargo.toml4
-rw-r--r--rust/collatz-conjecture/HELP.md86
-rw-r--r--rust/collatz-conjecture/README.md59
-rw-r--r--rust/collatz-conjecture/src/lib.rs3
-rw-r--r--rust/collatz-conjecture/tests/collatz-conjecture.rs45
8 files changed, 240 insertions, 0 deletions
diff --git a/rust/collatz-conjecture/.exercism/config.json b/rust/collatz-conjecture/.exercism/config.json
new file mode 100644
index 0000000..f53fbab
--- /dev/null
+++ b/rust/collatz-conjecture/.exercism/config.json
@@ -0,0 +1,34 @@
+{
+ "authors": [
+ "jgilray"
+ ],
+ "contributors": [
+ "coriolinus",
+ "cwhakes",
+ "eddyp",
+ "efx",
+ "ErikSchierboom",
+ "lutostag",
+ "nfiles",
+ "petertseng",
+ "rofrol",
+ "stringparser",
+ "xakon",
+ "ZapAnton"
+ ],
+ "files": {
+ "solution": [
+ "src/lib.rs",
+ "Cargo.toml"
+ ],
+ "test": [
+ "tests/collatz-conjecture.rs"
+ ],
+ "example": [
+ ".meta/example.rs"
+ ]
+ },
+ "blurb": "Calculate the number of steps to reach 1 using the Collatz conjecture.",
+ "source": "An unsolved problem in mathematics named after mathematician Lothar Collatz",
+ "source_url": "https://en.wikipedia.org/wiki/3x_%2B_1_problem"
+}
diff --git a/rust/collatz-conjecture/.exercism/metadata.json b/rust/collatz-conjecture/.exercism/metadata.json
new file mode 100644
index 0000000..9ee1081
--- /dev/null
+++ b/rust/collatz-conjecture/.exercism/metadata.json
@@ -0,0 +1 @@
+{"track":"rust","exercise":"collatz-conjecture","id":"31f0d88d0cdb403293e9479da7ea50ed","url":"https://exercism.org/tracks/rust/exercises/collatz-conjecture","handle":"dim13","is_requester":true,"auto_approve":false} \ No newline at end of file
diff --git a/rust/collatz-conjecture/.gitignore b/rust/collatz-conjecture/.gitignore
new file mode 100644
index 0000000..db7f315
--- /dev/null
+++ b/rust/collatz-conjecture/.gitignore
@@ -0,0 +1,8 @@
+# Generated by Cargo
+# will have compiled files and executables
+/target/
+**/*.rs.bk
+
+# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
+# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
+Cargo.lock
diff --git a/rust/collatz-conjecture/Cargo.toml b/rust/collatz-conjecture/Cargo.toml
new file mode 100644
index 0000000..dd64fa7
--- /dev/null
+++ b/rust/collatz-conjecture/Cargo.toml
@@ -0,0 +1,4 @@
+[package]
+edition = "2021"
+name = "collatz_conjecture"
+version = "1.2.1"
diff --git a/rust/collatz-conjecture/HELP.md b/rust/collatz-conjecture/HELP.md
new file mode 100644
index 0000000..67add76
--- /dev/null
+++ b/rust/collatz-conjecture/HELP.md
@@ -0,0 +1,86 @@
+# Help
+
+## Running the tests
+
+Execute the tests with:
+
+```bash
+$ cargo test
+```
+
+All but the first test have been ignored. After you get the first test to
+pass, open the tests source file which is located in the `tests` directory
+and remove the `#[ignore]` flag from the next test and get the tests to pass
+again. Each separate test is a function with `#[test]` flag above it.
+Continue, until you pass every test.
+
+If you wish to run _only ignored_ tests without editing the tests source file, use:
+
+```bash
+$ cargo test -- --ignored
+```
+
+If you are using Rust 1.51 or later, you can run _all_ tests with
+
+```bash
+$ cargo test -- --include-ignored
+```
+
+To run a specific test, for example `some_test`, you can use:
+
+```bash
+$ cargo test some_test
+```
+
+If the specific test is ignored, use:
+
+```bash
+$ cargo test some_test -- --ignored
+```
+
+To learn more about Rust tests refer to the online [test documentation][rust-tests].
+
+[rust-tests]: https://doc.rust-lang.org/book/ch11-02-running-tests.html
+
+## Submitting your solution
+
+You can submit your solution using the `exercism submit src/lib.rs Cargo.toml` command.
+This command will upload your solution to the Exercism website and print the solution page's URL.
+
+It's possible to submit an incomplete solution which allows you to:
+
+- See how others have completed the exercise
+- Request help from a mentor
+
+## Need to get help?
+
+If you'd like help solving the exercise, check the following pages:
+
+- The [Rust track's documentation](https://exercism.org/docs/tracks/rust)
+- The [Rust track's programming category on the forum](https://forum.exercism.org/c/programming/rust)
+- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
+- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
+
+Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
+
+## Rust Installation
+
+Refer to the [exercism help page][help-page] for Rust installation and learning
+resources.
+
+## Submitting the solution
+
+Generally you should submit all files in which you implemented your solution (`src/lib.rs` in most cases). If you are using any external crates, please consider submitting the `Cargo.toml` file. This will make the review process faster and clearer.
+
+## Feedback, Issues, Pull Requests
+
+The GitHub [track repository][github] is the home for all of the Rust exercises. If you have feedback about an exercise, or want to help implement new exercises, head over there and create an issue. Members of the rust track team are happy to help!
+
+If you want to know more about Exercism, take a look at the [contribution guide].
+
+## Submitting Incomplete Solutions
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
+
+[help-page]: https://exercism.org/tracks/rust/learning
+[github]: https://github.com/exercism/rust
+[contribution guide]: https://exercism.org/docs/community/contributors \ No newline at end of file
diff --git a/rust/collatz-conjecture/README.md b/rust/collatz-conjecture/README.md
new file mode 100644
index 0000000..0f2232a
--- /dev/null
+++ b/rust/collatz-conjecture/README.md
@@ -0,0 +1,59 @@
+# Collatz Conjecture
+
+Welcome to Collatz Conjecture on Exercism's Rust Track.
+If you need help running the tests or submitting your code, check out `HELP.md`.
+
+## Instructions
+
+The Collatz Conjecture or 3x+1 problem can be summarized as follows:
+
+Take any positive integer n.
+If n is even, divide n by 2 to get n / 2.
+If n is odd, multiply n by 3 and add 1 to get 3n + 1.
+Repeat the process indefinitely.
+The conjecture states that no matter which number you start with, you will always reach 1 eventually.
+
+Given a number n, return the number of steps required to reach 1.
+
+## Examples
+
+Starting with n = 12, the steps would be as follows:
+
+0. 12
+1. 6
+2. 3
+3. 10
+4. 5
+5. 16
+6. 8
+7. 4
+8. 2
+9. 1
+
+Resulting in 9 steps.
+So for input n = 12, the return value would be 9.
+
+## Source
+
+### Created by
+
+- @jgilray
+
+### Contributed to by
+
+- @coriolinus
+- @cwhakes
+- @eddyp
+- @efx
+- @ErikSchierboom
+- @lutostag
+- @nfiles
+- @petertseng
+- @rofrol
+- @stringparser
+- @xakon
+- @ZapAnton
+
+### Based on
+
+An unsolved problem in mathematics named after mathematician Lothar Collatz - https://en.wikipedia.org/wiki/3x_%2B_1_problem \ No newline at end of file
diff --git a/rust/collatz-conjecture/src/lib.rs b/rust/collatz-conjecture/src/lib.rs
new file mode 100644
index 0000000..84f1782
--- /dev/null
+++ b/rust/collatz-conjecture/src/lib.rs
@@ -0,0 +1,3 @@
+pub fn collatz(n: u64) -> Option<u64> {
+ todo!("return Some(x) where x is the number of steps required to reach 1 starting with {n}")
+}
diff --git a/rust/collatz-conjecture/tests/collatz-conjecture.rs b/rust/collatz-conjecture/tests/collatz-conjecture.rs
new file mode 100644
index 0000000..9bbced3
--- /dev/null
+++ b/rust/collatz-conjecture/tests/collatz-conjecture.rs
@@ -0,0 +1,45 @@
+use collatz_conjecture::*;
+
+#[test]
+fn zero_steps_for_one() {
+ let output = collatz(1);
+
+ let expected = Some(0);
+ assert_eq!(output, expected);
+}
+
+#[test]
+#[ignore]
+fn divide_if_even() {
+ let output = collatz(16);
+
+ let expected = Some(4);
+ assert_eq!(output, expected);
+}
+
+#[test]
+#[ignore]
+fn even_and_odd_steps() {
+ let output = collatz(12);
+
+ let expected = Some(9);
+ assert_eq!(output, expected);
+}
+
+#[test]
+#[ignore]
+fn large_number_of_even_and_odd_steps() {
+ let output = collatz(1000000);
+
+ let expected = Some(152);
+ assert_eq!(output, expected);
+}
+
+#[test]
+#[ignore]
+fn zero_is_an_error() {
+ let output = collatz(0);
+
+ let expected = None;
+ assert_eq!(output, expected);
+}