exercism/zig/collatz-conjecture/collatz_conjecture.zig

20 lines
461 B
Zig
Raw Normal View History

// Please implement the `ComputationError.IllegalArgument` error.
pub const ComputationError = error{IllegalArgument};
pub fn steps(number: usize) anyerror!usize {
if (number < 1) return ComputationError.IllegalArgument;
var count: usize = 0;
var temp: usize = number;
while (temp > 1) : (count += 1) {
if (temp % 2 == 0) {
temp /= 2;
} else {
temp = temp * 3 + 1;
}
}
return count;
}