You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1.5 KiB

iterators

Instructions

The Collatz Conjecture or 3x+1 problem can be summarized as follows:

Take any positive integer n.

  • If n is even, you will divide n by 2 to get n / 2.
  • If n is odd, you will 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.

But sometimes the number grow significantly before it reaches 1. This can lead to an integer overflow and makes the algorithm unsolvable within the range of a number in u64. You will not have to worry about that in this exercise.

Given a number n, return the number of steps required to reach 1.

Examples:

Starting with n = 16, the steps would be as follows:

0- 16 1- 8 2- 4 3- 2 4- 1

Resulting in 4 steps. So for input n = 16, the return value would be 4.

Notions

Expected functions


struct Collatz {
    v: u64,
    }

impl Iterator for Collatz {}

pub fn collatz(n: u64) -> Option<u64> {}

Usage

Here is a program to test your function.

use iterators::*;

fn main() {
    println!("{:?}", collatz(4));
    println!("{:?}", collatz(5));
    println!("{:?}", collatz(6));
    println!("{:?}", collatz(7));
    println!("{:?}", collatz(12));
}

And its output:

$ cargo run
Some(2)
Some(5)
Some(8)
Some(16)
Some(9)
$