#142EasyLoops· while Loops

Collatz Steps

Start from a positive integer `n` and repeat: if `n` is even, halve it; if it is odd, replace it with `3 * n + 1`. For every starting number anyone has tried, the sequence eventually reaches 1. Write `collatz_steps(n)` that returns `[steps, peak]`: how many steps it takes to reach 1, and the largest value seen along the way (including `n` itself).

Input format

`n`: a positive integer.

Output format

A list `[steps, peak]`.

Examples

Input: n = 6
Output: [8,16]
6, 3, 10, 5, 16, 8, 4, 2, 1: eight steps, and the highest value is 16.
Input: n = 1
Output: [0,1]
Already at 1: no steps, and the peak is 1.

Constraints

  • 1 <= n <= 10^6

Study first

  • #python
  • #loops
  • #while
  • #sequences