---
minutes: 30
---
# Exercise: Collatz Sequence
The [Collatz Sequence](https://en.wikipedia.org/wiki/Collatz_conjecture) is
defined as follows, for an arbitrary n1 greater than zero:
- If _ni_ is 1, then the sequence terminates at _ni_.
- If _ni_ is even, then _ni+1 = ni / 2_.
- If _ni_ is odd, then _ni+1 = 3 * ni + 1_.
For example, beginning with _n1_ = 3:
- 3 is odd, so _n2_ = 3 * 3 + 1 = 10;
- 10 is even, so _n3_ = 10 / 2 = 5;
- 5 is odd, so _n4_ = 3 * 15 + 1 = 16;
- 16 is even, so _n5_ = 16 / 2 = 8;
- 8 is even, so _n6_ = 8 / 2 = 4;
- 4 is even, so _n7_ = 4 / 2 = 2;
- 2 is even, so _n8_ = 1; and
- the sequence terminates.
Write a function to calculate the length of the collatz sequence for a given
initial `n`.
```rust,should_panic
{{#include exercise.rs:collatz_length}}
todo!("Implement this")
}
{{#include exercise.rs:main}}
todo!("Implement this")
}
```