How to NIntegrate $(-1)^{\left\lfloor \frac1x\right\rfloor}/x$?

Mathematica fails with

Integrate[(-1)^Floor[1/x]/x, {x, 0, 1}]

How can I evaluate it?

In view of it, I unsuccessfully try

NIntegrate[(-1)^Floor[1/x]/x, {x, 0, 1}, AccuracyGoal -> 4,
Method -> {"GlobalAdaptive", "MaxErrorIncreases" -> 10000,
Method -> "GaussKronrodRule"}, MaxRecursion -> 20]

NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 20 recursive bisections in x near {x} = {6.08983877753243723225686862226814152545986400616302122725995044034*10^-325}. NIntegrate obtained 710.5859812076798and 3.956615692341655 for the integral and error estimates.

710.586

This question is inspired by this discussion.

Mathematica needs to be helped a bit for this problem, as neither Integrate[] nor NIntegrate[] are sufficiently smart enough.

We proceed as in a hand calculation: letting $n\in\mathbb N$, we consider the integral

$$\int_{\frac1{n+1}}^{\frac1n}\frac{(-1)^{\lfloor\frac1x\rfloor}}{x}\mathrm dx$$ and then sum up all the terms.

Mathematica is still unable to deal with this integral, but evaluating it for increasing $n$ reveals a pattern:

Table[Integrate[(-1)^Floor[1/x]/x, {x, 1/(n + 1), 1/n}], {n, 8}]
{-Log, Log[3/2], -Log[4/3], Log[5/4], -Log[6/5], Log[7/6], -Log[8/7], Log[9/8]}

and we find that the general term is $(-1)^n\log\left(1+\frac1n\right)$. (I tried to use FindSequenceFunction[] on this sequence, but it took too long and I lost patience.)

A plausibility check:

