collatz_length(n) that returns the number of steps it takes for the Collatz sequence starting at n to reach 1. The rule: if the current value is even, divide by 2; if odd, multiply by 3 and add 1. Count each transformation as one step (reaching 1 itself is not counted as an extra step if n starts at 1, which takes 0 steps).collatz_length(6)
8
6->3->10->5->16->8->4->2->1 is 8 steps.
collatz_length(1)
0
Already at 1, so zero steps are needed.
Use a while loop that continues as long as the current value is not 1.
Increment a step counter on each transformation.