Collatz length
Level A. Teaches while, if as an expression and max=.
Start from n (1 ≤ n ≤ 10⁶). If the number is even, halve it; otherwise replace it with 3 times itself plus 1. Stop at 1. Print the number of steps, then the largest value seen on the way, including n itself.
Input: one integer n. Output: two integers on one line.
| Input | Output |
|---|---|
7 | 16 52 |
7 tests · Ctrl+Enter