Algorithmic problemElementary20 XPExercise 8/9

The Collatz sequence

Task

The Collatz sequence starts with a positive whole number n. If the number is even, the next one is its half; if odd, the next is 3 * n + 1. The sequence ends when it reaches 1.

The program reads n, prints the whole sequence (from n down to 1) and then the number of steps needed to reach 1. You may separate the numbers with spaces or print them on separate lines.

What the program should do

For 6: 6 3 10 5 16 8 4 2 1 and 8 steps.

Examples

  • Input

    6

    Expected output

    6 3 10 5 16 8 4 2 1
    8

Rules

  • Use a while loop in this lesson.

After you submit, 3 hidden tests will also check your solution: n = 1, n = 7, n = 27 (111 steps).

Python starts the first time you run code.

Ctrl+Enter runs the program. Tab indents. Press Esc, then Tab, to leave the editor.

Input (stdin)

Each line = one call to input().

Output

Hints

Try it yourself first. Each hint reveals a little more - and lowers the reward a little.

Signed out, hints are free - but no XP is awarded either.

Solution

The solution unlocks after the last hint.