Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 7, 2021 13:12
Show Gist options
  • Save rust-play/45794b6c4827916ddc8e5dda29aa9627 to your computer and use it in GitHub Desktop.
Save rust-play/45794b6c4827916ddc8e5dda29aa9627 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn collatz(num: i64, list: &mut Vec<i64>) {
let exists: bool = list.contains(&num);
match exists {
true => {}
false => {
list.push(num);
let even: bool = num % 2 == 0;
match even {
true => collatz(num / 2, list),
false => collatz(3 * num + 1, list),
}
}
}
}
fn main() {
for x in -42..=42 {
let mut list: Vec<i64> = Vec::new();
collatz(x, &mut list);
println!("collatz of {}: {:?}", x, &mut list)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment