cp's OEIS Frontend

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.

Showing 1-9 of 9 results.

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.

Original entry on oeis.org

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, 17, 43, 16, 44, 15, 45, 14, 46, 79, 113, 78, 114, 77, 39, 78, 38, 79, 37, 80, 36, 81, 35, 82, 34, 83, 33, 84, 32, 85, 31, 86, 30, 87, 29, 88, 28, 89, 27, 90, 26, 91, 157, 224, 156, 225, 155
Offset: 0

Views

Author

N. J. A. Sloane and Simon Plouffe, May 16 1991

Keywords

Comments

The name "Recamán's sequence" is due to N. J. A. Sloane, not the author!
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
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
From David W. Wilson, Jul 13 2009: (Start)
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.
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.
Clearly, there are injections satisfying [1] and [2], e.g., a(n) = n(n+1)/2.
Is there a lexicographically earliest injection satisfying [1] and [2]? (End)
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
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
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
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
This sequence cycles clockwise around the OEIS logo. - Ryan Brooks, May 09 2020

Examples

			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.
		

References

  • 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.
  • Benjamin Chaffin, N. J. A. Sloane, and Allan Wilks, On sequences of Recaman type, paper in preparation, 2006.
  • Bernardo Recamán Santos, letter to N. J. A. Sloane, Jan 29 1991
  • N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence).

Crossrefs

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).
A065056 gives partial sums, A160356 gives first differences.
A row of A066201.
Cf. A171884 (injective variant).
See A324784, A324785, A324786 for the "low points".

