Created
July 10, 2014 01:46
-
-
Save mtomwing/c78dd6ecc5dd4a1dd4be to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn hailstone(n: uint) -> uint { | |
match n % 2 { | |
0 => n / 2, | |
_ => 3 * n + 1, | |
} | |
} | |
fn hailstone_len(n: uint) -> uint { | |
match n { | |
1 => 0, | |
_ => hailstone_len(hailstone(n)) + 1, | |
} | |
} | |
fn divisors(n: int) -> Vec<int> { | |
let mut ret = Vec::new(); | |
for i in range(2, (n / 2) + 1) { | |
if n % i == 0 { | |
ret.push(i); | |
} | |
} | |
return ret; | |
} | |
fn main() { | |
println!("Hailstone stuff:"); | |
for &n in [11, 27, 686901248, 1].iter() { | |
println!("hailstone_len({}) = {}", n, hailstone_len(n)); | |
} | |
println!(""); | |
println!("Divisor stuff"); | |
for &n in [30, 64, 127].iter() { | |
println!("divisors({}) = {}", n, divisors(n)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment