login
This site is supported by donations to The OEIS Foundation.

 

Logo


Hints
(Greetings from The On-Line Encyclopedia of Integer Sequences!)
A000120 1's-counting sequence: number of 1's in binary expansion of n (or the binary weight of n).
(Formerly M0105 N0041)
961

%I M0105 N0041

%S 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,1,2,

%T 2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,1,2,2,3,

%U 2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3

%N 1's-counting sequence: number of 1's in binary expansion of n (or the binary weight of n).

%C The binary weight of n is also called Hamming weight of n.

%C a(n) is also the largest integer such that 2^a(n) divides binomial(2n, n) = A000984(n). - _Benoit Cloitre_, Mar 27 2002

%C To construct the sequence, start with 0 and use the rule: If k >= 0 and a(0), a(1), ..., a(2^k-1) are the 2^k first terms, then the next 2^k terms are a(0) + 1, a(1) + 1, ..., a(2^k-1) + 1. - _Benoit Cloitre_, Jan 30 2003

%C An example of a fractal sequence. That is, if you omit every other number in the sequence, you get the original sequence. And of course this can be repeated. So if you form the sequence a(0 * 2^n), a(1 * 2^n), a(2 * 2^n), a(3 * 2^n), ... (for any integer n > 0), you get the original sequence. - Christopher.Hills(AT)sepura.co.uk, May 14 2003

%C The n-th row of Pascal's triangle has 2^k distinct odd binomial coefficients where k = a(n) - 1. - _Lekraj Beedassy_, May 15 2003

%C Fixed point of the morphism 0 -> 01, 1 -> 12, 2 -> 23, 3 -> 34, 4 -> 45, etc., starting from a(0) = 0. - _Robert G. Wilson v_, Jan 24 2006

%C a(n) = number of times n appears among the mystery calculator sequences: A005408, A042964, A047566, A115419, A115420, A115421. - _Jeremy Gardiner_, Jan 25 2006

%C a(n) = number of solutions of the Diophantine equation 2^m*k + 2^(m-1) + i = n, where m >= 1, k >= 0, 0 <= i < 2^(m-1); a(5) = 2 because only (m, k, i) = (1, 2, 0) [2^1*2 + 2^0 + 0 = 5] and (m, k, i) = (3, 0, 1) [2^3*0 + 2^2 + 1 = 5] are solutions. - _Hieronymus Fischer_, Jan 31 2006

%C The first appearance of k, k >= 0, is at a(2^k-1). - _Robert G. Wilson v_, Jul 27 2006

%C a(n) = A138530(n, 2) for n > 1. - _Reinhard Zumkeller_, Mar 26 2008

%C Sequence is given by T^(infty)(0) where T is the operator transforming any word w = w(1)w(2)...w(m) into T(w) = w(1)(w(1)+1)w(2)(w(2)+1)...w(m)(w(m)+1). I.e., T(0) = 01, T(01) = 0112, T(0112) = 01121223. - _Benoit Cloitre_, Mar 04 2009

%C a(A077436(n)) = A159918(A077436(n)); a(A000290(n)) = A159918(n). - _Reinhard Zumkeller_, Apr 25 2009

%C For n >= 2, the minimal k for which a(k(2^n-1)) is not multiple of n is 2^n + 3. - _Vladimir Shevelev_, Jun 05 2009

%C a(n) = A063787(n) - A007814(n). - _Gary W. Adamson_, Jun 04 2009

%C Triangle inequality: a(k+m) <= a(k) + a(m). Equality holds if and only if C(k+m, m) is odd. - _Vladimir Shevelev_, Jul 19 2009

%C The number of occurrences of value k in the first 2^n terms of the sequence is equal to binomial(n,k), and also equal to the sum of the first n-k+1 terms of column k in the array A071919. Example with k=2, n=7: there are 21 = binomial(7,2) = 1+2+3+4+5+6 2's in a(0) to a(2^7-1). - Brent Spillner (spillner(AT)acm.org), Sep 01 2010, simplified by _R. J. Mathar_, Jan 13 2017

%C a(n) = A213629(n, 1) for n > 0. - _Reinhard Zumkeller_, Jul 04 2012

%C Let m be the number of parts in the listing of the compositions of n as lists of parts in lexicographic order, a(k) = n - length(composition(k)) for all k < 2^n and all n (see example); A007895 gives the equivalent for compositions into odd parts. - _Joerg Arndt_, Nov 09 2012

%C a(n) = A240857(n,n). - _Reinhard Zumkeller_, Apr 14 2014

%C From _Daniel Forgues_, Mar 13 2015: (Start)

%C Just tally up row k (binary weight equal k) from 0 to 2^n - 1 to get the binomial coefficient C(n,k). (See A007318.)

%C 0 1 3 7 15

%C 0: O | . | . . | . . . . | . . . . . . . . |

%C 1: | O | O . | O . . . | O . . . . . . . |

%C 2: | | O | O O . | O O . O . . . |

%C 3: | | | O | O O O . |

%C 4: | | | | O |

%C Due to its fractal nature, the sequence is quite interesting to listen to.

%C (End)

%C The binary weight of n is a particular case of the digit sum (base b) of n. - _Daniel Forgues_, Mar 13 2015

%C The mean of the first n terms is 1 less than the mean of [a(n+1),...,a(2n)], which is also the mean of [a(n+2),...,a(2n+1)]. - _Christian Perfect_, Apr 02 2015

%C a(n) is also the largest part of the integer partition having viabin number n. The viabin number of an integer partition is defined in the following way. Consider the southeast border of the Ferrers board of the integer partition and consider the binary number obtained by replacing each east step with 1 and each north step, except the last one, with 0. The corresponding decimal form is, by definition, the viabin number of the given integer partition. "Viabin" is coined from "via binary". For example, consider the integer partition [2,2,2,1]. The southeast border of its Ferrers board yields 10100, leading to the viabin number 20. - _Emeric Deutsch_, Jul 20 2017

%D J.-P. Allouche and J. Shallit, Automatic Sequences, Cambridge Univ. Press, 2003, p. 119.

%D D. E. Knuth, The Art of Computer Programming, vol. 4A, Combinatorial Algorithms, Section 7.1.3, Problem 41, p. 589. - _N. J. A. Sloane_, Aug 03 2012

%D Manfred R. Schroeder, Fractals, Chaos, Power Laws. W.H. Freeman, 1991, p. 383.

%D N. J. A. Sloane, A Handbook of Integer Sequences, Academic Press, 1973 (includes this sequence).

%D N. J. A. Sloane and Simon Plouffe, The Encyclopedia of Integer Sequences, Academic Press, 1995 (includes this sequence).

%H N. J. A. Sloane, <a href="/A000120/b000120.txt">Table of n, a(n) for n = 0..10000</a>

%H F. T. Adams-Waters, F. Ruskey, <a href="https://cs.uwaterloo.ca/journals/JIS/VOL12/Ruskey2/ruskey14.html">Generating Functions for the Digital Sum and Other Digit Counting Sequences</a>, JIS 12 (2009) 09.5.6

%H J.-P. Allouche and J. Shallit, <a href="http://www.cs.uwaterloo.ca/~shallit/Papers/as0.ps">The ring of k-regular sequences</a>, Theoretical Computer Sci., 98 (1992), 163-197.

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

%H J.-P. Allouche, J. Shallit and J. Sondow, <a href="http://arxiv.org/abs/math/0512399">Summation of Series Defined by Counting Blocks of Digits</a>, arXiv:math/0512399 [math.NT], 2005-2006.

%H J.-P. Allouche, J. Shallit, and J. Sondow, <a href="http://dx.doi.org/10.1016/j.jnt.2006.06.001">Summation of series defined by counting blocks of digits</a>, J. Number Theory, 123 (2007), 133-143.

%H R. Bellman and H. N. Shapiro, <a href="http://www.jstor.org/stable/1969281">On a problem in additive number theory</a>, Annals Math., 49 (1948), 333-340. - _N. J. A. Sloane_, Mar 12 2009

%H Jean Coquet, <a href="http://dx.doi.org/10.1016/0022-314X(86)90067-3">Power sums of digital sums</a>, J. Number Theory 22 (1986), no. 2, 161-176.

%H Karl Dilcher and Larry Ericksen, <a href="http://www.combinatorics.org/ojs/index.php/eljc/article/view/v22i2p24">Hyperbinary expansions and Stern polynomials</a>, Elec. J. Combin, 22, 2015, #P2.24.

%H Josef Eschgfäller, Andrea Scarpante, <a href="http://arxiv.org/abs/1603.08500">Dichotomic random number generators</a>, arXiv:1603.08500 [math.CO], 2016.

%H Emmanuel Ferrand, <a href="https://cs.uwaterloo.ca/journals/JIS/VOL10/Ferrand/ferrand8.html">Deformations of the Taylor Formula</a>, Journal of Integer Sequences, Vol. 10 (2007), Article 07.1.7.

%H Steven R. Finch, Pascal Sebah and Zai-Qiao Bai, <a href="http://arXiv.org/abs/0802.2654">Odd Entries in Pascal's Trinomial Triangle</a>, arXiv:0802.2654 [math.NT], 2008.

%H P. Flajolet et al., <a href="http://algo.inria.fr/flajolet/Publications/FlGrKiPrTi94.pdf">Mellin Transforms And Asymptotics: Digital Sums</a>, Theoret. Computer Sci. 23 (1994), 291-314.

%H Michael Gilleland, <a href="/selfsimilar.html">Some Self-Similar Integer Sequences</a>

%H P. J. Grabner, P. Kirschenhofer, H. Prodinger, R. F. Tichy, R. F., <a href="http://dx.doi.org/10.1007/978-94-011-2058-6_25">On the moments of the sum-of-digits function</a>, Applications of Fibonacci numbers, Vol. 5 (St. Andrews, 1992), 263-271, Kluwer Acad. Publ., Dordrecht, 1993.

%H R. L. Graham, <a href="http://dx.doi.org/10.1111/j.1749-6632.1970.tb56468.x">On primitive graphs and optimal vertex assignments</a>, pp. 170-186 of Internat. Conf. Combin. Math. (New York, 1970), Annals of the NY Academy of Sciences, Vol. 175, 1970

%H K. Hessami Pilehrood, T. Hessami Pilehrood, <a href="https://cs.uwaterloo.ca/journals/JIS/VOL13/Pilehrood/pilehrood2.html">Vacca-Type Series for Values of the Generalized Euler Constant Function and its Derivative</a>, J. Integer Sequences, 13 (2010), #10.7.3.

%H Nick Hobson, <a href="/A000120/a000120.py.txt">Python program for this sequence</a>

%H Kathy Q. Ji and Herbert S. Wilf, <a href="http://arXiv.org/abs/math.CO/0611465">Extreme Palindromes</a>, arXiv:math/0611465 [math.CO], 2006.

%H G. Louchard, H. Prodinger, <a href="https://cs.uwaterloo.ca/journals/JIS/VOL16/Prodinger/prodinger29.html">The Largest Missing Value in a Composition of an Integer and Some Allouche-Shallit-Type Identities</a>, J. Int. Seq. 16 (2013) # 13.2.2

%H J.-L. Mauclaire, Leo Murata, <a href="http://dx.doi.org/10.3792/pjaa.59.274">On q-additive functions, I</a>, Proc. Japan Acad. Ser. A Math. Sci. 59 (1983), no. 6, 274-276.

%H J.-L. Mauclaire, Leo Murata, <a href="http://dx.doi.org/10.3792/pjaa.59.441">On q-additive functions, II</a>, Proc. Japan Acad. Ser. A Math. Sci. 59 (1983), no. 9, 441-444.

%H M. D. McIlroy, <a href="http://dx.doi.org/10.1137/0203020">The number of 1's in binary integers: bounds and extremal properties</a>, SIAM J. Comput., 3 (1974), 255-261.

%H Kerry Mitchell, <a href="http://kerrymitchellart.com/articles/Spirolateral-Type_Images_from_Integer_Sequences.pdf">Spirolateral-Type Images from Integer Sequences</a>, 2013

%H Sam Northshield, <a href="http://www.jstor.org/stable/10.4169/000298910X496714">Stern's Diatomic Sequence 0,1,1,2,1,3,2,3,1,4,...</a>, Amer. Math. Month., Vol. 117 (7), pp. 581-598, 2010.

%H Theophanes E. Raptis, <a href="https://arxiv.org/abs/1805.06341">Finite Information Numbers through the Inductive Combinatorial Hierarchy</a>, arXiv:1805.06301 [physics.gen-ph], 2018.

%H C. Sanna, <a href="https://cs.uwaterloo.ca/journals/JIS/VOL15/Sanna/sanna3.html">On Arithmetic Progressions of Integers with a Distinct Sum of Digits</a>, Journal of Integer Sequences, Vol. 15 (2012), #12.8.1. - _N. J. A. Sloane_, Dec 29 2012

%H Nanci Smith, <a href="http://www.fq.math.ca/Scanned/4-4/elementary4-4.pdf">Problem B-82</a>, Fib. Quart., 4 (1966), 374-375.

%H J. Sondow, <a href="https://dl.dropboxusercontent.com/u/34767554/Sondow_Vacca_final2.pdf">New Vacca-type rational series for Euler's constant and its "alternating" analog ln 4/Pi</a>, arXiv:math/0508042 [math.NT] 2005; Additive Number Theory, D. and G. Chudnovsky, eds., Springer, 2010, pp. 331-340.

%H R. Stephan, <a href="/somedcgf.html">Some divide-and-conquer sequences ...</a>

%H R. Stephan, <a href="/A079944/a079944.ps">Table of generating functions</a>

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

%H K. B. Stolarsky, <a href="http://dx.doi.org/10.1137/0132060">Power and exponential sums of digital sums related to binomial coefficient parity</a>, SIAM J. Appl. Math., 32 (1977), 717-730. See B(n). - _N. J. A. Sloane_, Apr 05 2014

%H J. R. Trollope, <a href="http://www.jstor.org/stable/2687954">An explicit expression for binary digital sums</a>, Math. Mag. 41 1968 21-25.

%H Robert Walker, <a href="http://robertinventor.com/ftswiki/Self_Similar_Sloth_Canon_Number_Sequences">Self Similar Sloth Canon Number Sequences</a>

%H Eric Weisstein's World of Mathematics, <a href="http://mathworld.wolfram.com/Binary.html">Binary</a>, <a href="http://mathworld.wolfram.com/DigitCount.html">Digit Count</a>, <a href="http://mathworld.wolfram.com/Stolarsky-HarborthConstant.html">Stolarsky-Harborth Constant</a>, <a href="http://mathworld.wolfram.com/DigitSum.html">Digit Sum</a>.

%H Wolfram Research, <a href="http://functions.wolfram.com/NumberTheoryFunctions/DigitCount/31/01/ShowAll.html">Numbers in Pascal's triangle</a>

%H <a href="/index/Cor#core">Index entries for "core" sequences</a>

%H <a href="/index/Bi#binary">Index entries for sequences related to binary expansion of n</a>

%H <a href="/index/Coi#Colombian">Index entries for Colombian or self numbers and related sequences</a>

%H <a href="/index/Fi#FIXEDPOINTS">Index entries for sequences that are fixed points of mappings</a>

%F a(0) = 0, a(2*n) = a(n), a(2*n+1) = a(n) + 1.

%F a(0) = 0, a(2^i) = 1; otherwise if n = 2^i + j with 0 < j < 2^i, a(n) = a(j) + 1.

%F G.f.: Product_{k >= 0} (1 + y*x^(2^k)) = Sum_{n >= 0} y^a(n)*x^n. - _N. J. A. Sloane_, Jun 04 2009

%F a(n) = a(n-1) + 1 - A007814(n) = log2[A001316(n)] = 2n - A005187(n) = A070939(n) - A023416(n). - _Henry Bottomley_, Apr 04 2001; corrected by _Ralf Stephan_, Apr 15 2002

%F a(n) = log2(A000984(n)/A001790(n) ). - _Benoit Cloitre_, Oct 02 2002

%F For n > 0, a(n) = n - sum(k = 1, n, A007814(k)). - _Benoit Cloitre_, Oct 19 2002

%F a(n) = n - sum(k > 0, floor(n/2^k)) = n - A011371(n). - _Benoit Cloitre_, Dec 19 2002

%F G.f.: 1/(1-x) * Sum(k >= 0, x^(2^k)/(1+x^(2^k))). - _Ralf Stephan_, Apr 19 2003

%F a(0) = 0, a(n) = a(n - 2^log_2(floor(n))) + 1. Examples: a(6) = a(6 - 2^2) + 1 = a(2) + 1 = a(2 - 2^1) + 1 + 1 = a(0) + 2 = 2; a(101) = a(101 - 2^6) + 1 = a(37) + 1 = a(37 - 2^5) + 2 = a(5 - 2^2) + 3 = a(1 - 2^0) + 4 = a(0) + 4 = 4; a(6275) = a(6275 - 2^12) + 1 = a(2179 - 2^11) + 2 = a(131 - 2^7) + 3 = a(3 - 2^1) + 4 = a(1 - 2^0) + 5 = 5; a(4129) = a(4129 - 2^12) + 1 = a(33 - 2^5) + 2 = a(1 - 2^0) + 3 = 3. - _Hieronymus Fischer_, Jan 22 2006

%F A fixed point of the mapping 0 -> 01, 1 -> 12, 2 -> 23, 3 -> 34, 4 -> 45, ... With f(i) = floor(n/2^i), a(n) is the number of odd numbers in the sequence f(0), f(1), f(2), f(3), f(4), f(5), ... - _Philippe Deléham_, Jan 04 2004

%F When read mod 2 gives the Morse-Thue sequence A010060.

%F Let floor_pow4(n) denote n rounded down to the next power of four, floor_pow4(n) = 4 ^ floor(log4 n). Then a(0) = 0, a(1) = 1, a(2) = 1, a(3) = 2, a(n) = a(floor(n / floor_pow4(n))) + a(n % floor_pow4(n)). - Stephen K. Touset (stephen(AT)touset.org), Apr 04 2007

%F a(n) = n - Sum_{k=2..n} Sum_{j|n, j >= 2} (floor(log_2(j)) - floor(log_2(j-1))). - _Hieronymus Fischer_, Jun 18 2007

%F a(n) = A007814(C(2n, n)) = 1 + A007814(C(2n-1, n)). - _Vladimir Shevelev_, Jul 20 2009

%F For odd m >= 1, a((4^m-1)/3) = a((2^m+1)/3) + (m-1)/2 (mod 2). - _Vladimir Shevelev_, Sep 03 2010

%F a(n) - a(n-1) = { 1 - a(n-1) if and only if A007814(n) = a(n-1), 1 if and only if A007814(n) = 0, -1 for all other A007814(n) }. - Brent Spillner (spillner(AT)acm.org), Sep 01 2010

%F a(A001317(n)) = 2^a(n). - _Vladimir Shevelev_, Oct 25 2010

%F a(n) = A139351(n) + A139352(n) = Sum_k {A030308(n, k)}. - _Philippe Deléham_, Oct 14 2011

%F From _Hieronymus Fischer_, Jun 10 2012: (Start)

%F a(n) = Sum_{j = 1..m+1} (floor(n/2^j + 1/2)) - floor(n/2^j)), where m = floor(log_2(n)).

%F General formulas for the number of digits >= d in the base p representation of n, where 1 <= d < p: a(n) = Sum_{j = 1..m+1} (floor(n/p^j + (p-d)/p) - floor(n/p^j)), where m=floor(log_p(n)); g.f.: g(x) = (1/(1-x))*Sum_{j>=0} (x^(d*p^j) - x^(p*p^j))/(1-x^(p*p^j)). (End)

%F a(n) = log_2(C(2*n,n) - (C(2*n,n) AND C(2*n,n)-1)), n < 61. - _Gary Detlefs_, Jul 10 2014

%F Sum_{n >= 1} a(n)/2n(2n+1) = (gamma + log(4/Pi))/2, where gamma is Euler's constant A001620; see Sondow 2005, 2010 and Allouche, Shallit, Sondow 2007. - _Jonathan Sondow_, Mar 21 2015

%F For any integer base b >= 2, the sum of digits s_b(n) of expansion base b of n is the solution of this recurrence relation: s_b(n) = 0 if n = 0 and s_b(n) = s_b(floor(n/b)) + (n mod b). Thus, a(n) satisfies: a(n) = 0 if n = 0 and a(n) = a(floor(n/2)) + (n mod 2). This easily yields a(n) = Sum_{i=0..floor(log_2(n))} (floor(n/2^i) mod 2). From that one can compute a(n) = n - Sum_{i=1..floor(log_2(n))} floor(n/2^i). - _Marek A. Suchenek_, Mar 31 2016

%e a(4) = a(0) + a(0) = 0

%e a(8) = a(2) + a(0) = 1

%e a(13) = a(3) + a(1) = 2 + 1 = 3

%e a(23) = a(1) + a(7) = 1 + a(1) + a(3) = 1 + 1 + 2 = 4

%e _Gary W. Adamson_ points out (Jun 03 2009) that this can be written as a triangle:

%e 0,

%e 1,

%e 1,2,

%e 1,2,2,3,

%e 1,2,2,3,2,3,3,4,

%e 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,

%e 1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,

%e 1,2,2,3,2,3,...

%e where the rows converge to A063787.

%e From _Omar E. Pol_, Jun 07 2009: (Start)

%e Also, triangle begins:

%e 0;

%e 1,1;

%e 2,1,2,2;

%e 3,1,2,2,3,2,3,3;

%e 4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4;

%e 5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5;

%e 6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,...

%e (End)

%e From _Joerg Arndt_, Nov 09 2012: (Start)

%e Connection to the compositions of n as lists of parts (see comment):

