login
The OEIS is supported by the many generous donors to the OEIS Foundation.

 


Alternating bit sum for n: replace 2^k with (-1)^k in binary expansion of n.
44

%I #102 Jul 19 2024 08:48:07

%S 0,1,-1,0,1,2,0,1,-1,0,-2,-1,0,1,-1,0,1,2,0,1,2,3,1,2,0,1,-1,0,1,2,0,

%T 1,-1,0,-2,-1,0,1,-1,0,-2,-1,-3,-2,-1,0,-2,-1,0,1,-1,0,1,2,0,1,-1,0,

%U -2,-1,0,1,-1,0,1,2,0,1,2,3,1,2,0,1,-1,0,1,2,0,1,2,3,1,2,3,4,2,3,1,2,0,1,2,3,1,2,0,1,-1,0,1,2,0,1,-1,0,-2

%N Alternating bit sum for n: replace 2^k with (-1)^k in binary expansion of n.

%C Notation: (2)[n](-1)

%C From _David W. Wilson_ and _Ralf Stephan_, Jan 09 2007: (Start)

%C a(n) is even iff n in A001969; a(n) is odd iff n in A000069.

%C a(n) == 0 (mod 3) iff n == 0 (mod 3).

%C a(n) == 0 (mod 6) iff (n == 0 (mod 3) and n/3 not in A036556).

%C a(n) == 3 (mod 6) iff (n == 0 (mod 3) and n/3 in A036556). (End)

%C a(n) = A030300(n) - A083905(n). - _Ralf Stephan_, Jul 12 2003

%C From _Robert G. Wilson v_, Feb 15 2011: (Start)

%C First occurrence of k and -k: 0, 1, 2, 5, 10, 21, 42, 85, ..., (A000975); i.e., first 0 occurs for 0, first 1 occurs for 1, first -1 occurs at 2, first 2 occurs for 5, etc.;

%C a(n)=-3 only if n mod 3 = 0,

%C a(n)=-2 only if n mod 3 = 1,

%C a(n)=-1 only if n mod 3 = 2,

%C a(n)= 0 only if n mod 3 = 0,

%C a(n)= 1 only if n mod 3 = 1,

%C a(n)= 2 only if n mod 3 = 2,

%C a(n)= 3 only if n mod 3 = 0, ..., . (End)

%C a(n) modulo 2 is the Prouhet-Thue-Morse sequence A010060. - _Philippe Deléham_, Oct 20 2011

%C In the Koch curve, number the segments starting with n=0 for the first segment. The net direction (i.e., the sum of the preceding turns) of segment n is a(n)*60 degrees. This is since in the curve each base-4 digit 0,1,2,3 of n is a sub-curve directed respectively 0, +60, -60, 0 degrees, which is the net 0,+1,-1,0 of two bits in the sum here. - _Kevin Ryde_, Jan 24 2020

%H Antti Karttunen, <a href="/A065359/b065359.txt">Table of n, a(n) for n = 0..65535</a> (terms 0..1000 from Harry J. Smith)

