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-8 of 8 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

A057167 Term in Recamán's sequence A005132 where n appears for first time, or -1 if n never appears.

Original entry on oeis.org

0, 1, 4, 2, 131, 129, 3, 5, 16, 14, 12, 10, 8, 6, 31, 29, 27, 25, 23, 99734, 7, 9, 11, 13, 15, 17, 64, 62, 60, 58, 56, 54, 52, 50, 48, 46, 44, 42, 40, 38, 111, 22, 20, 18, 28, 30, 32, 222, 220, 218, 216, 214, 212, 210, 208, 206, 204, 202, 200, 198, 196
Offset: 0

Views

Author

N. J. A. Sloane, Sep 14 2000

Keywords

Crossrefs

Programs

  • Maple
    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
  • Mathematica
    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 *)
  • PARI
    first(n) = my(a=vector(n), r=[0]); while(#Set(a)Iain Fox, Jul 11 2022
    
  • Python
    from itertools import count, islice
    def agen(): # generator of terms
        an, A005132set, inv, y = 0, {0}, {0: 0}, 0
        for n in count(1):
            t = an - n
            an = t if t >= 0 and t not in A005132set else an + n
            A005132set.add(an)
            inv[an] = n
            while y in inv: yield inv[y]; y += 1
    print(list(islice(agen(), 61))) # Michael S. Branicky, Jul 12 2022

Extensions

I conjecture a(n) is never -1 - but see A064227, A064228.
a(0)=0 added and escape clause value changed to -1 by N. J. A. Sloane, May 01 2020

A160356 First differences of Recamán's sequence A005132.

Original entry on oeis.org

1, 2, 3, -4, 5, 6, 7, -8, 9, -10, 11, -12, 13, -14, 15, -16, 17, 18, 19, -20, 21, -22, -23, 24, -25, 26, -27, 28, -29, 30, -31, 32, 33, 34, -35, 36, -37, -38, 39, -40, 41, -42, 43, -44, 45, -46, 47, -48, 49, -50, 51, -52, 53, -54, 55, -56, 57, -58, 59, -60, 61, -62, 63
Offset: 1

Views

Author

M. F. Hasler, Jun 03 2009

Keywords

Crossrefs

Formula

a(n) = A005132(n)-A005132(n-1) = n*A160357(n).
As a set, A160356 = A057165 union -A057166.

A187922 Positions k of addition steps in Recamán's sequence where A005132(k-1)-k = A005132(m) for some 0 < m < k.

Original entry on oeis.org

6, 7, 9, 18, 19, 21, 33, 34, 36, 66, 67, 69, 71, 73, 75, 101, 102, 104, 106, 108, 113, 114, 115, 117, 121, 123, 125, 127, 133, 134, 172, 173, 175, 177, 179, 181, 183, 186, 188, 189, 190, 194, 224, 225, 227, 229, 231, 233, 236, 238, 240, 242, 244, 246, 287, 288, 290, 292, 294, 296, 298, 300, 302, 304, 339, 340, 342, 344, 346, 348, 350
Offset: 1

Views

Author

Reinhard Zumkeller, Mar 17 2011

Keywords

Comments

Subsequence of A057165; A005132(a(n)-1) - a(n) = A005132(A187943(n));
A005132(a(n)) = A005132(a(n)-1) + a(n);
See A187921 for the other positions of addition steps in A005132.

Examples

			a(5) = 19: A005132(19-1) = 43 and 43-19>0, but the term 24=43-19 is already in A005132, therefore A005132(19)=43+19=62; A187943(5)=15 and A005132(15)=24.
		

Crossrefs

Programs

  • Haskell
    import Data.Set (Set, singleton, member, insert)
    a187922 n = a187922_list !! (n-1)
    a187922_list = r (singleton 0) 1 0 where
       r :: Set Integer -> Integer -> Integer -> [Integer]
       r s n x | x <= n           = r (insert (x+n) s) (n+1) (x+n)
               | (x-n) `member` s = n : r (insert (x+n) s) (n+1) (x+n)
               | otherwise        = r (insert (x-n) s) (n+1) (x-n)
    (C++) See Links section.

Extensions

Added condition "0 < m" to definition. See A333552. - N. J. A. Sloane, May 04 2020

A057166 Indices of subtraction steps in Recamán's sequence A005132.

Original entry on oeis.org

4, 8, 10, 12, 14, 16, 20, 22, 23, 25, 27, 29, 31, 35, 37, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 68, 70, 72, 74, 76, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99, 103, 105, 107, 109, 110, 111, 116, 118, 119, 120, 122, 124, 126, 128
Offset: 1

Views

Author

N. J. A. Sloane, Sep 14 2000

Keywords

Crossrefs

A187921 Positions k of addition steps in Recamán's sequence where A005132(k-1)<=k.

Original entry on oeis.org

1, 2, 3, 5, 11, 13, 15, 17, 24, 26, 28, 30, 32, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 112, 130, 132, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163, 165, 167, 169, 171, 197, 199, 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 248, 250
Offset: 1

Views

Author

Reinhard Zumkeller, Mar 17 2011

Keywords

Examples

			a(10)=26: A005132(26-1) = 17 and 17-26<0, hence A005132(26) = 17+26 = 43.
		

Crossrefs

Subsequence of A057165;
A005132(a(n)-1) <= a(n); A005132(a(n)) = A005132(a(n)-1) + a(n);
see A187922 for the other positions of addition steps in A005132.

Programs

  • Haskell
    import Data.Set (Set, singleton, member, insert)
    a187921 n = a187921_list !! (n-1)
    a187921_list = r (singleton 0) 1 0 where
       r :: Set Integer -> Integer -> Integer -> [Integer]
       r s n x | x <= n           = n : r (insert (x+n) s) (n+1) (x+n)
               | (x-n) `member` s = r (insert (x+n) s) (n+1) (x+n)
               | otherwise        = r (insert (x-n) s) (n+1) (x-n)

A076213 2*a(n)-1 = sign(A005132(n+1)-A005132(n)).

Original entry on oeis.org

1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0
Offset: 0

Views

Author

Benoit Cloitre, Nov 03 2002

Keywords

Comments

Characteristic function of A057165 - 1. - M. F. Hasler, Jun 03 2009

Formula

Conjecture: let s(n)=sum(k=1, n, a(k)), then lim n ->infinity s(n)/n = 1/2; for any n, 2*s(n) > n; let v(n)=2*s(n)-n, then v(n)/log(n) is bounded and sum(k=1, n, v(k)) is asymptotic to c*n*log(n) with 1 < c < 3/2.
a(n) = 1-A160351(n+1) = (A160357(n)+1)/2. - M. F. Hasler, Jun 03 2009

Extensions

Added initial value a(0)=1. - M. F. Hasler, Jun 03 2009

A187943 A057167(A005132(A187922(n) - 1) - A187922(n)).

Original entry on oeis.org

1, 3, 2, 5, 15, 9, 6, 30, 20, 17, 63, 57, 51, 45, 35, 21, 98, 92, 66, 72, 38, 110, 103, 109, 25, 31, 10, 16, 2, 126, 36, 169, 163, 157, 151, 145, 139, 45, 35, 101, 182, 21, 32, 133, 187, 105, 123, 193, 23, 29, 8, 14, 3, 2, 75, 284, 278, 272, 266, 260, 254, 248, 226, 232, 100, 260, 254, 248, 226, 232, 306, 138, 144, 150, 156, 162, 168, 34, 65, 117, 228, 234
Offset: 1

Views

Author

Reinhard Zumkeller, Mar 17 2011

Keywords

Comments

Let x = A187922(n) and r = A005132(x-1) - x, by definition of A187922: r > 0 and r occurs earlier in Recamán's sequence: a(n) = A057167(r) or r = A005132(a(n)), hence A005132(x) <> r but A005132(x) = A005132(x-1) - x.

Examples

			n = 5: x = A187922(5) = 19, r = A005132(19-1) - 19 = 43 - 19 = 24 = A005132(15): a(5) = 15.
		

Crossrefs

Showing 1-8 of 8 results.