%I #36 Sep 27 2019 19:45:50
%S 0,1,0,2,1,-1,-4,0,3,2,0,4,3,1,-2,2,-3,-9,-16,-8,-17,-7,-18,-6,7,21,6,
%T -10,-27,-45,-26,-46,-25,-47,-24,0,5,4,2,-1,3,-2,-8,-15,-7,-16,-6,-17,
%U -5,8,22,7,-9,-26,-44,-25,-45,-24,-46,-23,1,26,0,6,5,3
%N Table read by rows: row n lists the wrecker ball sequence starting with n, or contains -1 if 0 is never reached..
%C The wrecker ball sequence starting with n is defined by x[1] = n, and while x[k] is nonzero, x[k+1] = x[k] + s(k)*k, where s(k) = sign(x[k]) if the "candidate" x[k] - sign(x[k])*k had occurred earlier as some x[j], j <= k, and s(k) = -sign(x[k]) else. (This means, from x[k] one moves a distance of k towards the direction of 0 if the result did not occur earlier, or else in the opposite direction.) - _M. F. Hasler_, Mar 18 2019
%C A228474(n) + 1 gives the length of row n.
%C It is currently unproved whether all rows are of finite length. In particular, the length of row 11281 is unknown but larger than 32*10^9. - _M. F. Hasler_, Mar 18 2019
%C _Hans Havermann_, running code from _Hugo van der Sanden_, has found that row 11281 has length 3285983871527. - _N. J. A. Sloane_, Mar 22 2019
%H Reinhard Zumkeller, <a href="/A248939/b248939.txt">Rows n = 0..16 of triangle, flattened</a>
%H Gordon Hamilton, <a href="http://www.youtube.com/watch?v=mQdNaofLqVc">Wrecker Ball Sequences</a>, Video, 2013
%H <a href="/index/Rea#Recaman">Index entries for sequences related to Recamán's sequence</a>
%F T(n,0) = n;
%F There are three cases for k > 0:
%F case T(n,k-1) > 0:
%F if T(n,k-1) - k != T(n,m), for all m=0..k-1 then T(n,k) = T(n,k-1) - k, otherwise T(n,k) = T(n,k-1) + k,
%F case T(n,k-1) < 0:
%F if T(n,k-1) + k != T(n,m), for all m=0..k-1 then T(n,k) = T(n,k-1) + k, otherwise T(n,k) = T(n,k-1) - k,
%F case T(n,k-1) = 0:
%F T(n,k) = 0; row ends, i.e., k = A228474(n).
%e 0: 0;
%e 1: 1 0;
%e 2: 2 1 -1 -4 0;
%e 3: 3 2 0;
%e 4: 4 3 1 -2 2 -3 -9 -16 -8 -17 -7 -18 -6 7 21 6 -10 -27 -45
%e -26 -46 -25 -47 -24 0;
%e 5: 5 4 2 -1 3 -2 -8 -15 -7 -16 -6 -17 -5 8 22 7 -9 -26 -44
%e -25 -45 -24 -46 -23 1 26 0;
%e 6: 6 5 3 0;
%e 7: 7 6 4 1 -3 2 -4 3 -5 -14 -24 -13 -1 12 -2 13 29 . . . . . . . 1730 3445 1729 3446 1728 3447 1727 3448 1726 3449 1725 0;
%e 8: 8 7 5 2 -2 3 -3 4 -4 -13 -23 -12 0;
%e 9: 9 8 6 3 -1 4 -2 5 -3 -12 -22 -11 1 14 0.
%t row[0] = 0;
%t row[n_] := Module[{b}, b[0] = n; b[k_] /; b[k-1] > 0 := b[k] = If[AllTrue[ Range[0, k-1], b[k-1] - k != b[#]&], b[k-1] - k, b[k-1] + k]; b[k_] /; b[k-1] < 0 := b[k] = If[AllTrue[Range[0, k-1], b[k-1] + k != b[#]&], b[k-1] + k, b[k-1] - k]; b[k_] /; b[k-1] == 0 := b[k] = 0; Reap[k = 0; While[b[k] != 0, Sow[b[k++]]]; Sow[0]][[2, 1]]];
%t row /@ Range[0, 16] // Flatten (* _Jean-François Alcover_, Sep 27 2019 *)
%o (Haskell) import Data.IntSet (singleton, member, insert)
%o a248939 n k = a248939_tabf !! n !! k
%o a248939_tabf = map a248939_row [0..]
%o a248939_row n = n : wBall 1 n (singleton n) where
%o wBall _ 0 _ = []
%o wBall k x s = y : wBall (k + 1) y (insert y s) where
%o y = x + (if (x - j) `member` s then j else -j)
%o j = k * signum x
%o (PARI) row(n)={my(M=Map(),L=List(),k=0); while(n, k++; listput(L,n); mapput(M, n, 1); my(t=if(n>0, -k, +k)); n+=if(mapisdefined(M, n+t), -t, t)); listput(L,0); Vec(L)}
%o for(n=0, 6, print(row(n))) \\ _Andrew Howroyd_, Mar 01 2018
%o (Python) def A248939_row(n):
%o seen = {n}; orbit = [n]; c = 0
%o while n != 0:
%o ++c; s = c if n>0 else -c; n += s if n-s in seen else -s
%o seen.add(n); orbit.append(n)
%o return orbit # _M. F. Hasler_, Mar 18 2019
%o (C++) #include <bits/stdc++.h>
%o A248939_row(long n) { int c=0, s; for(std::map<long, bool> seen; n; n += seen[n-(s=n>0?c:-c)] ? s:-s) { std::cout<<n<<", "; seen[n]=true; ++c; } std::cout<<0; } // _M. F. Hasler_, Mar 18 2019
%Y Cf. A228474 (row lengths - 1), A248961 (row sums), A248973 (partial sums per row), A248952 (min per row), A248953 (max per row), A001532 (Recamán).
%Y Cf. A248940 (row 7), A248941 (row 17), A248942 (row 20).
%K sign,tabf
%O 0,4
%A _Reinhard Zumkeller_, Oct 17 2014
%E Escape clause added by _N. J. A. Sloane_, Apr 19 2019