 # leonard7e's solution

## to Collatz Conjecture in the Rust Track

Published at May 15 2020 · 0 comments
Instructions
Test suite
Solution

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:

1. 12
2. 6
3. 3
4. 10
5. 5
6. 16
7. 8
8. 4
9. 2
10. 1

Resulting in 9 steps. So for input n = 12, the return value would be 9.

## Rust Installation

Refer to the exercism help page for Rust installation and learning resources.

## Writing the Code

Execute the tests with:

``````\$ 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 all ignored tests without editing the tests source file, use:

``````\$ cargo test -- --ignored
``````

To run a specific test, for example `some_test`, you can use:

``````\$ cargo test some_test
``````

If the specific test is ignored use:

``````\$ cargo test some_test -- --ignored
``````

## Further improvements

After you have solved the exercise, please consider using the additional utilities, described in the installation guide, to further refine your final solution.

To format your solution, inside the solution directory use

``````cargo fmt
``````

To see, if your solution contains some common ineffective use cases, inside the solution directory use

``````cargo clippy --all-targets
``````

## 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 exercism/rust repository on 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.

## Source

An unsolved problem in mathematics named after mathematician Lothar Collatz https://en.wikipedia.org/wiki/3x_%2B_1_problem

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### collatz-conjecture.rs

``````use collatz_conjecture::*;

#[test]
fn test_1() {
assert_eq!(Some(0), collatz(1));
}

#[test]
#[ignore]
fn test_16() {
assert_eq!(Some(4), collatz(16));
}

#[test]
#[ignore]
fn test_12() {
assert_eq!(Some(9), collatz(12));
}

#[test]
#[ignore]
fn test_1000000() {
assert_eq!(Some(152), collatz(1_000_000));
}

#[test]
#[ignore]
fn test_0() {
assert_eq!(None, collatz(0));
}``````
``````fn collatz_rec(n: u64, c: u64) -> Option<u64>{
match n {
n if n == 0 =>
None,
n if n == 1 =>
Some(c),
n if n%2 == 0 =>
collatz_rec(n/2, c+1),
n   =>
collatz_rec(n*3+1, c+1)
}
}

pub fn collatz(n: u64) -> Option<u64> {
collatz_rec(n, 0)
}``````