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