%e [ #]: a(n) composition

%e [ 0]: [0] 1 1 1 1 1

%e [ 1]: [1] 1 1 1 2

%e [ 2]: [1] 1 1 2 1

%e [ 3]: [2] 1 1 3

%e [ 4]: [1] 1 2 1 1

%e [ 5]: [2] 1 2 2

%e [ 6]: [2] 1 3 1

%e [ 7]: [3] 1 4

%e [ 8]: [1] 2 1 1 1

%e [ 9]: [2] 2 1 2

%e [10]: [2] 2 2 1

%e [11]: [3] 2 3

%e [12]: [2] 3 1 1

%e [13]: [3] 3 2

%e [14]: [3] 4 1

%e [15]: [4] 5

%e (End)

%p A000120 := proc(n) local w,m,i; w := 0; m := n; while m > 0 do i := m mod 2; w := w+i; m := (m-i)/2; od; w; end: wt := A000120;

%p A000120 := proc(n) add(i,i=convert(n,base,2)) end: # _Peter Luschny_, Feb 03 2011

%t Table[ DigitCount[n, 2, 1], {n, 0, 105}]

%t Nest[ Flatten[# /. # -> {#, # + 1}] &, {0}, 7] (* _Robert G. Wilson v_, Sep 27 2011 *)

%t Table[Plus @@ IntegerDigits[n, 2], {n, 0, 104}]

%t Nest[Join[#, # + 1] &, {0}, 7] (* _IWABUCHI Yu(u)ki_, Jul 19 2012 *)

%t Log[2,Nest[Join[#,2#]&,{1},14]] (* gives 2^14 term, _Carlos Alves_, Mar 30 2014 *)

%o (PARI) {a(n) = if( n<0, 0, 2*n - valuation((2*n)!, 2))};

%o (PARI) {a(n) = if( n<0, 0, subst(Pol(binary(n)), x ,1))};

%o (PARI) {a(n) = if( n<1, 0, a(n\2) + n%2)}; /* _Michael Somos_, Mar 06 2004 */

%o (PARI) a(n)=my(v=binary(n));sum(i=1,#v,v[i]) \\ _Charles R Greathouse IV_, Jun 24 2011

%o (PARI) A000120(n)=norml2(binary(n)) \\ _M. F. Hasler_, Oct 09 2012

%o (PARI) a(n)=hammingweight(n) \\ _Michel Marcus_, Oct 19 2013

%o (Common LISP) (defun floor-to-power (n pow) (declare (fixnum pow)) (expt pow (floor (log n pow)))) (defun enabled-bits (n) (if (< n 4) (n-th n (list 0 1 1 2)) (+ (enabled-bits (floor (/ n (floor-to-power n 4)))) (enabled-bits (mod n (floor-to-power n 4)))))) ; Stephen K. Touset (stephen(AT)touset.org), Apr 04 2007

%o See link in A139351 for Fortran program.

%o (Haskell)

%o import Data.Bits (Bits, popCount)

%o a000120 :: (Integral t, Bits t) => t -> Int

%o a000120 = popCount

%o a000120_list = 0 : c [1] where c (x:xs) = x : c (xs ++ [x,x+1])

%o -- _Reinhard Zumkeller_, Aug 26 2013, Feb 19 2012, Jun 16 2011, Mar 07 2011

%o (Haskell)

%o a000120 = concat r

%o where r = [0] : (map.map) (+1) (scanl (++) r)

%o -- _Luke Palmer_, Feb 16 2014

%o (Sage)

%o def A000120(n):

%o if n <= 1: return Integer(n)

%o return A000120(n//2) + n%2

%o [A000120(n) for n in range(105)] # _Peter Luschny_, Nov 19 2012

%o (Sage) def A000120(n) : return sum(n.digits(2)) # _Eric M. Schmidt_, Apr 26 2013

%o (Python)

%o def A000120(n):

%o ....return bin(n).count('1') # _Chai Wah Wu_, Sep 03 2014

%Y The basic sequences concerning the binary expansion of n are this one, A000788, A000069, A001969, A023416, A059015, A007088.

%Y Partial sums see A000788. See also A001792, A010062.

%Y Number of 0's in n: A023416 and A080791.

%Y a(n) = n - A011371(n).

%Y Sum of digits of n written in bases 2-16: this sequence, A053735, A053737, A053824, A053827, A053828, A053829, A053830, A007953, A053831, A053832, A053833, A053834, A053835, A053836.

%Y Cf. A001620, A007814.

%Y This is Guy Steele's sequence GS(3, 4) (see A135416).

%Y Cf. A055640, A055641, A102669-A102685, A117804, A122840, A122841, A160093, A160094, A196563, A196564 (for base 10).

%Y Cf. A230952 (boustrophedon transform).

%K nonn,easy,core,nice,hear,look,base,changed

%O 0,4

%A _N. J. A. Sloane_

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

License Agreements, Terms of Use, Privacy Policy. .

Last modified August 18 09:47 EDT 2018. Contains 313826 sequences. (Running on oeis4.)