-
-
Save hexagon6/c5be1f13402967869d84188221159142 to your computer and use it in GitHub Desktop.
recursive collatz in rust
This file contains 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 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
theres are missing semicolons on line 4 and line 20. Although they are not mandatory, my linter is complaining :)