Programs

  • Haskell
    import Data.Set (Set, singleton, notMember, insert)
    a005132 n = a005132_list !! n
    a005132_list = 0 : recaman (singleton 0) 1 0 where
       recaman :: Set Integer -> Integer -> Integer -> [Integer]
       recaman s n x = if x > n && (x - n) `notMember` s
                          then (x-n) : recaman (insert (x-n) s) (n+1) (x-n)
                          else (x+n) : recaman (insert (x+n) s) (n+1) (x+n)
    -- Reinhard Zumkeller, Mar 14 2011
    
  • MATLAB
    function a=A005132(m)
    % m=max number of terms in a(n). Offset n:0
    a=zeros(1,m);
    for n=2:m
        B=a(n-1)-(n-1);
        C=0.^( abs(B+1) + (B+1) );
        D=ismember(B,a(1:n-1));
        a(n)=a(n-1)+ (n-1) * (-1)^(C + D -1);
    end
    % Adriano Caroli, Dec 26 2010
    
  • Maple
    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
    A005132 := proc(n)
        option remember; local a, found, j;
        if n = 0 then return 0 fi;
        a := procname(n-1) - n ;
        if a <= 0 then return a+2*n fi;
        found := false;
        for j from 0 to n-1 while not found do
            found := procname(j) = a;
        od;
        if found then a+2*n else a fi;
    end:
    seq(A005132(n), n=0..70); # R. J. Mathar, Apr 01 2012 (reformatted by Peter Luschny, Jan 06 2019)
  • Mathematica
    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
    (* Second program: *)
    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 *)
    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 *)
  • 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
    
  • PARI
    A005132(N=1000,show=0)={ my(s,t); for(n=1,N, s=bitor(s,1<M. F. Hasler, May 11 2008, updated M. F. Hasler, Nov 03 2014
    
  • Python
    l=[0]
    for n in range(1, 101):
        x=l[n - 1] - n
        if x>0 and not x in l: l+=[x, ]
        else: l+=[l[n - 1] + n]
    print(l) # Indranil Ghosh, Jun 01 2017
    
  • Python
    def recaman(n):
      seq = []
      for i in range(n):
        if(i == 0): x = 0
        else: x = seq[i-1]-i
        if(x>=0 and x not in seq): seq+=[x]
        else: seq+=[seq[i-1]+i]
      return seq
    print(recaman(1000)) # Ely Golden, Jun 14 2018
    
  • Python
    from itertools import count, islice
    def A005132_gen(): # generator of terms
        a, aset = 0, set()
        for n in count(1):
            yield a
            aset.add(a)
            a = b if (b:=a-n)>=0 and b not in aset else a+n
    A005132_list = list(islice(A005132_gen(),30)) # Chai Wah Wu, Sep 15 2022

Formula

a(k) = A000217(k) - 2*Sum_{i=1..n} A057166(i), for A057166(n) <= k < A057166(n+1). - Christopher Hohl, Jan 27 2019

Extensions

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.
After 10^25 terms of A005132 the smallest missing number is still 852655. - Benjamin Chaffin, Jun 13 2006
Even after 7.78*10^37 terms, the smallest missing number is still 852655. - Benjamin Chaffin, Mar 28 2008
Even after 4.28*10^73 terms, the smallest missing number is still 852655. - Benjamin Chaffin, Mar 22 2010
Even after 10^230 terms, the smallest missing number is still 852655. - Benjamin Chaffin, 2018
Changed "positive" in definition to "nonnegative". - N. J. A. Sloane, May 04 2020

A064289 Height of n-th term in Recamán's sequence A005132.

Original entry on oeis.org

0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 5, 6, 5, 6, 5, 6, 5, 6, 5, 6, 7, 8, 7, 8, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 6, 7, 8, 9, 8, 9, 8, 9, 8, 9, 8, 9, 8, 7, 8, 7, 8, 7, 8, 7, 8, 7, 8, 7, 8, 7, 8, 7, 8, 7, 8, 7, 8, 7, 8, 7
Offset: 0

Views

Author

N. J. A. Sloane, Sep 25 2001

Keywords

Comments

The height of a term in A005132 = number of addition steps - number of subtraction steps to produce it.
Partial sums of A160357. - Allan C. Wechsler, Sep 08 2019

Examples

			A005132 begins 1, 3, 6, 2, 7, 13, 20, 12, ... and these terms have heights 1, 2, 3, 2, 3, 4, 5, 4, ...
		

Crossrefs

Programs

  • Maple
    g:= proc(n) is(n=0) end:
    b:= proc(n) option remember; local t;
          if n=0 then 0 else t:= b(n-1)-n; if t<=0 or g(t)
          then t:= b(n-1)+n fi; g(t):= true; t fi
        end:
    a:= proc(n) option remember; `if`(n=0, 0,
           a(n-1)+signum(b(n)-b(n-1)))
        end:
    seq(a(n), n=0..120);  # Alois P. Heinz, Sep 08 2019
  • Mathematica
    g[n_] := n == 0;
    b[n_] := b[n] = Module[{t}, If[n == 0, 0, t = b[n - 1] - n; If[t <= 0 || g[t], t = b[n - 1] + n]; g[t] = True; t]];
    a[n_] := a[n] = If[n == 0, 0, a[n - 1] + Sign[b[n] - b[n - 1]]];
    a /@ Range[0, 100] (* Jean-François Alcover, Apr 11 2020, after Alois P. Heinz *)

Extensions

a(0)=0 prepended by Allan C. Wechsler, Sep 08 2019

A064290 First number of height n in Recamán's sequence A005132.

Original entry on oeis.org

0, 1, 3, 6, 13, 20, 43, 62, 113, 224, 367, 494, 833, 1815, 3379, 5551, 9169, 17864, 32978, 58964, 106218, 131313, 155719, 180118, 591890, 881467, 1345004, 3012446, 5728819, 9309579, 17512700, 25641318, 52978675, 61998980, 125130665, 244636214, 280766754, 566273517, 1031389697, 2182394227, 3045423658, 3454917187
Offset: 1

Views

Author

N. J. A. Sloane, Sep 25 2001

Keywords

Comments

The height of a term in A005132 = number of addition steps - number of subtraction steps to reach it (see A064289).
Needs a b-file. - N. J. A. Sloane, May 01 2020

Examples

			A005132 begins 0, 1, 3, 6, 2, 7, 13, 20, 12, ... and these terms have heights 0, 1, 2, 3, 2, 3, 4, 5, 4, ...
		

Crossrefs

Extensions

a(0) = 0 added by N. J. A. Sloane, May 01 2020

A064387 Variation (2) 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 a(n) = a(n-1)+n+i, where i >= 0 is the smallest number such that a(n-1)+n+i has not already appeared.

Original entry on oeis.org

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, 45, 72, 100, 71, 101, 70, 38, 5, 39, 4, 40, 77, 115, 76, 36, 78, 120, 163, 119, 74, 28, 75, 27, 79, 29, 80, 132, 185, 131, 186, 130, 73, 15, 81, 141, 202
Offset: 1

Views

Author

N. J. A. Sloane, Sep 28 2001

Keywords

Comments

Variation (4) (A064389) is the nicest of these variations.
I would also like to get the following sequences: number of steps before n appears (or 0 if n never appears), list of numbers that never appear, height of n (cf. A064288, A064289, A064290), etc.

References

  • Suggested by J. C. Lagarias.

Crossrefs

Cf. A005132, A046901, A064388, A064389. Agrees with A064389 for first 187 terms, then diverges.

Programs

  • Maple
    h := array(1..100000); maxt := 100000; a := array(1..1000); a[1] := 1; h[1] := 1; for nx from 2 to 1000 do t1 := a[nx-1]-nx; if t1>0 and h[t1] <> 1 then a[nx] := t1; if t1 < maxt then h[t1] := 1; fi; else for i from 0 to 1000 do 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; fi; od; evalm(a);

A064388 Variation (3) on Recamán's sequence (A005132): set s (the step size) initially equal to 2; to get a(n), we first try to subtract s from a(n-1): a(n) = a(n-1)-s if positive and not already in the sequence, in which case we change s to s+1; if not, a(n) = a(n-1)+s+i, where i >= 0 is the smallest number such that a(n-1)+s+i has not already appeared and now we change s to s+i+1.

Original entry on oeis.org

1, 3, 6, 2, 7, 13, 20, 12, 21, 11, 22, 10, 23, 9, 24, 8, 25, 43, 62, 42, 63, 41, 18, 44, 17, 45, 16, 46, 15, 47, 14, 48, 83, 119, 82, 120, 81, 121, 80, 38, 84, 37, 85, 36, 86, 35, 87, 34, 88, 33, 89, 32, 90, 31, 91, 30, 92, 29, 93, 28, 94, 27, 95, 26, 96, 167, 239, 166, 240
Offset: 1

Views

Author

N. J. A. Sloane, Sep 28 2001

Keywords

Comments

Variation (4) (A064389) is the nicest of these variations.
I would also like to get the following sequences: number of steps before n appears (or 0 if n never appears), list of numbers that never appear, height of n (cf. A064288, A064289, A064290), etc.

Crossrefs

Programs

  • BASIC
    rem Chipmunk BASIC v3.6.4(b8) http://www.nicholson.com/rhn/basic/
    max=1000 : dim a(max)
    s=2 : z=1 : a(z)=1 : print str$(z)+",";
    for n=1 to 200
    x=z-s : if x <= 0 then goto yyy
    if a(x)=0 then a(x)=1 : print str$(x)+","; : s=s+1 : z=x : goto xxx
    yyy: for i=0 to max
    x=z+s+i
    if a(x)=0 then a(x)=1 : print str$(x)+","; : s=s+i+1 : z=x : goto xxx
    next i
    xxx: next n
    print : end
    rem Jeremy Gardiner, Feb 22 2014

Extensions

More terms from David Wasserman, Jul 16 2002

A064291 Record high values in Recamán's sequence A005132.

Original entry on oeis.org

0, 1, 3, 6, 7, 13, 20, 21, 22, 23, 24, 25, 43, 62, 63, 79, 113, 114, 157, 224, 225, 226, 227, 228, 265, 367, 368, 369, 370, 379, 494, 495, 631, 632, 633, 634, 635, 636, 643, 833, 1090, 1091, 1092, 1093, 1094, 1095, 1096, 1097, 1098, 1182, 1183
Offset: 0

Views

Author

N. J. A. Sloane, Sep 25 2001

Keywords

Crossrefs

Extensions

a(0)=0 added by N. J. A. Sloane, May 01 2020

A064492 Start of n-th segment of Recamán's sequence R(m) (A005132): values of n where the change in the fractional parts of successive values of R(n)/n is positive.

Original entry on oeis.org

1, 2, 4, 7, 12, 22, 40, 77, 135, 249, 454, 845, 1521, 2753, 5046, 9318, 17224, 31222, 57072, 99742, 181694, 328256, 589933, 1034839, 1788538, 3225919, 5784586, 10212211, 18399785, 32148795, 58056876, 101769230, 173395920, 302890749, 511561221, 904036925, 1610187039, 2789150424, 4910758398, 8416580355, 13808215356, 23006557539, 39488697701, 65854567303, 107078836274, 188471115227, 325374626149, 535755688022, 872467803894, 1404720439054, 2402964238974, 3883018238330, 6283167591180, 10125357598983, 16166305650061, 25735985498862, 40806937801473, 64628644387495, 102481019573338, 159888464280047, 250759470174414, 394178473635589, 599819882554936, 939455761022725
Offset: 1

Views

Author

John W. Layman, Oct 04 2001

Keywords

Crossrefs

Cf. A005132, A064288, A064289, A064292, A064293. This sequence and A064294 keep pretty close together.
See A065038 for the corresponding values of R(n). See also A065053 for first differences.

Extensions

More terms from John W. Layman and N. J. A. Sloane, Oct 12 2001
Extended to 10^15 by Allan Wilks, Nov 06 2001

A108840 First step in Recamán's sequence A005132 at which there are n consecutive subtraction steps in a row.

Original entry on oeis.org

4, 22, 109, 1360, 24404, 17695283, 6824557148
Offset: 1

Views

Author

Sergio Pimentel, Jul 25 2005

Keywords

Examples

			E.g., 109 is the third term of this sequence because it the first step in Recamán's sequence at which there are three consecutive subtraction steps in a row:
109. 370 - 109 = 261
110. 261 - 110 = 151
111. 151 - 111 = 40
		

Crossrefs

Extensions

a(6)-a(7) from Jud McCranie, Jan 24 2020

A383730 a(0) = 0, a(n) = a(n-1) + A002260(n) * (-1)^(n-1) if not already in the sequence, otherwise a(n) = a(n-1) - A002260(n) * (-1)^(n-1).

Original entry on oeis.org

0, 1, 2, 4, 3, 5, 8, 9, 7, 10, 6, 5, 7, 4, 8, 13, 12, 14, 11, 15, 20, 26, 25, 27, 24, 28, 23, 29, 22, 21, 19, 16, 20, 15, 21, 14, 22, 21, 23, 20, 24, 19, 25, 32, 40, 49, 48, 50, 47, 51, 46, 52, 45, 53, 44, 54, 55, 57, 60, 64, 59, 65, 58, 66, 75, 85, 74, 73, 71
Offset: 0

Views

Author

Markel Zubia, May 06 2025

Keywords

Comments

This sequence is a bidirectional form of Recamán's sequence.
Another way to define the sequence: starting at 0, take steps of size 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ... alternating left and right while avoiding repeated values (negative values are allowed).
The sequence is unbounded either above or below.
Conjecture: the sequence is unbounded both above and below.
Conjecture: each integer appears finitely often.
It exhibits a mix of chaotic and periodic behavior, including long plateaus and sudden large jumps.
Around term 25000, the sequence settles near -3000 in a visually fractal structure. After ~1.85 million terms, it appears to settle again near -500000. Astonishingly, after ~66.7 million steps, it jumps sharply from around -500000 to +4.51 million.

Examples

			a(1) = 0 + 1.
a(2) = 0 + 1 + 1 = 2, since 0 + 1 - 1 = 0 already appears in the sequence, as a(0) = 0.
a(6) = 0 + 1 + 1 + 2 - 1 + 2 + 3 = 8.
		

Crossrefs

Cf. A005132.
Other bidirectional extensions of Recamán's sequence: A063733, A079053, A064288, A064289, A064387, A064388, A064389, A228474.
Cf. A002260.

Programs

  • Python
    def a(n):
        t, k, curr = 1, 1, 0
        seen = set()
        for i in range(n):
            seen.add(curr)
            step = (-1)**i * t
            if curr + step not in seen:
                curr = curr + step
            else:
                curr = curr - step
            t += 1
            if t > k:
                t, k = 1, k + 1
        return curr
Showing 1-9 of 9 results.