%I #29 Dec 22 2021 02:18:34
%S 1,3,6,2,7,13,20,12,21,11,22,10,23,9,24,8,25,43,62,42,63,41,18,44,19,
%T 45,72,100,71,101,70,38,5,39,4,40,77,115,76,36,78,120,163,119,74,28,
%U 75,27,79,29,80,132,185,131,186,130,73,15,81,141,202
%N Variation (4) on Recamán's sequence (A005132): to get a(n), we first try to subtract n from a(n-1): a(n) = a(n-1) - n if positive and not already in the sequence; if not then we try to add n: a(n) = a(n-1) + n if not already in the sequence; if this fails we try to subtract n+1 from a(n-1), or to add n+1 to a(n-1), or to subtract n+2, or to add n+2, etc., until one of these produces a positive number not already in the sequence - this is a(n).
%C This is the nicest of these variations. Is this a permutation of the natural numbers?
%C The number of steps before n appears is the inverse series, A078758. The height of n is in A126712.
%C See A078758 for the inverse permutation (in case this is a permutation of the positive integers). - _M. F. Hasler_, Nov 03 2014
%C After 10^12 terms, the smallest number which has not appeared is 5191516. - _Benjamin Chaffin_, Oct 09 2016
%D Suggested by J. C. Lagarias.
%H Nick Hobson, <a href="/A064389/b064389.txt">Table of n, a(n) for n = 1..10000</a>
%H Nick Hobson, <a href="/A064389/a064389.py.txt">Python program for this sequence</a>
%H <a href="/index/Rea#Recaman">Index entries for sequences related to Recamán's sequence</a>
%H <a href="/index/Per#IntegerPermutation">Index entries for sequences that are permutations of the natural numbers</a>
%p h := array(1..100000); maxt := 100000; a := array(1..1000); a[1] := 1; h[1] := 1; for nx from 2 to 1000 do for i from 0 to 100 do t1 := a[nx-1]-nx-i; if t1>0 and h[t1] <> 1 then a[nx] := t1; if t1 < maxt then h[t1] := 1; fi; break; fi; t1 := a[nx-1]+nx+i; if h[t1] <> 1 then a[nx] := t1; if t1 < maxt then h[t1] := 1; fi; break; fi; od; od; evalm(a);
%t h[1] = 1; h[_] = 0; maxt = 100000; a[1] = 1; a[_] = 0; For[nx = 2, nx <= 1000, nx++, For[i = 0, i <= 100, i++, t1 = a[nx - 1] - nx - i; If[t1 > 0 && h[t1] != 1, a[nx] = t1; If[t1 < maxt, h[t1] = 1]; Break[]]; t1 = a[nx - 1] + nx + i; If[h[t1] != 1, a[nx] = t1; If[t1 < maxt, h[t1] = 1]; Break[]]]]; Table[a[n], {n, 1, 100}](* _Jean-François Alcover_, May 09 2012, after Maple *)
%o (PARI) A064389(n=1000,show=0)={ my(k,s,t); for(n=1,n, k=n; while( !(t>k && !bittest(s, t-k) && t-=k) && !(!bittest(s, t+k) && t+=k), k++); s=bitor(s,1<<t); show&&print1(t","));t} \\ _M. F. Hasler_, Nov 03 2014
%Y Cf. A005132, A046901, A064387, A064388. Agrees with A064387 for first 187 terms, then diverges.
%K nonn,easy,look,nice
%O 1,2
%A _N. J. A. Sloane_, Sep 28 2001