Challenge:
Given a positive integer input n, create a vector that follows this pattern:
0 1 0 -1 -2 -1 0 1 2 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 ... ±(n-1) ±n
Or, explained with words: The vector starts at 0
, and makes increments of 1
until it reaches the smallest odd positive integer that isn't part of the sequence, then it makes decrements until it reaches the smallest (in magnitude) even negative integer that isn't part of the sequence. It continues this way until n
is reached. The sequence will end on positive n
if n
is odd, and negative n
if n
is even.
The output format is flexible.
Test cases:
n = 1
0 1
-----------
n = 2
0 1 0 -1 -2
-----------
n = 3
0 1 0 -1 -2 -1 0 1 2 3
-----------
n = 4
0 1 0 -1 -2 -1 0 1 2 3 2 1 0 -1 -2 -3 -4
-----------
n = 5
0 1 0 -1 -2 -1 0 1 2 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0 1 2 3 4 5
You may choose to take the n zero-indexed. n = 1
would then give 0 1 0 -1 -2
.
This is code-golf, so the shortest code in each language wins! Explanations are encouraged as always!