Collatz Sequence
Given a positive integer n, return its Collatz sequence as one space-separated string. Start with n. While the current value is not 1, divide it by 2 when it…
Given a positive integer `n`, return its Collatz sequence as one space-separated string. Start with `n`. While the current value is not `1`, divide it by `2` when it is even; otherwise replace it with `3 × value + 1`. Include both the starting value and the final `1`.
- `1 <= n <= 1000` - Return every value from `n` through `1`, separated by exactly one space with no leading or trailing spaces.
Example 1: input n = 6; output "6 3 10 5 16 8 4 2 1". Apply the even or odd rule to each current value until the sequence reaches 1.
Example 2: input n = 1; output "1". The starting value is already 1, so the sequence has one value.
Category: Math & Numbers. Topic: math. Difficulty: Easy. Patterns: math.