Skip to content

Instantly share code, notes, and snippets.

@mtomwing
Created July 10, 2014 01:46
Show Gist options
  • Save mtomwing/c78dd6ecc5dd4a1dd4be to your computer and use it in GitHub Desktop.
Save mtomwing/c78dd6ecc5dd4a1dd4be to your computer and use it in GitHub Desktop.
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