TITLE: Limit approach to finding $1+2+3+4+\ldots$
QUESTION [18 upvotes]: When exploring the divergent series consisting of the sum of all natural numbers
$$\sum_{k=1}^\infty k=1+2+3+4+\ldots$$
I came across the following identity involving a one-sided limit:
$$\lim_{x\to0^-}\sum_{k=1}^\infty k\exp(kx)\cos(kx)=-\frac{1}{12}$$
Using zeta function regularization yields the same value:
$$\sum_{k=1}^\infty k^{-s}=\zeta(s)$$
$$\zeta(-1)=-\frac{1}{12}$$
In general, I found that for $y\neq0$ and $n=1,5,9,13,\ldots$ (i.e. $4m+1$ where $m\in\mathbb{N}$),
$$\lim_{x\to0^-}\sum_{k=1}^\infty k^n\exp(kxy)\cos(kxy)=\zeta(-n)$$
However, a similar limit did not exist for other powers of $k$, e.g.
$$\lim_{x\to 0^-}\sum_{k=1}^\infty k^2\exp(kx)\cos(kx)$$
$$\lim_{x\to 0^-}\sum_{k=1}^\infty k^3\exp(kx)\cos(kx)$$
The regularized values of the corresponding series are $\zeta(-2)=0$ and $\zeta(-3)=\frac{1}{120}$.
Given this information, I have the following questions:

What is the connection between the limit approach and the zeta function approach?
Why does the limit expression only seem to converge for $n=4m+1$?
Can the limit approach be used to find the sum for other powers of $k$? If so, how?

Here is a plot I made with Mathematica using the command
Plot[Evaluate[Sum[k*Exp[x*k]*Cos[x*k], {k, 1, Infinity}]], {x, -16, 16}, PlotRange -> {-.25, .25}, AspectRatio -> 1]


Notice how it approaches $-1/12\approx-0.08333$ as $x$ approaches $0$.
Further information:
$$\sum_{k=1}^\infty k^n\exp(kx)=\mathrm{Li}_{-n}(\exp(x))=\frac{n!}{(-x)^{n+1}}+\zeta(-n)+O(x)$$

Edit:
Based on Micah's answer, we seek an $f$ such that $f(s,0) = 1$ and
\begin{align}
\int_0^\infty x^s f(s,x) \,\mathrm{d}x &= 0
\end{align}
Let
\begin{align}
f(s,x) &= \mathrm{e}^{-x}(1+ax)
\end{align}
Then $f(s,0) = 1$. Assuming $\mathrm{Re}(s) > -1$,
\begin{align}
0
&= \int_0^\infty x^s f(s,x) \,\mathrm{d}x \\
&= \int_0^\infty x^s \mathrm{e}^{-x}(1+ax) \,\mathrm{d}x \\
&= \int_0^\infty x^s \mathrm{e}^{-x} \,\mathrm{d}x + a \int_0^\infty x^{s+1} \mathrm{e}^{-x} \\
&= \Gamma(s+1) + a (s+1) \Gamma(s+1) \\
&= (1 + a(s+1)) \Gamma(s+1) \\
\Longrightarrow a &= -\frac{1}{s+1} \\
\Longrightarrow f(s,x) &= \mathrm{e}^{-x}\left( 1 - \frac{x}{s+1} \right)
\end{align}
Thus
\begin{align}
\zeta(-s)
&= \lim_{\varepsilon \rightarrow 0^+} \lim_{m \rightarrow \infty} \sum_{n=1}^m n^s f(s, n \varepsilon) \\
&\overset{\star}{=} \lim_{m \rightarrow \infty} \lim_{\varepsilon \rightarrow 0^+} \sum_{n=1}^m n^s f(s, n \varepsilon) \\
&= \lim_{m \rightarrow \infty} \sum_{n=1}^m n^s f(s, 0) \\
&= \lim_{m \rightarrow \infty} \sum_{n=1}^m n^s \\
&= \sum_{n=1}^\infty n^s
\end{align}
where the star indicates the non-rigorous step of exchanging limits. In fact, this regulator seems to work for all $s \neq -1$ (see some plots for $s < -1$ below). Hence we can also regularize the harmonic series as follows:
\begin{align}
\gamma
&= \frac{1}{2} \lim_{\varepsilon \rightarrow 0^+} (\zeta(1+\varepsilon) + \zeta(1-\varepsilon)) \\
&= \frac{1}{2} \lim_{\varepsilon \rightarrow 0^+} \left(\lim_{m \rightarrow \infty} \sum_{n=1}^m n^{-1-\varepsilon} f(-1-\varepsilon, n \varepsilon) + \lim_{m \rightarrow \infty} \sum_{n=1}^m n^{-1+\varepsilon} f(-1+\varepsilon, n \varepsilon) \right) \\
&= \frac{1}{2} \lim_{\varepsilon \rightarrow 0^+} \lim_{m \rightarrow \infty} \left(\sum_{n=1}^m n^{-1-\varepsilon} f(-1-\varepsilon, n \varepsilon) + \sum_{n=1}^m n^{-1+\varepsilon} f(-1+\varepsilon, n \varepsilon) \right) \\
&\overset{\star}{=} \frac{1}{2} \lim_{m \rightarrow \infty} \lim_{\varepsilon \rightarrow 0^+} \left(\sum_{n=1}^m n^{-1-\varepsilon} f(-1-\varepsilon, n \varepsilon) + \sum_{n=1}^m n^{-1+\varepsilon} f(-1+\varepsilon, n \varepsilon) \right) \\
&= \frac{1}{2} \lim_{m \rightarrow \infty} \left(\sum_{n=1}^m n^{-1} f(-1, 0) + \sum_{n=1}^m n^{-1} f(-1, 0) \right) \\
&= \frac{1}{2} \lim_{m \rightarrow \infty} \left(\sum_{n=1}^m n^{-1} + \sum_{n=1}^m n^{-1} \right) \\
&= \lim_{m \rightarrow \infty} \sum_{n=1}^m n^{-1} \\
&= \sum_{n=1}^\infty n^{-1} \\
\end{align}
Here is the Mathematica code and its plots:
f[s_, x_] := Exp[-x] (1 + a x)
g[s_, t_] := 
 Evaluate@Simplify[
   f[s, t] /. Solve[Integrate[x^s f[s, x], {x, 0, Infinity}] == 0, a],
    Assumptions -> Re[s] > -1]
g[s, t]
Table[{s, 
    Plot[{Zeta[-s], 
      Sum[n^s g[s, n \[Epsilon]], {n, 1, 1000}]}, {\[Epsilon], 0, 1}, 
     Evaluated -> True]}, {s, -4, 4, 1/2}] // TableForm // Quiet
Plot[{EulerGamma, 
  Sum[(n^(-1 + \[Epsilon]) g[-1 + \[Epsilon], n \[Epsilon]] + 
    n^(-1 - \[Epsilon]) g[-1 - \[Epsilon], n \[Epsilon]])/
   2, {n, 1, 1000}]}, {\[Epsilon], 0, 1}, Evaluated -> True]

REPLY [7 votes]: Explanation for the Behavior of the Series (Parts $2$ and $3$)
Note that
$$
\begin{align}
\sum_{k=0}^\infty e^{ikx}
&=\frac1{1-e^{ix}}\\
&=\frac12+\frac i2\cot\left(\frac x2\right)\\
&=\frac ix+\frac12-i\,\underbrace{\left(\frac1x-\frac12\cot\left(\frac x2\right)\right)}_{\substack{\text{odd function of $x$}\\\text{analytic on $(-\pi,\pi)$}}}\tag{1}
\end{align}
$$

Odd Exponents
Taking $2n-1$ derivatives of $(1)$, we get
$$
\sum_{k=0}^\infty k^{2n-1}e^{ikx}=\frac{(-1)^n(2n-1)!}{x^{2n}}+(-1)^n\underbrace{\frac{\mathrm{d}^{2n-1}}{\mathrm{d}x^{2n-1}}\left(\frac1x-\frac12\cot\left(\frac x2\right)\right)}_{\substack{\text{even function of $x$}\\\text{analytic on $(-\pi,\pi)$}}}\tag{2}
$$

