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.

A006509 Cald's sequence: a(n+1) = a(n) - prime(n) if that value is positive and new, otherwise a(n) + prime(n) if new, otherwise 0; start with a(1)=1.

This page as a plain text file.
%I A006509 M2539 #83 Mar 07 2024 15:02:25
%S A006509 1,3,6,11,4,15,2,19,38,61,32,63,26,67,24,71,18,77,16,83,12,85,164,81,
%T A006509 170,73,174,277,384,275,162,35,166,29,168,317,468,311,148,315,142,321,
%U A006509 140,331,138,335,136,347,124,351,122,355,116,357,106,363,100,369,98,375,94,377,84,391,80,393,76,407,70,417,68,421,62,429,56,435,52,441,44,445,36,455,34,465,898,459,902,453,910,449,912,1379,900,413,904,405,908,399,920,397,938,1485,928,365,934,1505,2082,1495,2088,1489,888,281,894,1511,892,261,0,643,1290,637,1296,635,1308,631,1314,623,1324,615,1334,607,1340
%N A006509 Cald's sequence: a(n+1) = a(n) - prime(n) if that value is positive and new, otherwise a(n) + prime(n) if new, otherwise 0; start with a(1)=1.
%C A006509 The differences between this sequence and A117128 ("Recamán transform of primes") are (i) the offset (0 there) and (ii) there the sum is used in the second case whether it has already occurred or not (so duplicates occur), while here a(n+1) = 0 if the sum already occurred (so there are no duplicates apart from the zeros). - _M. F. Hasler_, Mar 06 2024
%D A006509 F. Cald, Problem 356, Franciscan order, J. Rec. Math., 7 (No. 4, 1974), 318; 10 (No. 1, 1977-78), 62-64.
%D A006509 "Cald's Sequence", Popular Computing (Calabasas, CA), Vol. 4 (No. 41, Aug 1976), pp. 16-17.
%D A006509 N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence).
%H A006509 T. D. Noe, <a href="/A006509/b006509.txt">Table of n, a(n) for n = 1..10000</a>
%H A006509 Francis Cald and others, Problem 356, Franciscan order, Solution and Guesses, J. Rec. Math., 10 (No. 1, 1977-78), 62-64: <a href="/A006509/a006509_1.pdf">Page 62</a>, <a href="/A006509/a006509_2.pdf">Page 63</a>, <a href="/A006509/a006509_3.pdf">Page 64</a>. [Annotated copy]
%H A006509 R. G. Wilson, <a href="/A006509/a006509.pdf">Letter to N. J. A. Sloane with attachment, Jan 1989</a>
%p A006509 M1:=500000; a:=array(0..M1); have:=array(0..M1); a[0]:=1;
%p A006509 for n from 0 to M1 do have[n]:=0; od: have[0]:=1; have[1]:=1;
%p A006509 M2:=2000; nmax:=M2; for n from 1 to M2 do p:=ithprime(n); i:=a[n-1]-p; j:=a[n-1]+p;
%p A006509 if i >= 1 and have[i]=0 then a[n]:=i; have[i]:=1;
%p A006509 elif j <= M1 and have[j]=0 then a[n]:=j; have[j]:=1;
%p A006509 elif j <= M1 then a[n]:=0; else nmax:=n-1; break; fi; od:
%p A006509 # To get A006509:
%p A006509 [seq(a[n],n=0..M2)];
%p A006509 # To get A112877 (off by 1 because of different offset in A006509):
%p A006509 zzz:=[]; for n from 0 to nmax do if a[n]=0 then zzz:=[op(zzz),n]; fi; od: [seq(zzz[i],i=1..nops(zzz))];
%t A006509 lst = {1}; f := Block[{b = Last@lst, p = Prime@ Length@lst}, If[b > p && !MemberQ[lst, b - p], AppendTo[lst, b - p], If[ !MemberQ[lst, b + p], AppendTo[lst, b + p], AppendTo[lst, 0]] ]]; Do[f, {n, 60}]; lst (* _Robert G. Wilson v_, Apr 25 2006 *)
%o A006509 (Haskell)
%o A006509 a006509 n = a006509_list !! (n-1)
%o A006509 a006509_list = 1 : f [1] a000040_list where
%o A006509    f xs'@(x:_) (p:ps) | x' > 0 && x' `notElem` xs = x' : f (x':xs) ps
%o A006509                       | x'' `notElem` xs          = x'' : f (x'':xs) ps
%o A006509                       | otherwise                 = 0 : f (0:xs) ps
%o A006509                       where x' = x - p; x'' = x + p
%o A006509 -- _Reinhard Zumkeller_, Oct 17 2011
%o A006509 (Python)
%o A006509 from sympy import primerange, prime
%o A006509 def aupton(terms):
%o A006509   alst = [1]
%o A006509   for n, pn in enumerate(primerange(1, prime(terms)+1), start=1):
%o A006509     x, y = alst[-1] - pn, alst[-1] + pn
%o A006509     if x > 0 and x not in alst: alst.append(x)
%o A006509     elif y > 0 and y not in alst: alst.append(y)
%o A006509     else: alst.append(0)
%o A006509   return alst
%o A006509 print(aupton(130)) # _Michael S. Branicky_, May 30 2021
%o A006509 (Python)
%o A006509 from sympy import nextprime
%o A006509 from itertools import islice
%o A006509 def agen(): # generator of terms
%o A006509     pn, an, aset = 2, 1, {1}
%o A006509     while True:
%o A006509         yield an
%o A006509         an = m if (m:=an-pn) > 0 and m not in aset else p if (p:=an+pn) not in aset else 0
%o A006509         aset.add(an)
%o A006509         pn = nextprime(pn)
%o A006509 print(list(islice(agen(), 131))) # _Michael S. Branicky_, Mar 07 2024
%o A006509 (PARI) A006509_upto(N, U=0)=vector(N,i, N=if(i>1, my(p=prime(i-1)); if( N>p && !bittest(U,N-p), N-p, !bittest(U, N+p), N+p), 1); N && U += 1 << N; N) \\ _M. F. Hasler_, Mar 06 2024
%Y A006509 Cf. A005132, A093903, A112877 & A370951 (indices of zeros).
%Y A006509 A111338 gives (conjecturally) the terms of the present sequence sorted into increasing order, and A111339 gives (conjecturally) the numbers missing from the present sequence.
%Y A006509 Cf. A117128, A117129, A064365, A000040.
%K A006509 nonn,nice
%O A006509 1,2
%A A006509 _N. J. A. Sloane_
%E A006509 More terms from Larry Reeves (larryr(AT)acm.org), Jul 20 2001
%E A006509 Many more terms added by _N. J. A. Sloane_, Apr 20 2006, to show difference from A117128.
%E A006509 Entry revised by _N. J. A. Sloane_, Mar 06 2024