Skip to content

Instantly share code, notes, and snippets.

@drewstaylor
Last active January 25, 2025 18:55
Show Gist options
  • Save drewstaylor/c4c783f44c258cff89bfb12ec2bdcd68 to your computer and use it in GitHub Desktop.
Save drewstaylor/c4c783f44c258cff89bfb12ec2bdcd68 to your computer and use it in GitHub Desktop.
// Collatz Conjecture
fn even_or_odd(num: &u32) -> u32 { num % 2 }
fn even(num: u32) -> u32 { num / 2 }
fn odd(num: u32) -> u32 { 3 * num + 1 }
fn collatz(mut num: u32) {
while num != 1 {
// println!("Processing: {}", num);
let n_type = even_or_odd(&num);
match n_type {
0 => num = even(num),
1 => num = odd(num),
_ => println!("Failed: {}", n_type),
}
}
// println!("End: {}", num);
assert_eq!(num, 1u32);
}
fn main () {
let start: u32 = 1;
let end: u32 = 100;
for i in start..end + 1 {
// println!("Start: {}", &i);
collatz(i as u32);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment