fn main() -> i32 { var x: i32 = 27; var y = determine_collatz_number(x); return: y } // Calculate the number of Collatz steps needed to reach 1. // The Collatz conjecture states that this function always terminates. fn determine_collatz_number(start: i32) -> i32 { var x = start; var steps = 0; { if x == 1 goto return; do_collatz_step(&x); steps = steps + 1; loop; } return: steps } // If x is even, divide it by 2. Otherwise calculate 3 * x + 1. // Do this without division or modulo operators (for demonstrative purposes). fn do_collatz_step(x: &i32) { var y = x; { if y == 0 { if y + y == x { x = y; goto end; } y = y + 1; loop; } else if y == 1 { x = 3 * x + 1; goto end; } y = y - 2; loop; } end: }