%I M0277 #90 Oct 23 2024 00:42:01
%S 1,1,2,2,3,4,4,4,5,6,7,8,8,8,8,8,9,10,11,12,13,14,15,16,16,16,16,16,
%T 16,16,16,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,32,32,32,
%U 32,32,32,32,32,32,32,32,32,32,32,32,32,33,34,35,36,37,38,39,40,41,42,43
%N a(1) = a(2) = 1; thereafter a(2n+1) = a(n+1) + a(n), a(2n) = 2a(n).
%C a(n+1) is the second-order survivor of the n-person Josephus problem where every second person is marked until only one remains, who is then eliminated; the process is repeated from the beginning until all but one is eliminated. a(n) is first a power of 2 when n is three times a power of 2. For example, the first appearances of 2, 4, 8 and 16 are at positions 3, 6, 12 and 24, or (3*1),(3*2),(3*4) and (3*8). Eugene McDonnell (eemcd(AT)aol.com), Jan 19 2002, reporting on work of Boyko Bantchev (Bulgaria).
%C Appears to coincide with following sequence: Let n >= 1. Start with a bag B containing n 1's. At each step, replace the two least elements x and y in B with the single element x+y. Repeat until B contains 2 or fewer elements. Let a(n) be the largest element remaining in B at this point. - _David W. Wilson_, Jul 01 2003
%C Hsien-Kuei Hwang, S Janson, TH Tsai (2016) show that A078881 is the same sequence, apart from the offset. - _N. J. A. Sloane_, Nov 26 2017
%D J. Arkin, D. C. Arney, L. S. Dewald and W. E. Ebel, Jr., Families of recursive sequences, J. Rec. Math., 22 (No. 22, 1990), 85-94.
%D Hsien-Kuei Hwang, S Janson, TH Tsai, Exact and asymptotic solutions of the recurrence f(n) = f(floor(n/2)) + f(ceiling(n/2)) + g(n): theory and applications, Preprint, 2016; http://140.109.74.92/hk/wp-content/files/2016/12/aat-hhrr-1.pdf. Also Exact and Asymptotic Solutions of a Divide-and-Conquer Recurrence Dividing at Half: Theory and Applications, ACM Transactions on Algorithms, 13:4 (2017), #47; DOI: 10.1145/3127585
%D N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence).
%H T. D. Noe, <a href="/A006165/b006165.txt">Table of n, a(n) for n = 1..1024</a>
%H J.-P. Allouche and J. Shallit, <a href="http://www.math.jussieu.fr/~allouche/kreg2.ps">The Ring of k-regular Sequences, II</a>
%H J.-P. Allouche and J. Shallit, <a href="http://dx.doi.org/10.1016/S0304-3975(03)00090-2">The ring of k-regular sequences, II</a>, Theoret. Computer Sci., 307 (2003), 3-29.
%H Dale Gerdemann, <a href="https://www.youtube.com/watch?v=-BP_ENt8MdY">Second-Order Josephus Problem</a> (video)
%H Jeffrey Shallit, <a href="https://arxiv.org/abs/2203.02917">Intertwining of Complementary Thue-Morse Factors</a>, arXiv:2203.02917 [cs.FL], 2022.
%H Ralf Stephan, <a href="/somedcgf.html">Some divide-and-conquer sequences ...</a>
%H Ralf Stephan, <a href="/A079944/a079944.ps">Table of generating functions</a>
%H <a href="/index/J#Josephus">Index entries for sequences related to the Josephus Problem</a>
%F For n >= 2, if a(n) >= A006257(n), i.e., if msb(n) > n - a(n)/2, then a(n+1) = a(n)+1, otherwise a(n+1) = a(n). - _Henry Bottomley_, Jan 21 2002
%F a(n+1) = min(msb(n), 1+n-msb(n)/2) for all n (msb = most significant bit, A053644). - Boyko Bantchev (bantchev(AT)math.bas.bg), May 17 2002
%F a(1)=1, a(n) = n - a(n - a(a(n-1))). - _Benoit Cloitre_, Nov 08 2002
%F a(1)=1, a(n) = n - a(n - a(n-a(floor(n/2))). - _Benoit Cloitre_, May 12 2024
%F For k > 0, 0 <= i <= 2^k-1, a(2^k+i) = 2^(k-1)+i; for 2^k-2^(k-2) <= x <= 2^k a(x) = 2^(k-1); (also a(m*2^k) = a(m)*2^k for m >= 2). - _Benoit Cloitre_, Dec 16 2002
%F G.f.: x * (1/(1+x) + (1/(1-x)^2) * Sum_{k>=0} t^2*(1-t)) where t = x^2^k. - _Ralf Stephan_, Sep 12 2003
%F a(n) = A005942(n+1)/2 - n = n - A060973(n) = 2n - A007378(n). - _Ralf Stephan_, Sep 13 2003
%F a(n) = A080776(n-1) + A060937(n). - _Ralf Stephan_
%F From _Peter Bala_, Jul 31 2022: (Start)
%F For k a positive integer, define the k-th iterated sequence a^(k) of a by a^(1)(n) = a(n) and setting a^(k)(n) = a^(k-1)(a(n)) for k >= 2. For example, a^(2)(n) = a(a(n)) and a^(3)(n) = a(a(a(n))).
%F Conjectures: for n >= 2 there holds
%F (i) a(n) + a(n - a(n - a(n - a(n - a(n))))) = n;
%F (ii) a(n - a(n - a(n - a(n)))) = a(n - a(n - a(n - a(n - a(n - a(n))))));
%F (iii) a^2(n) = a(n - a(n - a(n - a(n))));
%F (iv) n - a(n) = a(n - a^(2)(n));
%F (v) a(n - a(n)) = a^(2)(n - a^(2)(n - a^(2)(n - a^(2)(n))));
%F (vi) for k >= 2, a^(k)(n - a^(k)(n)) = a^(k)(n - a^(k)(n - a^(k)(n - a^(k)(n)))).
%F (vii) for k >= 1, the sequence {n - a^(k)(n) : n >= 1} has first differences either 0 or 1. We conjecture that the repeated values of the sequence are of the form (2^k - 1)*2^m. The number of repeated values appears to always be 2, 3, 5, 9, 17, 35, ..., independent of k, conjecturally A000051. Two examples are given below.
%F A similar property may hold for the sequences {n - A060973^(k)(n) : n >= 2^(k-1)}, k = 1,2,3,.... (End)
%e From _Peter Bala_, Aug 01 2022: (Start)
%e 1) The sequence {n - a(a(n)) : n >= 1} begins [0, 1, 2, 3, 3, 4, 5, 6, 6, 6, 7, 8, 9, 10, 11, 12, 12, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 24, 24, 24, 24, 24, 24, 24, 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, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 49, ...] has the repeated values 3 (twice), 6 (three times), 12 (five times), 24 (nine times), 48 (seventeen times) ..., conjecturally of the form 3*2^m
%e 2) The sequence {n - a(a(a(n))) : n >= 1} begins [0, 1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10, 11, 12, 13, 14, 14, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 28, 28, 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, 56, 56, 56, 56, 56, 56, 56, 56, 57, ...] has the repeated values 7 (twice), 14 (three times), 28 (five times), 56 (nine times) ..., conjecturally of the form 7*2^m. (End)
%p a := proc (n) option remember; if n = 1 then 1 else n - a(n - a(a(n-1))) end if end proc: seq(a(n), n = 1..100); # _Peter Bala_, Jul 31 2022
%t t = {1, 1}; Do[If[OddQ[n], AppendTo[t, t[[Floor[n/2]]] + t[[Ceiling[n/2]]]], AppendTo[t, 2*t[[n/2]]]], {n, 3, 128}] (* _T. D. Noe_, May 25 2011 *)
%o (Python)
%o from functools import lru_cache
%o @lru_cache(maxsize=None)
%o def A006165(n): return 1 if n <= 2 else A006165(n//2) + A006165((n+1)//2) # _Chai Wah Wu_, Mar 08 2022
%o (Python)
%o def A006165(n): return min(n-(m:=1<<n.bit_length()-2),m<<1) if n>1 else 1 # _Chai Wah Wu_, Oct 22 2024
%o (PARI) a(n) = my(i=logint(n,2)-1); if(bittest(n,i), 2<<i, n - 1<<i); \\ _Kevin Ryde_, Aug 06 2022
%o (PARI) a(n)=if(n<2,1,n-a(n-a(n\2))); \\ _Benoit Cloitre_, May 12 2024
%Y Cf. A053644, A060973, A066997, A078881.
%K nonn,easy,changed
%O 1,3
%A _N. J. A. Sloane_
%E More terms from Larry Reeves (larryr(AT)acm.org), Jun 12 2002