* collatz-conjecture: update tests to v1.2.0 * collatz-conjecture: fix example to pass tests v1.2.0
18 lines
297 B
Python
18 lines
297 B
Python
def collatz_steps(n):
|
|
if n <= 0:
|
|
raise ValueError("input should be positive")
|
|
|
|
step_count = 0
|
|
while n > 1:
|
|
if is_odd(n):
|
|
n = n * 3 + 1
|
|
else:
|
|
n = n / 2
|
|
step_count += 1
|
|
|
|
return step_count
|
|
|
|
|
|
def is_odd(n):
|
|
return n % 2 == 1
|