Skip to content

HarmonicNumber ​

The harmonic number Hn=∑k=1n1kH_n = \sum_{k=1}^n \tfrac1k, exact at a non-negative integer nn; with a second argument, the generalized Hn(r)=∑k=1nk−rH_n^{(r)} = \sum_{k=1}^n k^{-r}. Continued off the integers by Hz=ψ(z+1)+γH_z = \psi(z+1) + \gamma. Provided by @enumeratio/analytic.

✓ checked against that system · ! checked, with disagreements (hover a chip)FungrimHarmonicNumber✓Fungrim entry00c02a✓f50c74Wolfram LanguageHarmonicNumber!
HarmonicNumber(n)the harmonic number Hn=∑k=1n1/kH_n = \sum_{k=1}^n 1/k.
SageMathharmonic_number(n)SymPyharmonic(n)mpmathharmonic(n)Juliaenumeratio_ball(x -> digamma(x + 1) + const_euler(RR), n)Rustharmonic(n)
HarmonicNumber(n, r)the generalized harmonic number Hn(r)=∑k=1nk−rH_n^{(r)} = \sum_{k=1}^n k^{-r}.
SageMathharmonic_number(n, r)SymPyharmonic(n, r)Juliaenumeratio_ball((n, r) -> r == 1 ? digamma(n + 1) + const_euler(RR) : zeta(r) - zeta(r, n + 1), n, r)Rustgeneralized_harmonic(n, r)
Typed signatures
TypePackageApplies
(number, number?) -> numberanalyticalways

Domain: Special functions

Details
  • At a non-negative integer nn (and an integer rr of either sign, in the two-argument form) the sum is exact: H0=0H_0 = 0, H1=1H_1 = 1, H2=32H_2 = \tfrac32, H10=73812520H_{10} = \tfrac{7381}{2520}.
  • Continued off the lattice by the standard digamma identity Hz=ψ(z+1)+γH_z = \psi(z+1) + \gamma (PolyGamma, EulerGamma) and its generalization Hz(r)=ζ(r)−ζ(r,z+1)H_z^{(r)} = \zeta(r) - \zeta(r, z+1) (Zeta, HurwitzZeta) — both reduce to the same exact values at the integers, so there is one formula, not a case split.
  • Order r=1r = 1 is the one place that generalization can't be used directly: ζ(1)\zeta(1) is itself a pole, at every zz, even though Hz(1)=HzH_z^{(1)} = H_z is perfectly finite. HarmonicNumber(z, 1) is routed straight to the one-argument ψ(z+1)+γ\psi(z+1) + \gamma instead.
  • Negative integer nn has no sum and is a pole: H−1=H−2=⋯=ComplexInfinityH_{-1} = H_{-2} = \cdots = \mathrm{ComplexInfinity}, in both the one- and two-argument forms — matching Wolfram, which does not extend the sum by the continuation there.
  • Complex zz and complex/non-integer rr are supported numerically, via PolyGamma's digamma (rr absent) and HurwitzZeta (rr present).
  • A non-integer rr (or non-integer zz with rr present) stays symbolic under plain evaluation even at an otherwise-exact nn — the sum ∑k−r\sum k^{-r} has no rational value there — and only reduces under N() or a floating-point argument, the same gate every head in this package uses.
  • Differs from Wolfram: HarmonicNumber(1/4)HarmonicNumber(1/4) reduces to its closed form on evaluation; Wolfram keeps HarmonicNumber[1/4] until FunctionExpand. Both agree as identities.

Plot

Examples

25 more kept as data, checked against the oracles
Basic #

H10=73812520H_{10} = \tfrac{7381}{2520}

live ↗
InHarmonicNumber(10)\mathrm{HarmonicNumber}(10)Out7 3812 520\frac{7\,381}{2\,520}

H0=0H_0 = 0, the empty sum

live ↗
InHarmonicNumber(0)\mathrm{HarmonicNumber}(0)Out00

H5(−1)=∑k=15k=15H_5^{(-1)} = \sum_{k=1}^5 k = 15 — a negative order is still exact

live ↗
InHarmonicNumber(5,−1)\mathrm{HarmonicNumber}(5, -1)Out1515

