Sha256: 24e6fb80cf0ded4ce4cd7b1260270993f7375b0e5585f9776f20f4b07567db81

Contents?: true

Size: 451 Bytes

Versions: 65

Compression:

Stored size: 451 Bytes

Contents

pub fn collatz_positive(n: u64) -> u64 {
    if n == 1 {
        0
    } else  {
        1 + match n % 2 {
            0 => collatz_positive(n / 2),
            _ => collatz_positive(n * 3 + 1)
        }
    }
}

// return Ok(x) where x is the number of steps required to reach 1
pub fn collatz(n: u64) -> Result<u64, &'static str> {
    if n < 1 {
        Err("Only positive numbers are allowed")
    } else {
        Ok(collatz_positive(n))
    }
}

Version data entries

65 entries across 65 versions & 1 rubygems

Version Path
trackler-2.2.1.110 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.109 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.108 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.107 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.106 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.105 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.104 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.103 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.102 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.101 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.100 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.99 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.98 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.97 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.96 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.95 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.94 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.93 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.92 tracks/rust/exercises/collatz-conjecture/example.rs
trackler-2.2.1.91 tracks/rust/exercises/collatz-conjecture/example.rs