Collatz Steps

The Collatz sequence follows a simple rule: if the number is even, divide it by 2; if it is odd, multiply it by 3 and add 1. Write a function collatz_length that repeats this process until the number becomes 1. Return the number of steps needed to reach 1. For n = 1, there are no steps, so the answer is 0.

Примеры

Ввод
1
Вывод
0
Ввод
2
Вывод
1
Ввод
6
Вывод
8

Нужно реализовать

def collatz_length(n: int) -> int:

Теги

math
PostgreSQLv16

Taispeánfar toradh do chóid anseo

Visible tests
1
Input
1
Expected0
2
Input
2
Expected1
3
Input
6
Expected8
4
Input
7
Expected16
5
Input
27
Expected111
6
Input
16
Expected4
Focus radio
Paused · SomaFM · Fluid