# Summation of logs

Are there any useful identities for quickly calculating the sum of consecutive logs? For example $\sum_{k=1}^{N} log(k)$ or something to this effect. I should add that I am writing code to do this (as opposed to doing this on a calculator) so N can be very large.

• $\log a+\log b=\log (a\cdot b)$ – lab bhattacharjee May 8 '13 at 14:57

## 4 Answers

For large $N$, we have $N!\approx N^Ne^{-N}\sqrt {2\pi N}$ (Stirling formula) and hence $$\sum_{k=1}^N\ln k\approx\left( N+\frac12\right)\ln N-N+\frac12\ln(2\pi).$$

The Euler-Maclaurin Sum Formula, can also be used to get an asymptotic expansion. It gives $$\sum_{k=1}^n\log(k)=\overbrace{\vphantom{\frac12}C}^{\frac12\log(2\pi)}+\overbrace{\vphantom{\frac12}n\log(n)-n}^{\int f(n)\,\mathrm{d}n}+\overbrace{\frac12\log(n)}^{\frac12f(n)}+\overbrace{\frac1{12n}}^{\frac1{12}f'(n)}-\overbrace{\frac1{360n^3}}^{\frac1{720}f'''(n)}+\dots$$ The constant $\frac12\log(2\pi)$ is derived as in the proof of Stirling's Formula.

Hint:

Use the fact that $\log(a)+\log(b)=\log(ab)$

Your expression simply becomes $\sum_1^N \log k=\log(N!)$, and now you can have Stirling Approximation to approximate $N!$

• I am trying to do this with a computer, so for large $N$, calculating N! may not be possible – user74255 May 8 '13 at 14:58
• @Inceptio, it's better to show the means than end – lab bhattacharjee May 8 '13 at 14:59
• @labbhattacharjee: Considered it. Edited.:) – Inceptio May 8 '13 at 15:03
• Will the approximation of N! interfere with the log calculation? I am not sure what O(ln(n)) is – user74255 May 8 '13 at 15:04
• @user74255: No. You can calculate the approximate value of it, and then take $\log$ of it. – Inceptio May 8 '13 at 15:07

In the particular case of $$\sum_{k=1}^N\log k=\log N!=\log\Gamma(N+1)$$ this is just the loggama function, which is implemented in many software systems. This is fast and accurate.

For example, it is lngamma in GP, lgamma in C (math.h), LogGamma in Mathematica, lnGAMMA in Maple, LogGamma in Magma, gammaln in MatLab, lnGamma in Mathcad, log_gamma in Sage, math.lgamma in Python, and gammaln in Perl (Math::SpecFun::Gamma).