1
$\begingroup$

What's the Mathematica code for the skew harmonic number: $$\overline{H}_n=\sum_{k=1}^n \frac{(-1)^{k-1}}{k}.$$

Wolfram expresses this number as $$\ln(2)-(-1)^n \text{LerchPhi}(-1,1,n+1).$$

I am wondering if there is a shorter command that Mathematica can understand.

$\endgroup$
6
  • 1
    $\begingroup$ Is Sum[(-1)^(k-1)/k,{k,n}] satisfactory? $\endgroup$
    – Adam
    Feb 24, 2021 at 22:14
  • $\begingroup$ @Adam thank you but this is just the sum representation. I'm looking for a simple code. $\endgroup$ Feb 24, 2021 at 22:28
  • $\begingroup$ H[n_]:=Sum[(-1)^(k-1)/k,{k,n}] --or-- H[n_] := (Log[ 2] - (-1)^n LerchPhi[-1, 1, n + 1]) ? $\endgroup$ Feb 24, 2021 at 22:57
  • $\begingroup$ SeriesCoefficient[Log[x + 1]/(1 - x), {x, 0, n}]... $\endgroup$
    – ciao
    Feb 24, 2021 at 23:04
  • 1
    $\begingroup$ It's not clear why you don't find the expression in terms of the Lerch transcendent satisfactory. $\endgroup$ Feb 25, 2021 at 1:52

2 Answers 2

3
$\begingroup$

What about

Log[2] + (-1)^n*(1/2)*(PolyGamma[1/2 + n/2] - PolyGamma[1 + n/2])

or

Log[2] + (-1)^n*(1/2)*(HarmonicNumber[-(1/2) + n/2] - HarmonicNumber[n/2])

?

$\endgroup$
1
  • $\begingroup$ Thank you for the efforts +1 $\endgroup$ Feb 25, 2021 at 2:29
2
$\begingroup$
Clear["Global`*"]

$Version

(* "12.2.0 for Mac OS X x86 (64-bit) (December 12, 2020)" *)

f[n_] = Sum[(-1)^(k - 1)/k, {k, n}] /. (-1)^(n + 1) :> -(-1)^n

(* (-1)^(1 + n) LerchPhi[-1, 1, 1 + n] + Log[2] *)

For large n this converges to Log[2]

Limit[f[n], n -> Infinity]

(* Log[2] *)

The real part of f is

f2[n_] = Assuming[Element[n, Reals],
  Re[f[n]] // ComplexExpand // FullSimplify]

(* -Cos[n π] LerchPhi[-1, 1, 1 + n] + Log[2] *)

f2 is equal to f for integer arguments:

Assuming[Element[n, Integers], f[n] == f2[n] // Simplify]

(* True *)

Graphically,

Show[
 Plot[f2[n], {n, 0, 25},
  PlotRange -> All,
  GridLines -> {None, {Log[2]}},
  GridLinesStyle ->
   Directive[Gray, AbsoluteThickness[1], Dashed]],
 DiscretePlot[f[n], {n, 0, 25},
  PlotStyle -> ColorData[97][2]]]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thank you. I'm wondering if there is a shorter expression. +1 though. $\endgroup$ Feb 25, 2021 at 2:28
  • 1
    $\begingroup$ I'll only note that (-1)^(1 + n) LerchPhi[-1, 1, 1 + n] + Log[2] // FunctionExpand does nothing, but (-1)^(1 + n) HurwitzLerchPhi[-1, 1, 1 + n] + Log[2] // FunctionExpand can be simplified into the expressions in Andreas's answer. $\endgroup$ Feb 25, 2021 at 4:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.