TI-Basic (TI-84 Plus CE), 31 bytes
.5(Ans+1+remainder(Ans+1,2)-4not(remainder(Ans,4)))i^(2Ans
TI-Basic is a tokenized language and each token used here is one byte, except remainder(
, which is two.
This uses the 1-indexed version.
Explanation:
There is a pattern that repeats every four numbers. In the 1-indexed version, it is: -(x+1)/2, (x+1)/2, -(x+1)/2, (x-1)/2 for the input value x. This can be represented as a piecewise-defined function.
f(x) = -(x+1)/2 if x ≡ 1 mod 4; (x+1)/2 if x ≡ 2 mod 4; -(x+1)/2 if x ≡ 3 mod 4; (x-1)/2 if x ≡ 0 mod 4
Because the "x ≡ 1 mod 4" and "x ≡ 3 mod 4" parts are the same, we can combine them into "x ≡ 1 mod 2".
Now are piecewise function is:
f(x) = -(x+1)/2 if x ≡ 1 mod 2; (x+2)/2 if x ≡ 2 mod 4; (x-2)/2 if x ≡ 0 mod 4
This is where I start breaking it into actual commands. Since the value is positive for even indexes and negative for odd ones, we can use (-1)^x. However, in TI-Basic i^(2X
(5 bytes) is shorter than (-1)^Ans
(6 bytes). Note that parentheses are required due to order of operations.
Now that we have the way to negate the odd inputs out of the way, we move on to the mods (adding the negating back on later). I made the case of an odd input the default, so we start with .5(Ans+1)
.
To fix the case of even input, just add one to the number in the parentheses, but only when x ≡ 0 mod 2. This could be represented as .5(Ans+1+remainder(Ans+1,2))
or .5(Ans+1+not(remainder(Ans,2)))
, but they have the same byte count, so it doesn't matter which.
To fix the case of multiple-of-4 input, we need to subtract 3 from the number in the parentheses, but also another 1 because all multiples of 4 are even, which would add one from our previous step, so we now have .5(Ans+1+remainder(Ans+1,2)-4not(remainder(Ans,4)))
.
Now, just tack on the sign-determining part to the end to get the full program.