Exponents which are $\boldsymbol{1\bmod{4}}$
If $n$ is odd, so that $2n-1\equiv1\pmod{4}$, substitute $x=(1-i)y$ in $(2)$ to get
$$
\sum_{k=0}^\infty k^{2n-1}e^{k(1+i)y}
=\underbrace{\frac{(2n-1)!}{(2i)^ny^{2n}}}_{\substack{\text{pure imaginary}\\\text{since $n$ is odd}}}
-\underbrace{\frac{\mathrm{d}^{2n-1}}{\mathrm{d}x^{2n-1}}\left(\frac1x-\frac12\cot\left(\frac x2\right)\right)}_{\text{plug in $x=(1-i)y$}}\tag{3}
$$
Thus, the real part of $(3)$ is
$$
\sum_{k=0}^\infty k^{2n-1}e^{ky}\cos(ky)=\left.-\frac{\mathrm{d}^{2n-1}}{\mathrm{d}x^{2n-1}}\left(\frac1x-\frac12\cot\left(\frac x2\right)\right)\right|_{x=(1-i)y}\tag{4}
$$
Taking the limit of $(4)$ as $y\to0$ yields the identity given in the question
$$
\begin{align}
&\text{When $n$ is odd; that is, }2n-1\equiv1\pmod{4}:\\
&\bbox[5px,border:2px solid #C0A000]{\lim_{y\to0^-}\sum_{k=0}^\infty k^{2n-1}e^{ky}\cos(ky)=\left.-\frac{\mathrm{d}^{2n-1}}{\mathrm{d}x^{2n-1}}\left(\frac1x-\frac12\cot\left(\frac x2\right)\right)\right|_{x=0}}\tag{5}
\end{align}
$$

Exponents which are $\boldsymbol{3\bmod{4}}$
If $n$ is even, so that $2n-1\equiv3\pmod{4}$, the situation is a bit more complicated. We can still substitute $x=(1-i)y$ in $(2)$, getting
$$
\sum_{k=0}^\infty k^{2n-1}e^{k(1+i)y}
=\underbrace{\frac{(2n-1)!}{(2i)^ny^{2n}}}_{\text{real since $n$ is even}}
+\underbrace{\frac{\mathrm{d}^{2n-1}}{\mathrm{d}x^{2n-1}}\left(\frac1x-\frac12\cot\left(\frac x2\right)\right)}_{\text{plug in $x=(1-i)y$}}\tag{6}
$$
We can also substitute $x=-iy$ in $(2)$, getting
$$
\sum_{k=0}^\infty k^{2n-1}e^{ky}=\frac{(2n-1)!}{y^{2n}}+\underbrace{\frac{\mathrm{d}^{2n-1}}{\mathrm{d}x^{2n-1}}\left(\frac1x-\frac12\cot\left(\frac x2\right)\right)}_{\text{plug in $x=-iy$}}\tag{7}
$$
Taking a linear combination of $(6)$ and $(7)$ to cancel the singular part and maintain the constant part, we get
$$
\begin{align}
&\text{When $n$ is even; that is, }2n-1\equiv3\pmod{4}:\\
&\bbox[5px,border:2px solid #C0A000]{\lim_{y\to0^-}\sum_{k=0}^\infty k^{2n-1}e^{ky}\frac{2^n\cos(ky)-(-1)^{n/2}}{2^n-(-1)^{n/2}}
=\left.\frac{\mathrm{d}^{2n-1}}{\mathrm{d}x^{2n-1}}\left(\frac1x-\frac12\cot\left(\frac x2\right)\right)\right|_{x=0}}\tag{8}
\end{align}
$$

Even Exponents
Taking $2n$ derivatives of $(1)$, we get
$$
\sum_{k=0}^\infty k^{2n}e^{ikx}
=(-1)^n\frac{(2n)!}{x^{2n+1}}i
-(-1)^ni\underbrace{\frac{\mathrm{d}^{2n}}{\mathrm{d}x^{2n}}\left(\frac1x-\frac12\cot\left(\frac x2\right)\right)}_{\substack{\text{odd function of $x$}\\\text{analytic on $(-\pi,\pi)$}}}\tag{9}
$$
Substitute $x=(1-i)y$ in $(9)$ to get
$$
\sum_{k=0}^\infty k^{2n}e^{k(1+i)y}
=-\frac{(2n)!}{y^{2n+1}}\frac{1+i}{(2i)^{n+1}}
-(-1)^ni\underbrace{\frac{\mathrm{d}^{2n}}{\mathrm{d}x^{2n}}\left(\frac1x-\frac12\cot\left(\frac x2\right)\right)}_{\text{plug in $x=(1-i)y$}}\tag{10}
$$
We can also substitute $x=-iy$ in $(9)$ to get
$$
\sum_{k=0}^\infty k^{2n}e^{ky}
=-\frac{(2n)!}{y^{2n+1}}
-(-1)^ni\underbrace{\frac{\mathrm{d}^{2n}}{\mathrm{d}x^{2n}}\left(\frac1x-\frac12\cot\left(\frac x2\right)\right)}_{\text{plug in $x=-iy$}}\tag{11}
$$
Taking a linear combination of $(10)$ and $(11)$ to cancel the singular part and maintain the constant part, we get
$$
\bbox[5px,border:2px solid #C0A000]{\lim_{y\to0^-}\sum_{k=0}^\infty k^{2n}e^{ky}\frac{2^{n+1}\cos(ky)+(-1)^{\lfloor(n-1)/2\rfloor}}{2^{n+1}+(-1)^{\lfloor(n-1)/2\rfloor}}=0}\tag{12}
$$
where $(-1)^{\lfloor(n-1)/2\rfloor}=\mathrm{Re}\left(\frac{i-1}{i^n}\right)$.

Unified Expression
Combining $(5)$, $(8)$, and $(12)$ yields
$$
\bbox[5px,border:2px solid #C0A000]{\lim_{x\to0^-}\sum_{k=0}^\infty k^ne^{kx}\frac{2^{\lceil n/2\rceil}\cos(kx)+c_n}{2^{\lceil n/2\rceil}+c_n}=\zeta(-n)}\tag{13}
$$
where $c_n$ is given by
$$
c_n=-\tfrac14(-1)^{\lceil n/4\rceil}\left(2+(-1)^{\lceil n/2\rceil}\left(1-(-1)^n\right)\right)
$$
or by
$$
c_n=-(-1)^{\lceil n/4\rceil}\sin^2\left(\tfrac\pi4(n-1)\right)
$$
or in the following table
$$
\small\begin{array}{c|r|r|r|r|r|r|r}
n\bmod8&1&2&3&4&5&6&7&8\\
\hline c_n&\hphantom{-}0&\hphantom{-}\frac12&\hphantom{-}1&\hphantom{-}\frac12&\hphantom{-}0&-\frac12&-1&-\frac12
\end{array}\tag{14}
$$