Last active
January 25, 2025 18:55
-
-
Save drewstaylor/c4c783f44c258cff89bfb12ec2bdcd68 to your computer and use it in GitHub Desktop.
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
// 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