Return the `n`th term of the Recamán's sequence.

```
a(0) = 0;

        a(n-1) - n, if this value is positive and not yet in the sequence
      /
a(n) <
      \
        a(n-1) + n, otherwise
```

___

A video about Recamán's sequence by Numberphile: https://www.youtube.com/watch?v=FGC5TdIiT9U

Difficulty: introductory

Starter code:

def recaman(n):
	

Solutions:
def recaman(n):
    series, last = {0}, 0
    for i in range(1, n+1): 
        test = last - i
        last = last + i if test < 0 or test in series else test
        series.add(last)     

    return last

inseq, seq = {0}, [0]
for n in range(1, 10 ** 6):
    x = seq[-1] - n
    if x < 0 or x in inseq: x += 2*n
    seq.append(x)
    inseq.add(x)

recaman = seq.__getitem__

def recaman(n):
    s = set()
    prev = 0
    curr = 0
    for i in range(1, n + 1):
        curr = prev - i
        if curr <= 0 or curr in s:
            curr = prev + i
        s.add(curr)
        prev = curr
    return curr

buf = [0]

def recaman(n):
    while len(buf)<=n:
        x = buf[-1]; v = x-len(buf)
        buf.append(v if 0<=v and v not in buf else x+len(buf))
    return buf[n]


def recaman(n):
    seq = [0]
    seen = {0}
    for i in range(1, n+1):
        x = seq[-1] - i
        if x < 0 or x in seen:
            x = seq[-1] + i
        seq.append(x)
        seen.add(x)
    return seq[-1]


RECA = [0]
seen = {0}
last = 0

for n in range(1, 30000):
    new = last - n
    if new <= 0 or new in seen:
        new = last + n
    RECA.append(new)
    seen.add(new)
    last = new
del seen


def recaman(n):
    return RECA[n]

def recaman(n):
    k, r, s = 0, 0, 0
    S = {0}
    while n != 0:
        r = k + 1
        if s - r < 0 or s - r in S:
            s += r
        else:
            s -= r
        k += 1
        n -= 1
        S.add(s)
    return s

def recaman(n):
    a, i = 0, 0
    k = {0}
    while i<=n:
        x = [a+i,a-i][(a-i) > 0 and a-i not in k]
        k.add(x)
        a = x
        i += 1
    return x      

def recaman(n):
    seq={0}
    cur=0
    for i in range(1,n+1):
        cur+=[i,-i][cur-i not in seq and cur-i>0]
        seq.add(cur)
    return cur        

R=[0]
for i in range(1,30000):      
    if R[i-1]-i>0 and      R[i-1]-i     not in R:
            R.append(  R[i-1]-i )
    else:
            R.append(   R[i-1]+i )
def recaman(n):
    return R[n]