#### alternating series, sum and add (22.11.99)

##### John Kurtzke

I have found Maple adds terms in a series very quickly, but I have had problems with the alternating harmonic series, sum( (-1)^(n+1)/n ) It will sum this up to 1000 quickly, but with 10,000 or more, Maple gobbles up memory to the limit and locks things up.

Speciﬁcally:

evalf( sum( ((-1)^(n+1))/n, n=1..N) );

works well when N= 1000, but not at all for N = 10000.

I then broke things up to even & odd and Maple computed the sums in a ﬂash:

odds := evalf( sum( 1/(2*n+1), n=1..N) );
evens := evalf( sum(1/(2*n), n=1..N) );

I used 10000 for N and Maple computed these two sums in the blink of an eye.

I suspect Maple is having trouble with the alternating nature of the original series, but I don’t know why.

This has happened using Maple V Release 5.0 on both my (old) Power Mac 8100/100 and on a Pentium III PC in one of our computer classrooms.

##### Bill Bauldry (23.11.99)

Consider using an inert Sum instead of sum. The sum attempts an antidiﬀerence, then goes to the evalf. The Sum just dumps to evalf. Here’s what I get on a Mac G3:

> restart;
> S := N -> evalf( sum( ((-1)^(n+1))/n, n=1..N) ):
> SI := N -> evalf( Sum( ((-1)^(n+1))/n, n=1..N) ):
> time():
> evaln(S) = S(1000);
> (time() - %%)*seconds;

> time():
> evaln(SI) = SI(1000);
> (time() - %%)*seconds;
S = .6926474306
.317 seconds

SI = .6926474306
.067 seconds
> time():
> evaln(S) = S(10000);
> (time() - %%)*seconds;

> time():
> evaln(SI) = SI(10000);
> (time() - %%)*seconds;
S = .6930971831
12.333 seconds

SI = .6930971831
.783 seconds

##### Robert Israel (23.11.99)

I don’t think that’s it at all. The ”sum” function is for symbolic summation. It ﬁnds sum((-1)^(n+1)/n, n=1..N) (with N left unassigned) as

                          (N + 1)
ln(2) + 1/2 (-1)        (Psi(1 + 1/2 N) - Psi(1/2 N + 1/2))
and it finds sum(1/n, n=1..N) as

Psi(N + 1) + gamma
It finds sum(1/n, n=1..10000) as

Psi(10001) + gamma

(which ”evalf” can then handle, giving a numerical answer 9.787606026). However, for some reason (I don’t know exactly why) it doesn’t calculate the symbolic solution for sum((-1)^n/n, n=1..N) when N has a numerical value; instead it simply adds the terms of the sum. This is done with rational arithmetic, so the result will be a rational number with huge numerator and denominator: for N=10000 the numerator and denominator each have 4346 digits, and I expect that for N=10000 they will have about 4320 digits.

If you want a numerical answer obtained from adding up the individual terms, you can use

> evalf(Sum((-1)^(n+1)/n, n=1..10000));

Or if you want to evaluate the symbolic answer numerically, you can use

> evalf(eval(sum((-1)^(n+1)/n, n=1..N), N=10000));

There are a number of issues involved with your observation. I am working with Release 5.1 under Solaris (UNIX) so some of the memory issues are a little diﬀerent.

First, you need to be aware of the diﬀerences between the sum and add commands. The sum (and Sum) commands attempt to ﬁnd explicit formulae for deﬁnite and indeﬁnite sums; only add actually adds each term in the sum. (Well, sum will add the terms if all attempts to ﬁnd an explicit formula fail.)

The following session illustrates some of these diﬀerences. Note how Maple automatically simpliﬁes the sum when sum is used. This makes all evaluations extremely fast. The next section, which uses the intert summation, Sum, is similar.

It is somewhat disturbing that Maple could not make up its mind whether to return a diﬀerence of Psi functions or the LerchPhi function, but that is an issue for the developers (hint, hint). In the third section, the sum is computed using add.

Here we see a slowdown as N increases. In this demonstration the timings are slight superlinear; in other tests they were much more linear. The speciﬁcs of these timings are likely to be very dependent on the amount of memory, etc.

I hope you will ﬁnd this response informative, even if it only indirectly addresses your question about even and odd sums.

> restart;
> s := sum( (-1)^(n+1)/n, n=1..N );

(N + 1)
s := ln(2) + 1/2 (-1)        (Psi(1 + 1/2 N) - Psi(1/2 N + 1/2))

> st := time(): N := 10^3: evalf( s ); time()-st;

.692647431    .010

> st := time(): N := 10^4: evalf( s ); time()-st;

.693097184     0

> st := time(): N := 10^5: evalf( s ); time()-st;

.693142181     0

> st := time(): N := 10^3-1: evalf( s ); time()-st;

.693647431     0

> st := time(): N := 10^4-1: evalf( s ); time()-st;

.693197184     0

> st := time(): N := 10^5-1: evalf( s ); time()-st;

.693152181     0

> restart;
> S := Sum( (-1)^(n+1)/n, n=1..N );

> st := time(): value( S ); time()-st;

(N + 1)
ln(2) + (-1)        (-LerchPhi(-1, 1, N) + 1/N)

.210

> st := time(): N := 10^3: evalf( S ); time()-st;

.6926474306     .080

> st := time(): N := 10^4: evalf( S ); time()-st;

.6930971831    1.000

> st := time(): N := 10^5: evalf( S ); time()-st;

.6931421806   11.150

> st := time(): N := 10^3-1: evalf( S ); time()-st;

.6936474306     .080

> st := time(): N := 10^4-1: evalf( S ); time()-st;

.6931971831    1.020

> st := time(): N := 10^5-1: evalf( S ); time()-st;

.6931521806   11.150

> restart;
> N := 10^3: st := time(): evalf( add( (-1)^(n+1)/n, n=1..N )); time()-st;

.6926474306     .220

> N := 10^4: st := time(): evalf( add( (-1)^(n+1)/n, n=1..N ) ); time()-st;

.6930971831   16.410

> N := 10^4-1: st := time(): evalf( add( (-1)^(n+1)/n, n=1..N ) ); time()-st;

.6931971831   16.540