A floating-point argument evaluates via ψ(z+1)+γ\psi(z+1) + \gamma

live ↗
InHarmonicNumber(2.5)\mathrm{HarmonicNumber}(2.5)Out1.680 372 305 546 776 047 8371.680\,372\,305\,546\,776\,047\,837

H10(2)=∑k=110k−2H_{10}^{(2)} = \sum_{k=1}^{10} k^{-2}, exact

live ↗
InHarmonicNumber(10,2)\mathrm{HarmonicNumber}(10, 2)Out1 968 3291 270 080\frac{1\,968\,329}{1\,270\,080}
Scope #

Non-integer arguments via ψ(z+1)+γ\psi(z+1) + \gamma

live ↗
InHarmonicNumber(0.33)\mathrm{HarmonicNumber}(0.33)Out0.441 523 646 937 363 528 1110.441\,523\,646\,937\,363\,528\,111

The generalized form off the integers, via ζ(r)−ζ(r,z+1)\zeta(r) - \zeta(r, z+1)

live ↗
InHarmonicNumber(0.8,3)\mathrm{HarmonicNumber}(0.8, 3)Out0.940 124 048 948 776 980 4840.940\,124\,048\,948\,776\,980\,484

Complex arguments

live ↗
InHarmonicNumber(1.5+2i)\mathrm{HarmonicNumber}(1.5+2i)Out1.617 049 423 074 486 3+0.780 197 135 729 858 8i1.617\,049\,423\,074\,486\,3+0.780\,197\,135\,729\,858\,8i

Order r=1r = 1 off the integers is plain HzH_z: He=1.75002…H_e = 1.75002\ldots — routed to the one-argument form directly rather than through ζ(1)−ζ(1,z+1)\zeta(1) - \zeta(1, z+1), which is a pole at every zz

live ↗
InN(HarmonicNumber(e,1))N(\mathrm{HarmonicNumber}(e, 1))Out1.750 021 380 536 541 71.750\,021\,380\,536\,541\,7

Listable: threads over a list

live ↗
InHarmonicNumber([2,3,5,7,11])\mathrm{HarmonicNumber}(\bigl\lbrack2, 3, 5, 7, 11\bigr\rbrack)Out[32,116,13760,363140,83 71127 720]\bigl\lbrack\frac{3}{2}, \frac{11}{6}, \frac{137}{60}, \frac{363}{140}, \frac{83\,711}{27\,720}\bigr\rbrack

Threads elementwise over an array of orders, and H2(1/2)=1+1/2H_2^{(1/2)} = 1 + 1/\sqrt2 is a finite sum

live ↗
InHarmonicNumber(2,[[12,2],[2,12]])\mathrm{HarmonicNumber}(2, \bigl\lbrack\bigl\lbrack\frac{1}{2}, 2\bigr\rbrack, \bigl\lbrack2, \frac{1}{2}\bigr\rbrack\bigr\rbrack)Out[[1+22,54],[54,1+22]]\bigl\lbrack\bigl\lbrack1+\frac{\sqrt{2}}{2}, \frac{5}{4}\bigr\rbrack, \bigl\lbrack\frac{5}{4}, 1+\frac{\sqrt{2}}{2}\bigr\rbrack\bigr\rbrack

Interval arithmetic in the order

live ↗
InHarmonicNumber(0.2,Interval(2.1,2.2))\mathrm{HarmonicNumber}(0.2, \mathrm{Interval}(2.1, 2.2))OutInterval(0.386 346 941 184 175 9,0.395 092 392 240 286 8)\mathrm{Interval}(0.386\,346\,941\,184\,175\,9, 0.395\,092\,392\,240\,286\,8)

Uncertainty propagation in the order

live ↗
InHarmonicNumber(32,Around(2.1,0.01))\mathrm{HarmonicNumber}(\frac{3}{2}, \mathrm{Around}(2.1, 0.01))OutAround(1.145 516 434 049 132 6,0.000 879 982 646 698 109 1)\mathrm{Around}(1.145\,516\,434\,049\,132\,6, 0.000\,879\,982\,646\,698\,109\,1)

To exactly 30 significant digits, the last one correctly rounded