%H J.-P. Allouche and J. Shallit, <a href="http://dx.doi.org/10.1016/0304-3975(92)90001-V">The ring of k-regular sequences</a>, Theoretical Computer Sci., 98 (1992), 163-197. [<a href="http://www.math.jussieu.fr/~allouche/kreg2.ps">Preprint</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 H.-K. Hwang, S. Janson and T.-H. Tsai. <a href="http://algo.stat.sinica.edu.tw/hk/wp-content/files/2016/12/aat-hhrr-1.pdf">Exact and asymptotic solutions of the recurrence f(n) = f(floor(n/2)) + f(ceiling(n/2)) + g(n): theory and applications</a>. Preprint, 2016.

%H H.-K. Hwang, S. Janson and T.-H. Tsai. <a href="http://doi.org/10.1145/3127585">Exact and Asymptotic Solutions of a Divide-and-Conquer Recurrence Dividing at Half: Theory and Applications</a>. ACM Transactions on Algorithms, 13:4 (2017), #47. DOI:10.1145/3127585.

%H Hsien-Kuei Hwang, Svante Janson, and Tsung-Hsi Tsai, <a href="https://arxiv.org/abs/2210.10968">Identities and periodic oscillations of divide-and-conquer recurrences splitting at half</a>, arXiv:2210.10968 [cs.DS], 2022, p. 45.

%H Helge von Koch, <a href="http://archive.org/details/actamathematica11lefgoog">Une Méthode Géométrique Élémentaire pour l'Étude de Certaines Questions de la Théorie des Courbes Planes</a>, Acta Arithmetica, volume 30, 1906, pages 145-174.

%H William Paulsen, wpaulsen(AT)csm.astate.edu, <a href="http://myweb.astate.edu/wpaulsen/primemaze/mazepart.html">Partitioning the [prime] maze</a>

%H Ralf Stephan, <a href="https://arxiv.org/abs/math/0307027">Divide-and-conquer generating functions. I. Elementary sequences</a>, arXiv:math/0307027 [math.CO], 2003.

%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>

%F G.f.: (1/(1-x)) * Sum_{k>=0} (-1)^k*x^2^k/(1+x^2^k). - _Ralf Stephan_, Mar 07 2003

%F a(0) = 0, a(2n) = -a(n), a(2n+1) = 1-a(n). - _Ralf Stephan_, Mar 07 2003

%F a(n) = Sum_{k>=0} A030308(n,k)*(-1)^k. - _Philippe Deléham_, Oct 20 2011

%F a(n) = -a(floor(n/2)) + n mod 2. - _Reinhard Zumkeller_, Mar 20 2015

%F a(n) = A139351(n) - A139352(n). - _Kevin Ryde_, Jan 24 2020

%F G.f. A(x) satisfies: A(x) = x / (1 - x^2) - (1 + x) * A(x^2). - _Ilya Gutkovskiy_, Jul 28 2021

%F a(n) = A195017(A019565(n)). - _Antti Karttunen_, Jun 19 2024

%e Alternating bit sum for 11 = 1011 in binary is 1 - 1 + 0 - 1 = -1, so a(11) = -1.

%p A065359 := proc(n) local dgs ; dgs := convert(n,base,2) ; add( -op(i,dgs)*(-1)^i,i=1..nops(dgs)) ; end proc: # _R. J. Mathar_, Feb 04 2011

%t f[0]=0; f[n_] := Plus @@ (-(-1)^Range[ Floor[ Log2@ n + 1]] Reverse@ IntegerDigits[n, 2]); Array[ f, 107, 0]

%o (PARI)

%o SumAD(x)= { local(a=1, s=0); while (x>9, s+=a*(x-10*(x\10)); x\=10; a=-a); return(s + a*x) }

%o baseE(x, b)= { local(d, e=0, f=1); while (x>0, d=x-b*(x\b); x\=b; e+=d*f; f*=10); return(e) }

%o { for (n=0, 1000, b=baseE(n, 2); write("b065359.txt", n, " ", SumAD(b)) ) } \\ _Harry J. Smith_, Oct 17 2009

%o (PARI) for(n=0,106,s=0;u=1;for(k=0,#binary(n)-1,s+=bittest(n,k)*u;u=-u);print1(s,", ")) /* _Washington Bomfim_, Jan 18 2011 */

%o (PARI) a(n) = my(b=binary(n)); b*[(-1)^k|k<-[-#b+1..0]]~; \\ _Ruud H.G. van Tol_, Oct 16 2023

%o (Haskell)

%o a065359 0 = 0

%o a065359 n = - a065359 n' + m where (n', m) = divMod n 2

%o -- _Reinhard Zumkeller_, Mar 20 2015

%o (Python)

%o def a(n):

%o return sum((-1)**k for k, bi in enumerate(bin(n)[2:][::-1]) if bi=='1')

%o print([a(n) for n in range(107)]) # _Michael S. Branicky_, Jul 13 2021

%o (Python)

%o from sympy.ntheory import digits

%o def A065359(n): return sum((0,1,-1,0)[i] for i in digits(n,4)[1:]) # _Chai Wah Wu_, Jul 19 2024

%Y Cf. A005536 (partial sums), A056832 (abs first differences), A010060 (mod 2), A039004 (indices of 0's).

%Y Cf. A000120, A065360, A065081, A019565, A195017.

%Y Cf. also A004718.

%Y Cf. analogous sequences for bases 3-10: A065368, A346688, A346689, A346690, A346691, A346731, A346732, A055017 and also A373605 (for primorial base).

%K base,easy,sign

%O 0,6

%A _Marc LeBrun_, Oct 31 2001

%E More terms from _Ralf Stephan_, Jul 12 2003

Lookup | Welcome | Wiki | Register | Music | Plot 2 | Demos | Index | Browse | WebCam
Contribute new seq. or comment | Format | Style Sheet | Transforms | Superseeker | Recents
The OEIS Community | Maintained by The OEIS Foundation Inc.

License Agreements, Terms of Use, Privacy Policy. .

Last modified September 19 20:04 EDT 2024. Contains 376014 sequences. (Running on oeis4.)