解答

1. /// Determine the length of the collatz sequence beginning at `n`.
2. fn collatz_length(mut n: i32) -> u32 {
3. let mut len = 1;
4. while n > 1 {
5. n = if n % 2 == 0 { n / 2 } else { 3 * n + 1 };
6. len += 1;
7. }
8. len
9. }

11. #[test]
12. fn test_collatz_length() {
13. assert_eq!(collatz_length(11), 15);
14. }

16. fn main() {
17. println!("Length: {}", collatz_length(11));
18. }