OFFSET
0
EXAMPLE
For n=14, the points visited are 14, 13, 11, 8, 4, -1, 5, -2, 6, -3, 7, -4, -16, -29, -15, 0. The closest of these to zero is -1, which is negative, thus a(14) = sgn(-1) = -1.
PROG
(Python)
#spots - positions in order with possible repetition
#flee - positions from which we move away from zero with possible repetition
#stuck - positions from which we move to a spot already visited with possible repetition
def trip(n):
stucklist = list()
spotsvisited = [n]
leavingspots = list()
turn = 0
forbidden = {n}
while n != 0:
turn += 1
sign = n // abs(n)
st = sign * turn
if n - st not in forbidden:
n = n - st
else:
leavingspots.append(n)
if n + st in forbidden:
stucklist.append(n)
n = n + st
spotsvisited.append(n)
forbidden.add(n)
return {'stuck':stucklist, 'spots':spotsvisited,
'turns':turn, 'flee':leavingspots}
def sgn(x):
if x:
return x//abs(x)
return 0
def maxorzero(x):
if x:
return max(x)
return 0
def minorzero(x):
if x:
return min(x)
return 0
#Actual sequence
def a(n):
d = trip(n)
neg=maxorzero([i for i in d['spots'] if i < 0])
pos=minorzero([i for i in d['spots'] if i > 0])
return (-sgn(neg+pos) + [2, 0][sgn(neg)])*sgn(n)
CROSSREFS
KEYWORD
sign
AUTHOR
David Nacin, Mar 10 2019
STATUS
approved