# Speed up definite integral of 1/x^(n/2)

I'm a bit buffled by how terribly slow this is:

Table[Integrate[1/x^(n/2), {x, 2 a, b + c}, Assumptions -> 0 < 2 a < b + c], {n, 1, 20}]; // AbsoluteTiming
{8.9362, Null}

In contrast, without the bounds I get:

Table[Integrate[1/x^(n/2), x], {n, 1, 20}]; // AbsoluteTiming
{0.0106568, Null}

Why is the first way so slow and how can I speed it up? Of course I can resort to doing it manually, but I was looking for a built-in solution.

myIntegrate[f_, var_, min_, max_] :=
With[{F = Integrate[f, var]}, (F /. var -> max) - (F /. var -> min)]

Table[myIntegrate[1/x^(n/2), x, 2 a, b + c], {n, 1, 20}]; // AbsoluteTiming
{0.0121743, Null}

Your original code is slow because it repeats almost the same integration 20 times. Since the integration is the most time consuming part, integrate just once and reuse the result. If I were you, I would proceed as follows:

Timing[int=Integrate[1/x^(n/2),{x,2a,b+c},Assumptions->0<2a<b+c]]

Timing[ans=Table[{n,int},{n,1,20}];]
(* {0.001437, Null} *)

There are some error message, such as Infinite expression 1/0 encountered, etc. Let us check the answer:

Grid[ans, Dividers -> All]

The error occurred when n=2. The correct result would be probably given by

Limit[int, n->2]

-Log[a] + Log[(b + c)/2]

Of course, if you feel this limiting procedure is illegal, you can make independent computation for n=2.

\$Version

(* "14.0.0 for Mac OS X ARM (64-bit) (December 13, 2023)" *)

Clear["Global`*"]

(tab1 = Assuming[0 < 2  a < b + c, Integrate[
1/x^(Range[20]/2), {x, 2  a, b + c}]];) // AbsoluteTiming

(* {7.86243, Null} *)

(tab2 = Assuming[0 < 2  a < b + c,
(# /. x -> b + c) - (# /. x -> 2 a) & /@
Integrate[1/x^(Range[20]/2), x]];) // AbsoluteTiming

(* {0.003101, Null} *)

Assuming[0 < 2  a < b + c, tab1 - tab2 == 0 // Simplify]

(* True *)
• I appreciate the answer. But isn't this basically my approach from above? Commented Jul 25 at 20:54
• This makes use of the Listable attribute instead of Table. Commented Jul 25 at 20:56