This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.
%I A005132 M2511 #376 Jun 23 2025 17:10:05 %S A005132 0,1,3,6,2,7,13,20,12,21,11,22,10,23,9,24,8,25,43,62,42,63,41,18,42, %T A005132 17,43,16,44,15,45,14,46,79,113,78,114,77,39,78,38,79,37,80,36,81,35, %U A005132 82,34,83,33,84,32,85,31,86,30,87,29,88,28,89,27,90,26,91,157,224,156,225,155 %N A005132 Recamán's sequence (or Recaman's sequence): a(0) = 0; for n > 0, a(n) = a(n-1) - n if nonnegative and not already in the sequence, otherwise a(n) = a(n-1) + n. %C A005132 The name "Recamán's sequence" is due to _N. J. A. Sloane_, not the author! %C A005132 I conjecture that every number eventually appears - see A057167, A064227, A064228. - _N. J. A. Sloane_. That was written in 1991. Today I'm not so sure that every number appears. - _N. J. A. Sloane_, Feb 26 2017 %C A005132 As of Jan 25 2018, the first 13 missing numbers are 852655, 930058, 930557, 964420, 966052, 966727, 969194, 971330, 971626, 971866, 972275, 972827, 976367, ... For further information see the "Status Report" link. - _Benjamin Chaffin_, Jan 25 2018 %C A005132 From _David W. Wilson_, Jul 13 2009: (Start) %C A005132 The sequence satisfies [1] a(n) >= 0, [2] |a(n)-a(n-1)| = n, and tries to avoid repeats by greedy choice of a(n) = a(n-1) -+ n. %C A005132 This "wants" to be an injection on N = {0, 1, 2, ...}, as it attempts to avoid repeats by choice of a(n) = a(n-1) + n when a(n) = a(n-1) - n is a repeat. %C A005132 Clearly, there are injections satisfying [1] and [2], e.g., a(n) = n(n+1)/2. %C A005132 Is there a lexicographically earliest injection satisfying [1] and [2]? (End) %C A005132 Answer: Yes, of course: The set of injections satisfying [1] and [2] is not empty, so there's a lexicographically least element. More concretely, it starts with the same 23 terms a(0..22) which are known to be minimal, but after a(22) = 41 it has to go on with a(23) = 41 + 23 = 64, since choosing "-" here necessarily yields a non-injective sequence. See A171884. - _M. F. Hasler_, Apr 01 2019 %C A005132 It appears that a(n) is also the value of "x" and "y" of the endpoint of the L-toothpick structure mentioned in A210606 after n-th stage. - _Omar E. Pol_, Mar 24 2012 %C A005132 Of course this is not a permutation of the integers: the first repeated term is 42 = a(24) = a(20). - _M. F. Hasler_, Nov 03 2014. Also 43 = a(18) = a(26). - _Jon Perry_, Nov 06 2014 %C A005132 Of all the sequences in the OEIS, this one is my favorite to listen to. Click the "listen" button at the top, set the instrument to "103. FX 7 (Echoes)", click "Save", and open the MIDI file with a program like QuickTime Player 7. - _N. J. A. Sloane_, Aug 08 2017 %C A005132 This sequence cycles clockwise around the OEIS logo. - _Ryan Brooks_, May 09 2020 %D A005132 Alex Bellos and Edmund Harriss, Visions of the Universe (2016), Unnumbered pages. Includes Harriss's illustration of the first 65 steps drawn as a spiral. %D A005132 Benjamin Chaffin, N. J. A. Sloane, and Allan Wilks, On sequences of Recaman type, paper in preparation, 2006. %D A005132 Bernardo Recamán Santos, letter to N. J. A. Sloane, Jan 29 1991 %D A005132 N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence). %H A005132 N. J. A. Sloane, <a href="/A005132/b005132.txt">The first 100000 terms</a> %H A005132 Farid Aliniaeifard and Shu Xiao Li, <a href="https://arxiv.org/abs/2506.12868">Peak algebra in noncommuting variables</a>, arXiv:2506.12868 [math.CO], 2025. See p. 45. %H A005132 Alex Bellos and Brady Haran, <a href="https://www.youtube.com/watch?v=FGC5TdIiT9U">The Slightly Spooky Recamán Sequence</a>, Numberphile video, 2018. %H A005132 Alex Bellos and Brady Haran, <a href="/A005132/a005132_1.png">Edmund Harriss's illustration of first 62 steps drawn as a spiral</a>, snapshot from Numberphile video "The Slightly Spooky Recamán Sequence" (2018) at 2:37 minutes. [Included with permission of the authors] See also the Harriss link below. %H A005132 Harlan Brothers, <a href="https://www.youtube.com/watch?v=_itqtYqCHqo">Recamán's Blues (a sonification Recamán's sequence)</a>, Animation, Jun 8, 2024. %H A005132 Benjamin Chaffin, <a href="/A005132/a005132.png">Log-log plot of first 10^230 terms</a> %H A005132 Benjamin Chaffin, <a href="/A005132/a005132_1.txt">Status Report</a>, Jan 25 2018. %H A005132 Fabio Deelan Cunden, Marilena Ligabò, and Tommaso Monni, <a href="https://arxiv.org/abs/2301.13555">Random matrices associated to Young diagrams</a>, arXiv:2301.13555 [math.PR], 2023. See p. 7. %H A005132 Michael De Vlieger, video of first 1200 steps of the <a href="https://youtu.be/xl1yLPWypmo">Recamán sequence</a>, with audio accompaniment generated by aspects of the sequence. Oct 12, 2019. %H A005132 GBnums, <a href="https://www.echolalie.org/echolaliste/gbnums/eois.htm">A nice OEIS sequence</a> %H A005132 Gordon Hamilton, <a href="http://www.youtube.com/watch?v=mQdNaofLqVc">Wrecker Ball Sequences</a>, Video, 2013 %H A005132 Edmund Harriss, <a href="/A005132/a005132_1.pdf">The first 65 steps drawn as a spiral</a> %H A005132 Nick Hobson, <a href="/A005132/a005132.py.txt">Python program for this sequence</a> %H A005132 Bradley Klee, <a href="/A005132/a005132.go.txt">Code for pdf spiral drawing (golang)</a> %H A005132 Bradley Klee, <a href="/A005132/a005132_2.pdf">The first 65 steps drawn as a spiral (color)</a> %H A005132 Dana G. Korssjoen, Biyao Li, Stefan Steinerberger, Raghavendra Tripathi, and Ruimin Zhang, <a href="https://arxiv.org/abs/2012.04625">Finding structure in sequences of real numbers via graph theory: a problem list</a>, arXiv:2012.04625, Dec 08, 2020 %H A005132 C. L. Mallows, <a href="/A005132/a005132.jpg">Plot (jpeg) of first 10000 terms</a> %H A005132 C. L. Mallows, <a href="/A005132/a005132.ps">Plot (postscript) of first 10000 terms</a> %H A005132 Joseph Samuel Myers, Richard Schroeppel, Scott R. Shannon, N. J. A. Sloane, and Paul Zimmermann, <a href="http://arxiv.org/abs/2004.14000">Three Cousins of Recaman's Sequence</a>, arXiv:2004:14000 [math.NT], April 2020. %H A005132 Tilman Piesk, <a href="http://commons.wikimedia.org/wiki/File:Recaman%27s_sequence.svg">First 172 items in a coordinate system</a> [This is a graph of the start of A005132 rotated clockwise by 90 degs. - _N. J. A. Sloane_, Mar 23 2012] %H A005132 Omar E. Pol, <a href="http://www.polprimos.com/imagenespub/polcrt01.jpg">Illustration of initial terms of A001057, A005132, A000217</a>, 2012. %H A005132 Omar E. Pol, <a href="http://www.polprimos.com/imagenespub/polrec01.jpg">Illustration of initial terms</a>, 2012. %H A005132 Omar E. Pol, <a href="https://oeis.org/plot2a?name1=A005132&name2=A000004&tform1=untransformed&tform2=untransformed&shift=0&radiop1=matp&drawlines=true">Lateral view of a 3D-model whose front view is formed by spirals</a>, 2022 (using Plot 2 A005132 vs A000004) %H A005132 Bernardo Recamán Santos and N. J. A. Sloane, <a href="/A005132/a005132.pdf">Correspondence</a>, 1991. %H A005132 Scott R. Shannon, <a href="/A005132/a005132_2.png">Illustration of the first 2000 terms plotted as steps on a 2D square (Ulam) spiral</a>. The colors are graduated across the spectrum from red to violet to show the relative step order. %H A005132 Muhammad Khubab Siddique, <a href="https://www.researchgate.net/publication/344659758_Mathematics-II_B_Ed_Programme">Sequence and Series-I</a>, Unit 8, Mathematics-II, Dept. of Sci. Ed., Allama Iqbal Open Univ. (Islamabad, Pakistan, 2020), 281-313. %H A005132 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 A005132 N. J. A. Sloane, <a href="/A005132/a005132.txt">FORTRAN program for A005132, A057167, A064227, A064228</a> %H A005132 N. J. A. Sloane, <a href="https://arxiv.org/abs/2301.03149">"A Handbook of Integer Sequences" Fifty Years Later</a>, arXiv:2301.03149 [math.NT], 2023, pp. 10, 12-13. %H A005132 Alex van den Brandhof, <a href="https://pyth.eu/uploads/user/ArchiefPDF/Pyth55-2.pdf">Een bizarre rij</a>, Pythagoras, 55ste Jaargang, Nummer 2, Nov 2015 (Article in Dutch about this sequence, see page 19 and back cover). %H A005132 <a href="/index/Rea#Recaman">Index entries for sequences related to Recamán's sequence</a> %F A005132 a(k) = A000217(k) - 2*Sum_{i=1..n} A057166(i), for A057166(n) <= k < A057166(n+1). - _Christopher Hohl_, Jan 27 2019 %e A005132 Consider n=6. We have a(5)=7 and try to subtract 6. The result, 1, is certainly positive, but we cannot use it because 1 is already in the sequence. So we must add 6 instead, getting a(6) = 7 + 6 = 13. %p A005132 h := array(1..100000); maxt := 100000; a := [1]; ad := [1]; su := []; h[1] := 1; for nx from 2 to 500 do t1 := a[nx-1]-nx; if t1>0 and h[t1] <> 1 then su := [op(su), nx]; else t1 := a[nx-1]+nx; ad := [op(ad), nx]; fi; a := [op(a),t1]; if t1 <= maxt then h[t1] := 1; fi; od: # a is A005132, ad is A057165, su is A057166 %p A005132 A005132 := proc(n) %p A005132 option remember; local a, found, j; %p A005132 if n = 0 then return 0 fi; %p A005132 a := procname(n-1) - n ; %p A005132 if a <= 0 then return a+2*n fi; %p A005132 found := false; %p A005132 for j from 0 to n-1 while not found do %p A005132 found := procname(j) = a; %p A005132 od; %p A005132 if found then a+2*n else a fi; %p A005132 end: %p A005132 seq(A005132(n), n=0..70); # _R. J. Mathar_, Apr 01 2012 (reformatted by _Peter Luschny_, Jan 06 2019) %t A005132 a = {1}; Do[ If[ a[ [ -1 ] ] - n > 0 && Position[ a, a[ [ -1 ] ] - n ] == {}, a = Append[ a, a[ [ -1 ] ] - n ], a = Append[ a, a[ [ -1 ] ] + n ] ], {n, 2, 70} ]; a %t A005132 (* Second program: *) %t A005132 f[s_List] := Block[{a = s[[ -1]], len = Length@s}, Append[s, If[a > len && !MemberQ[s, a - len], a - len, a + len]]]; Nest[f, {0}, 70] (* _Robert G. Wilson v_, May 01 2009 *) %t A005132 RecamanSeq[i_Integer] := Fold[With[{lst=Last@#, len=Length@#}, Append[#, If[lst > len && !MemberQ[#, lst - len], lst - len, lst + len]]] &, {0}, Range@i]; RecamanSeq[10^5] (* _Mikk Heidemaa_, Nov 02 2024 *) %o A005132 (PARI) a(n)=if(n<2,1,if(abs(sign(a(n-1)-n)-1)+setsearch(Set(vector(n-1,i,a(i))),a(n-1)-n),a(n-1)+n,a(n-1)-n)) \\ _Benoit Cloitre_ %o A005132 (PARI) A005132(N=1000,show=0)={ my(s,t); for(n=1,N, s=bitor(s,1<<t += if( t<=n || bittest(s, t-n), n, -n)); show&&print1(t","));t} \\ _M. F. Hasler_, May 11 2008, updated _M. F. Hasler_, Nov 03 2014 %o A005132 (MATLAB) %o A005132 function a=A005132(m) %o A005132 % m=max number of terms in a(n). Offset n:0 %o A005132 a=zeros(1,m); %o A005132 for n=2:m %o A005132 B=a(n-1)-(n-1); %o A005132 C=0.^( abs(B+1) + (B+1) ); %o A005132 D=ismember(B,a(1:n-1)); %o A005132 a(n)=a(n-1)+ (n-1) * (-1)^(C + D -1); %o A005132 end %o A005132 % _Adriano Caroli_, Dec 26 2010 %o A005132 (Haskell) %o A005132 import Data.Set (Set, singleton, notMember, insert) %o A005132 a005132 n = a005132_list !! n %o A005132 a005132_list = 0 : recaman (singleton 0) 1 0 where %o A005132 recaman :: Set Integer -> Integer -> Integer -> [Integer] %o A005132 recaman s n x = if x > n && (x - n) `notMember` s %o A005132 then (x-n) : recaman (insert (x-n) s) (n+1) (x-n) %o A005132 else (x+n) : recaman (insert (x+n) s) (n+1) (x+n) %o A005132 -- _Reinhard Zumkeller_, Mar 14 2011 %o A005132 (Python) %o A005132 l=[0] %o A005132 for n in range(1, 101): %o A005132 x=l[n - 1] - n %o A005132 if x>0 and not x in l: l+=[x, ] %o A005132 else: l+=[l[n - 1] + n] %o A005132 print(l) # _Indranil Ghosh_, Jun 01 2017 %o A005132 (Python) %o A005132 def recaman(n): %o A005132 seq = [] %o A005132 for i in range(n): %o A005132 if(i == 0): x = 0 %o A005132 else: x = seq[i-1]-i %o A005132 if(x>=0 and x not in seq): seq+=[x] %o A005132 else: seq+=[seq[i-1]+i] %o A005132 return seq %o A005132 print(recaman(1000)) # _Ely Golden_, Jun 14 2018 %o A005132 (Python) %o A005132 from itertools import count, islice %o A005132 def A005132_gen(): # generator of terms %o A005132 a, aset = 0, set() %o A005132 for n in count(1): %o A005132 yield a %o A005132 aset.add(a) %o A005132 a = b if (b:=a-n)>=0 and b not in aset else a+n %o A005132 A005132_list = list(islice(A005132_gen(),30)) # _Chai Wah Wu_, Sep 15 2022 %Y A005132 Cf. A057165 (addition steps), A057166 (subtraction steps), A057167 (steps to hit n), A008336, A046901 (simplified version), A064227 (records for reaching n), A064228 (value of n that take a record number of steps to reach), A064284 (no. of times n appears), A064290 (heights of terms), A064291 (record highs), A119632 (condensed version), A063733, A079053, A064288, A064289, A064387, A064388, A064389, A228474 (bidirectional version). %Y A005132 A065056 gives partial sums, A160356 gives first differences. %Y A005132 A row of A066201. %Y A005132 Cf. A171884 (injective variant). %Y A005132 See A324784, A324785, A324786 for the "low points". %Y A005132 Cf. A330791, A331659, A331670. %K A005132 nonn,nice,hear,look %O A005132 0,3 %A A005132 _N. J. A. Sloane_ and _Simon Plouffe_, May 16 1991 %E A005132 _Allan Wilks_, Nov 06 2001, computed 10^15 terms of this sequence. At this point all the numbers below 852655 had appeared, but 852655 = 5*31*5501 was missing. %E A005132 After 10^25 terms of A005132 the smallest missing number is still 852655. - _Benjamin Chaffin_, Jun 13 2006 %E A005132 Even after 7.78*10^37 terms, the smallest missing number is still 852655. - _Benjamin Chaffin_, Mar 28 2008 %E A005132 Even after 4.28*10^73 terms, the smallest missing number is still 852655. - _Benjamin Chaffin_, Mar 22 2010 %E A005132 Even after 10^230 terms, the smallest missing number is still 852655. - _Benjamin Chaffin_, 2018 %E A005132 Changed "positive" in definition to "nonnegative". - _N. J. A. Sloane_, May 04 2020