Avatar of epequeno

epequeno's solution

to Collatz Conjecture in the Rust Track

Published at Dec 21 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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 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

To learn more about Rust tests refer to the online test documentation

Make sure to read the Modules chapter if you haven't already, it will help you with organizing your files.

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(1000000));
}

#[test]
#[ignore]
fn test_0() {
    assert_eq!(None, collatz(0));
}
pub fn collatz(n: u64) -> Option<u64> {
    if n == 0 {
        return None;
    }
    let mut x = n;
    let mut counter = 0;
    loop {
        if x == 1 {
            break;
        }

        match x % 2 {
            0 => {
                x /= 2;
                counter += 1;
            }
            1 => {
                x = (x * 3) + 1;
                counter += 1;
            }
            _ => (),
        }
    }
    Some(counter)
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?