live ↗
InN(HarmonicNumber(12),30)N(\mathrm{HarmonicNumber}(\frac{1}{2}), 30)Out0.613 705 638 880 109 381 165 535 757 0840.613\,705\,638\,880\,109\,381\,165\,535\,757\,084
1 / 2

Order 1 gives HzH_z itself, not the ζ(1)\zeta(1) pole another order's formula would hit.

live ↗
InN(HarmonicNumber(2.5,1))N(\mathrm{HarmonicNumber}(2.5, 1))Out1.680 372 305 546 776 047 8371.680\,372\,305\,546\,776\,047\,837
Properties #

No sum below n=0n = 0

live ↗
InHarmonicNumber(−3)\mathrm{HarmonicNumber}(-3)Out∞~\tilde∞

H1/2=2−2ln⁡2H_{1/2} = 2 - 2\ln 2: exact at rational arguments by Gauss's digamma theorem

live ↗
InHarmonicNumber(12)\mathrm{HarmonicNumber}(\frac{1}{2})Out2−2ln⁡(2)2-2\ln(2)

H1/4=4−π2−3ln⁡2H_{1/4} = 4 - \tfrac{\pi}{2} - 3\ln 2

live ↗
InHarmonicNumber(14)\mathrm{HarmonicNumber}(\frac{1}{4})Out4−3ln⁡(2)−π24-3\ln(2)-\frac{π}{2}

Symbolic nn: Hn(−1)=n(n+1)/2H_n^{(-1)} = n(n+1)/2, a Faulhaber sum, expanded as Wolfram does

live ↗
InHarmonicNumber(n,−1)\mathrm{HarmonicNumber}(n, -1)Outn22+n2\frac{n^2}{2}+\frac{n}{2}

The harmonic series diverges

live ↗
InHarmonicNumber(∞)\mathrm{HarmonicNumber}(∞)Out∞∞

H∞(r)=ζ(r)H_\infty^{(r)} = \zeta(r) for r>1r > 1: the Basel sum

live ↗
InHarmonicNumber(∞,2)\mathrm{HarmonicNumber}(∞, 2)Outπ26\frac{π^2}{6}
Possible issues #

A non-integer order stays symbolic under plain evaluation, even at an exact nn; N() gives 3.23167…3.23167\ldots

live ↗
InHarmonicNumber(5,12)\mathrm{HarmonicNumber}(5, \frac{1}{2})OutHarmonicNumber(5,12)\mathrm{HarmonicNumber}(5, \frac{1}{2})
About
Examples48
Checked againstWolfram Language 46 agree2 differSageMath 41 agree3 differ2 unevaluatedSymPy 44 agree2 differ1 unevaluatedmpmath 39 agree1 unevaluated4 errorJulia 38 agree1 unevaluated5 errorRust 12 agree3 differ1 unevaluated20 error
CrosswalkFungrimFungrim entry ×2Wolfram LanguageSageMath ×2SymPy ×2mpmathJulia ×2Rust ×2

Implementation

referenceenginenotatio

The one-argument digamma identity; exact at the integers too (ψ(n+1) + γ = Hₙ), so it doubles as the oracle at both.

nativeenginetypescriptpackages/symbols/analysis/analytic/src/harmonic.ts
mappedexternalwolfram / mpmathpackages/oracle/src/mappings.ts

HarmonicNumber[n] / HarmonicNumber[n, r]; mpmath.harmonic(n) (one-argument only — mpmath has no generalized order).

mappedsympypackages/oracle/src/mappings.ts
mappedsympypackages/oracle/src/mappings.ts
mappedmpmathpackages/oracle/src/mappings.ts
mappedmpmathpackages/oracle/src/mappings.ts

H(n, r) = ζ(r) − ζ(r, n + 1), the plain harmonic number at r = 1.

mappedsagepackages/oracle/src/mappings.ts
mappedsagepackages/oracle/src/mappings.ts
mappedjuliapackages/oracle/src/mappings.ts
mappedjuliapackages/oracle/src/mappings.ts
mappedrustpackages/oracle/src/mappings.ts

Non-negative integer orders only.

mappedrustpackages/oracle/src/mappings.ts

A non-negative integer order only.

See also: PolyGamma, EulerGamma, Zeta, HurwitzZeta