8

The binary weight of the non negative integer k is defined by

w[k_] := Total[IntegerDigits[k, 2]]

The first values are (cf. http://oeis.org/ A000120)

Table[w[k], {k, 0, 10}]

(*
  {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2}
*)

Now define formally the generating function

g[z_] = Sum[w[k] z^k, {k, 0, ∞}]

(*
  (2 - z)/(-1 + z)^2 
*)

Most surprisingly, Mathematica returns an explicit result.

But this can't be correct, as the expansions about z = 0 differ

Sum[w[k] z^k, {k, 0, 5}]

(*
  z + z^2 + 2 z^3 + z^4 + 2 z^5
*)

Series[(2 - z)/(-1 + z)^2, {z, 0, 5}] // Normal

(*
  2 + 3 z + 4 z^2 + 5 z^3 + 6 z^4 + 7 z^5
*)

It looks as if in the infinite sum the function w[k] is replaced by (k + 2).

Any explanation? Seems to be a bug.

EDIT #1

24.02.15 19:08

To avoid the head replacement effect pointed out by belisarius we can consider the function

w1[n_] := n - Sum[IntegerExponent[k, 2], {k, 1, n}]

which is identical to w[k] for any k.

Now the infinite sum

g1[z_] = Sum[w1[n] z^n, {n, 0, \[Infinity]}]

is returned unevaluated, as it "should" (because it is too complicated)

$\sum _{n=0}^{\infty } z^n \left(n-\sum _{k=1}^n \text{IntegerExponent}[k,2]\right)$

So my discovery is not a bug, but I have learned the lesson that one should be very careful with infinite sums and their interpretation of the terms to be added.

LCarvalho
  • 9,233
  • 4
  • 40
  • 96
Dr. Wolfgang Hintze
  • 13,039
  • 17
  • 47

1 Answers1

9

The problem is that Total[] works with any head. So:

Total[Derivative[1, 2, 1]]
(* 4 *)

Total[IntegerDigits[k, 2]]
(* 2 + k *)

And so you can expect

Sum[(2 + k) z^k, {k, 0, ∞}] == Sum[Total[IntegerDigits[k, 2]] z^k, {k, 0, ∞}]
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453