[Question]
  [
Your challenge today is to output a given term of a sequence enumerating all of the integers. The sequence is as follows: If we have a 0-indexed function generating the sequence `f(n)` and `ceil(x)` is the ceiling function, then `f(0) = 0`; `abs(f(n)) = ceil(n/2)`; `sign(f(n))` is positive when `n` and `ceil(n/2)` are either both even or both odd.
To help understand this sequence, the first few terms are as follows: `0 1 -1 -2 2 3 -3 -4 4 5 -5 -6 6 7 -7...`
Your task is to write a program to that takes an integer `n` and outputs the `n`th term of the sequence. Input may be 0 or 1-indexed only.
Test cases (0-indexed):
```
0  =>  0
1  =>  1
2  => -1
3  => -2
4  =>  2
5  =>  3
```
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), fewest bytes wins!
  
[Answer]
# [Python 2](https://docs.python.org/2/), 26 24 bytes
```
lambda x:-~x/2/(1-(x&2))
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUaHCSreuQt9IX8NQV6NCzUhT839afpFCpkJmnkJRYl56qoahgaYVF2dBUWZeiUaaRqamJtd/AA)
[Answer]
# [SOGL V0.12](https://github.com/dzaima/SOGL), ~~8~~ 6 [bytes](https://github.com/dzaima/SOGL/blob/master/chartable.md)
```
I».»⌡±
```
[Try it Here!](https://dzaima.github.io/SOGLOnline/?code=SSVCQi4lQkIldTIzMjElQjE_,inputs=NA__) or [try the first couple numbers](https://dzaima.github.io/SOGLOnline/?code=SSVCQmUlQkIldTIzMjElQjElMjMlMEEldTIyMkJIJUIzRU8ldTAxM0MlM0ElMjNw,inputs=MTU_) (changed a bit so it'd work)  
0-indexed.
Explanation:
```
I       increment the input
 »      floor divide by 2
  .     push the original input
   »    floor divide by 2
    ⌡   that many times
     ±    negate
```
Or simpler:
```
(input + 1) // 2 negated input // 2 times
        I     »     ±      .     »    ⌡