Block[{$MaxPiecewiseCases = 120, n = 100}, Integrate[(-1)^Floor[1/x]/x, {x, 1/(n + 1), 1}] == Sum[(-1)^k Log[1 + 1/k], {k, n}] // Simplify] True and the OP's integral is thus equivalent to evaluating $$\sum_{n=1}^\infty (-1)^n\log\left(1+\frac1n\right)$$ which Sum[] is unable to do, but is easily dealt with by NSum[]: NSum[(-1)^k Log[1 + 1/k], {k, 1, ∞}, Method -> "AlternatingSigns", WorkingPrecision -> 40] -0.451582705289454864726195229894882608267 If you really must use NIntegrate[], one could generate the sequence of integrals $$\int_{\frac1n}^1\frac{(-1)^{\lfloor\frac1x\rfloor}}{x}\mathrm dx$$ and accelerate the convergence rate of the sequence using the Wynn$\varepsilon$algorithm, via NumericalMathNSequenceLimit[] (SequenceLimit[] in earlier versions): NumericalMathNSequenceLimit[Table[ NIntegrate[(-1)^Floor[1/x]/x, {x, 1/n, 1}, Method -> {"SymbolicPiecewiseSubdivision", "MaxPiecewiseCases" -> (n + 2)}, WorkingPrecision -> 50], {n, 2, 30}]] -0.45158270528945486472619524 Note the use of "SymbolicPiecewiseSubdivision" to ensure the splitting of the integral into intervals where a polynomial approximation will be fine, and "MaxPiecewiseCases" will ensure that the whole business is split up. Mariusz's answer shows how to get a closed form answer. Here's one way to do it in Mathematica. First, note the following: {D[(-1)^(n - 1)/(n + 1)^z, z], D[(-1)^n/n^z, z]} /. z -> 0 {(-1)^n Log[1 + n], (-1)^(1 + n) Log[n]} Thus, Sum[D[(-1)^(n - 1)/(n + 1)^z, z], {n, 1, ∞}] + Sum[D[(-1)^n/n^z, z], {n, 1, ∞}] /. z -> 0 // FullSimplify Log[2/π] • +1. Nice, but rather by hand with help of Mathematica. The question remains open: how to NIntegrate that? – user64494 Mar 24 '18 at 10:48 • FindSequenceFunction fails with it in short time. – user64494 Mar 24 '18 at 10:58 • Now I accept it. – user64494 Mar 24 '18 at 11:18 The OP's integral is thus equivalent to evaluating the sum: $$\sum _{n=1}^{\infty } (-1)^n \ln \left(1+\frac{1}{n}\right)$$ Using identity: $$\int_0^{\infty } \frac{(1-\exp (-t)) \exp (-n t)}{t} \, dt=\ln\left(1+\frac{1}{n}\right)$$ puting to sum: $$\sum _{n=1}^{\infty } \frac{(-1)^n ((1-\exp (-t)) \exp (-n t))}{t}=-\frac{1-e^{-t}}{t+e^t t}$$ and integrating: $$\int_0^{\infty } -\frac{1-e^{-t}}{t+e^t t} \, dt=\ln \left(\frac{2}{\pi }\right)$$ Closed form solution: $$\color{blue}{\int_0^1 \frac{(-1)^{\left\lfloor \frac{1}{x}\right\rfloor }}{x} \, dx=\sum _{n=1}^{\infty } (-1)^n \ln \left(1+\frac{1}{n}\right)=\ln \left(\frac{2}{\pi }\right)}$$ EDITED: Solution by: Maple 2018 Alternative how find closed form solution ?: $$\sum _{n=1}^{\infty } (-1)^n \ln \left(1+\frac{1}{n}\right)=\sum _{n=1}^{\infty } -(-1)^n \ln (n)+\sum _{n=1}^{\infty } (-1)^n \ln (n+1)$$ By Regularization Sums Sum[-(-1)^n*Log[n], {n, 1, Infinity}, Regularization -> "Abel"] + Sum[(-1)^n*Log[n + 1], {n, 1, Infinity}, Regularization -> "Abel"] // FullSimplify (* Log[2/π] *) A Feynman's trick: s1 = Sum[-(-1)^n*n^k, {n, 1, Infinity}] + Sum[(-1)^n*(n + 1)^k, {n, 1, Infinity}] // FullSimplify Limit[D[s1, k], k -> 0] // FullSimplify (* Log[2/π] *) • Neat idea to use Frullani's integral. (IOU one upvote.) – J. M. will be back soon Mar 24 '18 at 15:09 • @J.M.Analytic solution is always the best solution no need to struggling with NSum or NIntegrate. – Mariusz Iwaniuk Mar 24 '18 at 15:15 • @MariuszIwaniuk what is the role of$k$in the sum of your closed form solution? – José Antonio Díaz Navas Mar 24 '18 at 17:26 • @JoséAntonioDíazNavas. A typo.Corrected.Thanks.:) – Mariusz Iwaniuk Mar 24 '18 at 17:32 • @MariuszIwaniuk, impressed how you obtained the closed-form (+1) – José Antonio Díaz Navas Mar 24 '18 at 17:37 Sum[(-1)^n Log[1 + 1/n], {n, 1, Infinity}] returns unevaluated, but the following product gives a closed form Log[Product[(1 + 1/(2*k)) / (1 + 1/(2*k - 1)), {k, 1, Infinity}]] (* Log[2/Pi] *) N[%, 40] (* -0.4515827052894548647261952298948821435718 *) • How come this Product,Can you explain ? – Mariusz Iwaniuk Mar 25 '18 at 21:07 • @Mariusz Iwaniuk: If n is even then (-1)^nLog[1 + 1/n] = Log[1 + 1/n]. If n is odd then (-1)^nLog[1 + 1/n] = -Log[1 + 1/n] = Log[1/(1 + 1/n)]. And Log[a] + Log[b] + Log[c] + ... = Log[abc*...]. It's clear now ? – Vaclav Kotesovec Mar 25 '18 at 21:47 • Yes it's clear. Thanks :) – Mariusz Iwaniuk Mar 25 '18 at 22:34 This is the same calculation from another point of view. We can realise that after the variable change$t=1/x$, the integrand is equal to: $$\frac{(-1)^{\lfloor t\rfloor }}{t}$$ and integrated in$(1,\infty)$. After plotting the function: we can calculate easily the integrals and, summing them: NSum[Evaluate@Integrate[(-1)^Floor[n]*1/t, {t, n, n + 1}], {n, 1, ∞}, Method -> "AlternatingSigns"] (* -0.451583 *) or integrating piecewisely in$(n,n+1)$, with$n\in \mathrm{N}$: Assuming[n > 1 && n ∈ Integers, Integrate[(-1)^Floor[n]*t^-1, {t, n, n + 1}]] $$(-1)^{ n } \log \left(\frac{1}{n}+1\right)$$ and summing: NSum[(-1)^n Log[1 + 1/n], {n, 1, ∞}, Method -> "AlternatingSigns"] (* -0.451583 *) By using the Frullani's integral identity proposed by @MariuszIwaniuk: $$\int_{(0,\infty)} \frac{f(ax)-f(bx)}{x} dx=(f(0)-f(\infty)) \log \frac{b}{a}$$ his closed form is obtained: $$\int_0^1 \frac{(-1)^{\left\lfloor \frac{1}{x}\right\rfloor }}{x} \, dx=\sum _{n=1}^{\infty } (-1)^n \log \left(1+\frac{1}{n}\right)=\log \left(\frac{2}{\pi }\right)$$ where$\log\$ is the natural logarithm in this case.

• When you changed variables, did you remember to differentiate your substitution? – J. M. will be back soon Mar 24 '18 at 13:21
• @J.M., yes, I found my error. Good catch ! thnx... – José Antonio Díaz Navas Mar 24 '18 at 16:44

Slightly more automized

Block[{x, n},
NSum[
(-1)^n Evaluate[ Integrate[1/x, {x, 1/(n + 1), 1/n}, Assumptions -> n > 0]],
{n, 1, ∞}]
] // AbsoluteTiming

{0.806038, -0.451583}

• No output during several minutes in version 11.3 on Windows 10. Did you check your suggestion? – user64494 Mar 24 '18 at 11:07
• @user, it works quickly in 11.2; what version are you using? – J. M. will be back soon Mar 24 '18 at 11:08
• Works, but does not result. – user64494 Mar 24 '18 at 11:10
• I use version 11.3 on macos and it returns -0.4515827052894538` after a second or so. Have you tried to clean the kernel? If x or n have values assigned to them then Evaluate might do shabby things. – Henrik Schumacher Mar 24 '18 at 11:13
• @Henrik Schumacher: Thank you for your advice. Now it works well. – user64494 Mar 24 '18 at 11:17