%I #35 Aug 21 2022 09:27:12
%S 0,1,4,2,131,129,3,5,16,14,12,10,8,6,31,29,27,25,23,99734,7,9,11,13,
%T 15,17,64,62,60,58,56,54,52,50,48,46,44,42,40,38,111,22,20,18,28,30,
%U 32,222,220,218,216,214,212,210,208,206,204,202,200,198,196
%N Term in Recamán's sequence A005132 where n appears for first time, or -1 if n never appears.
%H T. D. Noe, <a href="/A057167/b057167.txt">Table of n, a(n) for n=0..1642</a> (taking a(1355) from A064227)
%H Nick Hobson, <a href="/A057167/a057167.py.txt">Python program for this sequence</a>
%H C. L. Mallows, <a href="/A005132/a005132.jpg">Plot (jpeg) of first 10000 terms of A005132</a>
%H C. L. Mallows, <a href="/A005132/a005132.ps">Plot (postscript) of first 10000 terms of A005132</a>
%H N. J. A. Sloane, <a href="http://neilsloane.com/doc/sg.txt">My favorite integer sequences</a>, in Sequences and their Applications (Proceedings of SETA '98).
%H N. J. A. Sloane, <a href="/A005132/a005132.txt">FORTRAN program for A005132, A057167, A064227, A064228</a>
%H <a href="/index/Rea#Recaman">Index entries for sequences related to Recamán's sequence</a>
%p w := array(1..10000); for j from 1 to 100 do l := 0; for k from 1 to nops(a) do if a[k] = j then l := k; exit; fi; od: w[j] := l; od: s := [seq(w[j],j=1..100)]; # where a is an array formed from sequence A005132
%t A005132 = {0}; Do[If[(r = Last[A005132] - n) <= 0 || MemberQ[ A005132, r], r = r + 2n]; AppendTo[ A005132, r], {n, 1, 10^5}]; a[n_] := If[p = Position[ A005132, n]; p == {}, 0, p[[1, 1]] - 1]; Table[a[n], {n, 1, 60}] (* _Jean-François Alcover_, Jul 18 2012 *)
%o (PARI) first(n) = my(a=vector(n), r=[0]); while(#Set(a)<n, my(i=#r, j=r[i]-i, k=j+(j<1 || setsearch(Set(r), j))*2*i); r=concat(r, [k]); if(k<n && !a[k+1], a[k+1]=i)); a \\ _Iain Fox_, Jul 11 2022
%o (Python)
%o from itertools import count, islice
%o def agen(): # generator of terms
%o an, A005132set, inv, y = 0, {0}, {0: 0}, 0
%o for n in count(1):
%o t = an - n
%o an = t if t >= 0 and t not in A005132set else an + n
%o A005132set.add(an)
%o inv[an] = n
%o while y in inv: yield inv[y]; y += 1
%o print(list(islice(agen(), 61))) # _Michael S. Branicky_, Jul 12 2022
%Y Cf. A005132, A057165, A057166.
%Y Cf. also A187943, A187922.
%K nonn,easy,nice,look
%O 0,3
%A _N. J. A. Sloane_, Sep 14 2000
%E I conjecture a(n) is never -1 - but see A064227, A064228.
%E a(0)=0 added and escape clause value changed to -1 by _N. J. A. Sloane_, May 01 2020