```
[Answer]
# JavaScript (ES6), 18 bytes
1-indexed.
```
n=>n/(++n&2||-2)|0
```
### Demo
```
let f =
n=>n/(++n&2||-2)|0
for(n = 1; n < 20; n++) {
  console.log(n + ' --> ' + f(n))
}
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 6 bytes
```
HµĊN⁸¡
```
[Try it online!](https://tio.run/##y0rNyan8/9/j0NYjXX6PGnccWvj//39jAA "Jelly – Try It Online")
Uses dzaima's algorithm.
-1 thanks to [Jonathan Allan](https://codegolf.stackexchange.com/users/53748/jonathan-allan).
[Answer]
# C, 25 bytes
```
f(n){return~n/2*~-(n&2);}
```
[Answer]
# [Haskell](https://www.haskell.org/), 26 bytes
```
f x=div(x+1)2*(-1)^div x 2
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P02hwjYls0yjQttQ00hLQ9dQMw7IVahQMPqfm5iZp2CrUFCUmVeioKKQm1igkKYQbaCnZ2gQ@x8A "Haskell – Try It Online")
The other Haskell answers seem to be overcomplicating things… ^^
[Answer]
# [Pyke](https://github.com/muddyfish/PYKE), 6 bytes
```
heQeV_
```
[Try it here!](http://pyke.catbus.co.uk/?code=heQeV_&input=3&warnings=0&hex=0)
Uses [dzaima's approach](https://codegolf.stackexchange.com/a/142894/59487)... ~~Beats~~ Ties Jelly!
# Explanation
```
h      - Increment the input, which is implicit at the beginning.
 e     - Floor halve.
  Q    - Push the input.
   e   - Floor halve.
    V_ - Apply repeatedly (V), ^ times, using negation (_).
       - Output implicitly.
```
The hex-encoded bytes equivalent would be: [`68 65 51 65 56 5F`](http://pyke.catbus.co.uk/?code=68+65+51+65+56+5F&input=3&warnings=0&hex=1).
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 6 bytes
```
;DîsF(
```
[Try it online!](https://tio.run/##MzBNTDJM/f/f2uXwumI3jf//jQE "05AB1E – Try It Online")
Uses dzaima's algorithm.
[Answer]
# [Python 2](https://docs.python.org/2/), 21 bytes
```
lambda x:-x/2*~-(x&2)
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUaHCSrdC30irTlejQs1I839afpFCpkJmnkJRYl56qoahgaaVQkFRZl6JQppGpibXfwA "Python 2 – Try It Online")
[Answer]
# Java 8, 15 bytes
```
n->~n/2*~-(n&2)
```
EDIT: Is Java really the shortest of the non-golfing languages?! o.Ô
**Explanation:**
[Try it here.](https://tio.run/##LU7LDoIwELzzFXsyrQRUEk@IfyAXjsZDLcUUYUtoITEGfr0uYLKP7OxMZmoxish0Cuvy7WUjrIWb0PgNADQ61VdCKsiXcwVAsmUiTwmZqKmsE05LyAEhA4/RdcZDsp8jhruE@3QjdcOzIdKfOxpdQks@rHC9xtf9AYJvJpXpVwudHVPQl@x0phWG/y9A8bFOtbEZXNyR1DXIMKZUfI20hZqCyf8A)
I'll use the table below as reference of what's happening.
1. `~n` is equal to `-n-1`.
2. Since integer division in Java automatically floors on positive integers and ceils on negative integers, `~n/2` will result in the sequence `0,-1,-1,-2,-2,-3,-3,-4,-4,-5,-5,...`
3. `n&2` will result in either `0` or `2`, in the sequence `0,0,2,2,0,0,2,2,0,0,2,...`
4. `~-x` is equal to `(x-1)`, so `~-(n&2)` (`((n&2)-1)`) results in the sequence `-1,-1,1,1,-1,-1,1,1,-1,-1,1,...`
5. Multiplying the two sequences of `~n/2` and `~-(n&2)` gives is the correct sequence asked in the challenge: `0,1,-1,-2,2,3,-3,-4,4,5,-5,...`
Overview table:
```
n       ~n      ~n/2    n&2     ~-(n&2)     ~n/2*~-(n&2)
0       -1      0       0       -1          0
1       -2      -1      0       -1          1
2       -3      -1      2       1           -1
3       -4      -2      2       1           -2
4       -5      -2      0       -1          2
5       -6      -3      0       -1          3
6       -7      -3      2       1           -3
7       -8      -4      2       1           -4
8       -9      -4      0       -1          4
9       -10     -5      0       -1          5
10      -11     -5      2       1           -5
```
[Answer]
# Mathematica, 24 bytes
```
(s=⌈#/2⌉)(-1)^(#+s)&  
```
-14 bytes from @Misha Lavrov
[Answer]
# [Haskell](https://www.haskell.org/), ~~25~~ ~~43~~ 42 bytes
```
((do a<-[0..];[[-a,a],[a,-a]]!!mod a 2)!!)
```
[Try it online!](https://tio.run/##BcFBDoIwEAXQq/wmLiBpm@JWOck4ix@wSmyhAc/P8N6Xx@9diuXxZV03b@AzSIpRHyKBnuqFPlDVubrNIO69c71VLitGtH1Z/7ihsiFDhhiHlNTOKRd@DgtTaxc "Haskell – Try It Online") 1-indexed.
*Edit:* The previous version had the signs in a wrong order, thanks to @Potato44 for pointing out. Fixed for 18 bytes ...
*Edit 2:* Thanks to BMO for -1 byte!
[Answer]
# [Python 3](https://docs.python.org/3/), 29 bytes
```
lambda n:-~n//2*(-1)**(n%4>1)
```
[Try it online!](https://tio.run/##K6gsycjPM/6fZhvzPycxNyklUSHPSrcuT1/fSEtD11BTS0sjT9XEzlDzf1p@kUKmQmaeQlFiXnqqhqGBgaZVQVFmXolGmkampuZ/AA "Python 3 – Try It Online")
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 9 bytes
```
_F/hQ2/Q2
```
**[Try it here!](https://pyth.herokuapp.com/?code=_F%2FhQ2%2FQ2&test_suite=1&test_suite_input=0%0A1%0A2%0A3%0A4%0A5%0A6%0A7%0A8%0A9%0A10%0A11&debug=0)**
Uses [dzaima's approach](https://codegolf.stackexchange.com/a/142894/59487).
[Answer]
## Haskell, 36 bytes
```
g x=x: -x:g(-x-signum x)
((0:g 1)!!)
```
[Try it online!](https://tio.run/##BcFBCoAgEAXQfaf4QgtdGLoVPEm0cJGTpCJZMLef3rvSvM9aRQgcOcByIG3ZzkL9a2Cz5Ki1CwRvlDLSUumIGE/pL1a0NJCxu23z7pAf "Haskell – Try It Online")
[Answer]
## Batch, 29 bytes
```
@cmd/cset/a"%1/2^(%1<<30>>30)
```
[Answer]
## JavaScript (ES6), 18 bytes
```
f=
n=>n/2^(n<<30>>30)
```
```
<input type=number min=0 value=0 oninput=o.textContent=f(this.value)><pre id=o>0
```
0-indexed.
[Answer]
## Javascript, 17 bytes
```
n=>~n/2*~-(n&2)^0
```
```
f=
n=>~n/2*~-(n&2)^0
```
```
<input type=number min=0 value=0 oninput=o.textContent=f(this.value)><pre id=o>0
```
This one is 0 indexed. It's entirely bitwise trickery.
[Answer]
# [Cubically](https://github.com/aaronryank/cubically), 23 bytes
(1-indexed)
```
FDF'$:7+8/0_0*0-8*7/0%6
```
[Try it online!](https://tio.run/##Sy5NykxOzMmp/P/fzcVNXcXKXNtC3yDeQMtA10LLXN9A1ez/fxMA "Cubically – Try It Online")
The main difficulty when writing code in Cubically are:
* There is only 1 write-able variable, and
* Get constants is hard.
So, this solution calculate
```
((((n+1)/2)%2)*2-1)*n/2
```
where `/` denotes integer division. That only need 1 temporary variable, and constants 1 and 2.
Explanation:
```
FDF'$:7+8/0_0*0-8*7/0%6
FDF'                      Set face value of face 0 to 2, and value of memory index 8 (cube is unsolved) to 1 (true = unsolved)
    $                     Read input
     :7                                 input
       +8                                + 1
         /0                        (        ) /2
           _0                     (             ) %2
             *0                  (                  ) *2
               -8                                        -1
                 *7             (                          ) *n
                   /0                                          /2
                     %6   Print
```
[Answer]
# TI-Basic (TI-84 Plus CE), 20 bytes
```
‾int(‾Ans/2)(1-2remainder(int(Ans/2),2
```
A full program that is called like `5:prgmNAME`.
TI-Basic is a [tokenized lanugage](http://tibasicdev.wikidot.com/one-byte-tokens), all tokens used here are one byte, except for `remainder(` which is two. `‾` represents the regative token, which is typed with the `(-)` key.
Examples:
```
0:prgmNAME
 => 0
1:prgmNAME
 => 1
2:prgmNAME
 => -1
#etc
```
Explanation:
```
‾int(‾Ans/2)(1-2remainder(int(Ans/2),2
‾int(‾Ans/2)                           # -int(-X) is ciel(X), so ciel(Ans/2)
                          int(Ans/2)   # int(X) is floor(X), so floor(Ans/2)
                remainder(int(Ans/2),2 # 1 if floor(Ans/2) is odd else 0
            (1-2remainder(int(Ans/2),2 # -1 if floor(Ans/2) is odd, else 1
_int(_Ans/2)(1-2remainder(int(Ans/2),2 # -ciel(Ans/2) if floor(Ans/2) is odd, else ciel(Ans/2)
```
Same formula as a Y-var function:
```
Y1= ‾int(‾X/2)(1-2remainder(int(X/2),2
```
[Answer]
# [dc](https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html), 16 bytes
```
1+d2~+2%2*1-r2/*
```
I am sure there's a way to make 0..1 to -1..1 in dc shorter, but no ideas for now.
[Try it online!](https://tio.run/##S0n@H/3fUDvFqE7bSNVIy1C3yEhf63@sQrGBAle0QkpKnkL0o7ZJsQEKOQYVCgUKxXkKhtoKKQqGBgYKdoYKQHWGXAYKOYYV/wE "dc – Try It Online")
[Answer]
# [Brain-Flak](https://github.com/Flakheads/BrainHack), ~~86~~ ~~74~~ ~~72~~ 70 bytes
```
{({}[()]<({}<>([({})]{(<{}([{}]())>)}{}())<>)>)}{}<>{}{<>([{}])(<>)}<>
```
[Try it online!](https://tio.run/##JYqxDYBADAPXsXvKKItEXzw0ICQKWiuz54Oozj57f@f1nPO4qwRlgMOa5ogGh2BKhHKAdGYX0vyP5kp9156Jtm2qals "Brain-Flak (BrainHack) – Try It Online")
## Explanation
There are two parts to this code. The first part
```
({}[()]<({}<>([({})]{(<{}([{}]())>)}{}())<>)>)}{}
```
does the brawn of the computation. It determines `ceil(n/2)` and whether or not to negate the output.
To explain how it works I will first explain how one would calculate `ceil(n/2)`. This could be done with the following code
```
{({}[()]<({}([{}]()))>)}{}
```
This counts down from **n** each time it performs a not (`([{}]())`) on a counter and adds the counter to a result. Since the counter is zero half the time we only increment every other run starting with the first one.
Now I want to also compute the sign of our results. To do this we start another counter. This counter only changes state if the first counter is off. That way we get the desired pattern. We put these two counters on the off stack to ease with moving them around when the time comes.
Now once we have finished that computation our stack looks like this
```
          parity(n)
ceil(n/2) sign
```
So we need to do some work to get the intended result this second part does it.
```
<>{}{<>([{}])(<>)}<>
```
[Answer]
# [Perl 5](https://www.perl.org/), 32 + 1 (`-p`) = 33 bytes
```
$_='-'x(($_+++($b=0|$_/2))%2).$b
```
[Try it online!](https://tio.run/##K0gtyjH9/18l3lZdV71CQ0MlXltbW0MlydagRiVe30hTU9VIU08l6f9/43/5BSWZ@XnF/3ULAA "Perl 5 – Try It Online")
[Answer]
# [Proton](https://github.com/alexander-liao/proton), 23 bytes
```
n=>-~n//2//(-1)**(n//2)
```
[Try it online!](https://tio.run/##KyjKL8nP@59m@z/P1k63Lk9f30hfX0PXUFNLSwPE0fyfll@kkKlgpWCop2doUM2lAAQFRZl5JRqZOuq6duo6aRqZmppctf8B "Proton – Try It Online")
Port of [Halvard's solution](https://codegolf.stackexchange.com/a/142898/59487).
# [Proton](https://github.com/alexander-liao/proton), 23 bytes
```
n=>-~n//2*(-1)**(n%4>1)
```
[Try it online!](https://tio.run/##KyjKL8nP@59m@z/P1k63Lk9f30hLQ9dQU0tLI0/VxM5Q839afpFCpoKVgqGenqFBNZcCEBQUZeaVaGTqqOvaqeukaWRqanLV/gcA "Proton – Try It Online")
Port of [Leaky's solution](https://codegolf.stackexchange.com/a/142896/59487).
A bit more Protonic, 24 bytes: 
```
n=>-~n//2*(**)(-1,n%4>1)
```
[Answer]
# [QBIC](https://drive.google.com/drive/folders/0B0R1Jgqp8Gg4cVJCZkRkdEthZDQ), ~~27~~ 26 bytes
```
g=(:+1)'\2`~(a-g)%2|?-g\?g
```
## Explanation
```
g=          set worker var 'g' to
(:+1)           our index (plus one for the ceil() bit)
'\2`            integer divided by 2 (the int div needs a code literal: '..`
~(a-g)%2    IF index - temp result is odd (index 2 minus result 1 = 1)
|?-g        THEN PRINT g negated
\?g         ELSE PRINT g
```
[Answer]
# Clojure 122 bytes
Verbose, even when golfed. I'm going for the sympathy vote here... :-)
Golfed:
```
(defn d[n](let[x(int(Math/ceil(/ n 2)))y(cond(or(and(even? n)(even? x))(and(odd? n)(odd? x)))(Math/abs x):else(- 0 x))]y))
```
Ungolfed:
```
(defn dizzy-integer [n]
  (let [x   (int (Math/ceil (/ n 2)))
        y   (cond
                (or (and (even? n) (even? x))
                    (and (odd? n)  (odd? x))) (Math/abs x)
                :else (- 0 x)) ]
    y))
```
[Answer]
# Excel VBA 32-Bit, ~~39~~ 37 Bytes
Anonymous VBE immediate window function that takes input from cell `A1` and outputs to the VBE immediate window
```
?[Sign((-1)^Int(A1/2))*Int((A1+1)/2)]
```
Restricted to 32-Bit as `A^B`is not valid in 64-Bit (`A ^B` is as close as can be achieved)
[Answer]
# [Julia 0.6](http://julialang.org/), 16 bytes
It's just the java solution except I need `÷` for integer division.
```
n->~n÷2*~-(n&2)
```
[Try it online!](https://tio.run/##yyrNyUw0@5@mYKuQlJqemfc/T9euLu/wdiOtOl2NPDUjzf@peSlcDsUZ@eUKaXoaBlaGBpr/AQ "Julia 0.6 – Try It Online")
[Answer]
# [Husk](https://github.com/barbuz/Husk), 9 bytes
```
!m?I_%2İZ
```
[Try it online!](https://tio.run/##yygtzv7/XzHX3jNe1ejIhqj///8bAgA "Husk – Try It Online")
1-indexed.
## Explanation
```
!m?I_%2İZ
       İZ List of all integers (builtin) [0,1,-1,2,-2,...]
 m        map to the following function
  ?  %2   if the number is even,
    _     negate it.
   I      otherwise return it
!         find element at input index
```
# Alternate solution, 9 bytes
```
!⌊½¹¡_⌊½→
```
[Try it online!](https://tio.run/##yygtzv7/X/FRT9ehvYd2HloYD2Y9apv0//9/QwA "Husk – Try It Online")
Uses the same equation as the SOGL answer.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 8 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
z°U&2ªJÑ
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&code=erBVJjKqStE&input=MTc)
]