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.
 
 
 
 
 
 
Michele Sessa 28bccd6e59 1460-DEV-3511 feat(iterators): improve consistency and clarity 2 years ago
..
README.md 1460-DEV-3511 feat(iterators): improve consistency and clarity 2 years ago

README.md

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:

  1. 16
  2. 8
  3. 4
  4. 2
  5. 1

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

Notions

Expected functions

#[derive(Copy, Clone)]
pub struct Collatz {
    pub v: u64,
}

impl Iterator for Collatz {}

impl Collatz {
	pub fn new(n: u64) -> Self {}
}

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

Usage

Here is a program to test your function.

use iterators::*;

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

And its output:

$ cargo run
0
0
2
5
8
16
9
$