Loading...
Loading...
Loading...
Start from a number n. If it is even, halve it; if it is odd, triple it and add 1. Repeat until you reach 1. Write a recursive function steps(n) that returns how many moves it takes (0 for n = 1) and print it.
Example
Input
6
Output
8
6, 3, 10, 5, 16, 8, 4, 2, 1 is 8 moves.
Try the problem first. Open one hint at a time.
The base case is n == 1, which needs 0 more steps.
Every other call takes one move and asks steps about the next number.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor