39. Series in practice: computing e, π, and Euler's formula
Everything in this module has been building toward a genuinely practical payoff: Taylor series are how transcendental constants and functions actually get computed, to as many digits as anyone could want. This lesson puts the machinery to work — computing e and \pi directly from series, comparing a slow method against a fast one, and deriving one of mathematics' most quoted identities as a direct consequence of §8.1's three standard Maclaurin series.
Computing e
Substitute x=1 into the Maclaurin series for e^x from §8.1:
e=e^1=\sum_{n=0}^\infty\frac1{n!}=1+1+\frac12+\frac16+\frac1{24}+\cdots
§8.2's remainder bound explains why this converges so fast: with M=e<3 (a bound on e^c for c\in[0,1]) and x=1,
|R_n(1)|\le\frac3{(n+1)!}
— and (n+1)! overwhelms the fixed numerator almost immediately. Just 10 terms (through \frac1{9!}) already match e to 6 decimal places: the error is 3.03\times10^{-7}, comfortably inside the bound's \frac3{10!}=8.3\times10^{-7}.
Computing \pi: two methods, radically different speeds
The Maclaurin series for \arctan x (obtainable by integrating the geometric series \frac1{1+x^2}=1-x^2+x^4-\cdots term by term, §4.5's technique run in reverse) is
\arctan x=x-\frac{x^3}3+\frac{x^5}5-\frac{x^7}7+\cdots,\qquad|x|\le1
The Leibniz formula plugs in x=1 (using \arctan1=\frac\pi4):
\frac\pi4=1-\frac13+\frac15-\frac17+\cdots
This converges — it's an alternating series with terms \to0 (§7.8) — but agonizingly slowly, since x=1 sits at the very edge of the interval of convergence, where §8.2's remainder bound is at its weakest. Ten thousand terms barely nails down four correct decimal digits.
Machin's formula (1706) uses the same \arctan series but evaluated at much smaller, faster-converging arguments, combined via the angle-addition identity for tangent:
\frac\pi4=4\arctan\frac15-\arctan\frac1{239}
Since \frac15 and especially \frac1{239} are far from the boundary x=1, each series converges dramatically faster — the same (n+1)!-style argument from §8.2, but now also helped by x^{2n+1} shrinking fast when x is small. Just ten terms of each arctan series reproduce \pi to 12 correct decimal digits — a speedup of several orders of magnitude over Leibniz, using the exact same underlying series, just evaluated somewhere better-behaved.
Euler's formula
Substitute x=i\theta (where i=\sqrt{-1}) into the Maclaurin series for e^x from §8.1, and use i^2=-1, i^3=-i, i^4=1, cycling every four powers:
e^{i\theta}=\sum_{n=0}^\infty\frac{(i\theta)^n}{n!}=1+i\theta-\frac{\theta^2}2-\frac{i\theta^3}6+\frac{\theta^4}{24}+\frac{i\theta^5}{120}-\cdots
Separate the real terms (even powers of \theta, no leftover i) from the imaginary terms (odd powers, one leftover factor of i):
\text{real part: }1-\frac{\theta^2}2+\frac{\theta^4}{24}-\cdots=\cos\theta\qquad\text{(§8.1's series for }\cos\theta\text{, exactly)}
\text{imaginary part: }\theta-\frac{\theta^3}6+\frac{\theta^5}{120}-\cdots=\sin\theta\qquad\text{(§8.1's series for }\sin\theta\text{, exactly)}
\boxed{e^{i\theta}=\cos\theta+i\sin\theta}
This isn't a coincidence engineered by the choice of \theta — the real and imaginary parts of e^{i\theta}'s series are, term for term, \cos\theta's and \sin\theta's own series. The three most important Maclaurin series in this course turn out to be one single series, split by where the factors of i land.
Setting \theta=\pi (using \cos\pi=-1, \sin\pi=0):
e^{i\pi}=\cos\pi+i\sin\pi=-1+0=-1
\boxed{e^{i\pi}+1=0}
Euler's identity — five of mathematics' most fundamental constants (e, i, \pi, 1, 0), three operations (exponentiation, addition, equality), connected in one equation, and it drops directly out of substituting an imaginary number into a Taylor series built in §8.1 for an entirely different purpose.
Doing it in Python
Computing e from its series and watching the error shrink:
from fractions import Fraction
from math import e, factorial
for n in (5, 10, 15):
approx = sum(Fraction(1, factorial(k)) for k in range(n))
print(f"n={n:>2} terms: {float(approx):.10f} error = {abs(float(approx)-e):.2e}")
print(f"\ne = {e:.10f}")
Leibniz's slow \pi versus Machin's fast \pi, same underlying \arctan series, radically different convergence:
import math
def arctan_series(x, n_terms):
return sum((-1)**k * x**(2*k+1) / (2*k+1) for k in range(n_terms))
leibniz_pi = 4 * arctan_series(1, 10000)
machin_pi = 4 * (4*arctan_series(1/5, 10) - arctan_series(1/239, 10))
print(f"Leibniz, 10000 terms: {leibniz_pi:.10f}")
print(f"Machin, 10 terms: {machin_pi:.10f}")
print(f"true pi: {math.pi:.10f}")
Confirming Euler's formula symbolically, and Euler's identity as its special case:
import sympy as sp
theta, x = sp.symbols('theta x')
lhs = sp.exp(sp.I * theta)
rhs = sp.cos(theta) + sp.I * sp.sin(theta)
print(f"e^(i*theta) - (cos(theta) + i*sin(theta)) simplifies to: {sp.simplify(lhs - rhs)}")
euler_identity = sp.exp(sp.I * sp.pi) + 1
print(f"e^(i*pi) + 1 = {sp.simplify(euler_identity)}")
Worked example
Use 6 terms of the Maclaurin series for e^x to approximate e^{0.5}, and bound the error using §8.2's remainder formula.
T_5(0.5)=\sum_{n=0}^5\frac{0.5^n}{n!}=1+0.5+0.125+0.0208\overline3+0.0026041\overline6+0.00026041\overline6
\approx1.64869792
Error bound (§8.2), using M=\sqrt e<2 as a bound on e^c for c\in[0,0.5], with the next term (n+1=6) supplying the bound:
|R_5(0.5)|\le\frac2{6!}(0.5)^6=\frac2{720}\cdot\frac1{64}=\frac2{46080}\approx0.0000434
Sanity check. The true value e^{0.5}\approx1.64872127, giving an actual error of about 1.64872127-1.64869792\approx0.0000233 — comfortably under the bound of 0.0000434, as guaranteed. ✓ The bound isn't tight (the true error is roughly half of it), which is expected: §8.2's bound uses the worst-case M across the whole interval, while the actual c that makes the Lagrange formula exact could sit anywhere in [0,0.5], typically giving a smaller real error than the guaranteed ceiling.
Your turn
1. Using 4 terms (n=0 through 3) of the e^x Maclaurin series, approximate e^{0.1}, and compare to the true value.
2. Explain, without computing anything new, why Machin's formula converges faster than Leibniz's formula, referencing §8.2's remainder bound.
3. Using Euler's formula, find e^{i\pi/2} and interpret the result.
Solutions
1. T_3(0.1)=1+0.1+\frac{0.01}2+\frac{0.001}6=1+0.1+0.005+0.0001\overline6\approx1.1051\overline6. True value: e^{0.1}\approx1.10517. Error \approx0.0000042 — extremely small, since x=0.1 is close to the center and §8.2's bound shrinks fast for small |x-a|.
2. §8.2's remainder bound for the \arctan series scales with |x|^{2n+1}. Leibniz uses x=1, so this factor is 1^{2n+1}=1 regardless of n — it never helps shrink the bound, leaving only the (much slower) polynomial denominator to do the work. Machin uses x=\frac15 and x=\frac1{239}, where |x|^{2n+1} shrinks geometrically fast in addition to whatever the denominator contributes — the same "x close to the center converges faster" principle that made problem 1's e^{0.1} approximation so much more accurate than e^{0.5}'s.
3. e^{i\pi/2}=\cos\frac\pi2+i\sin\frac\pi2=0+i(1)=\boxed i. Euler's formula maps real angles onto the unit circle in the complex plane; \theta=\frac\pi2 lands exactly on the imaginary axis, a quarter-turn from e^{i\cdot0}=1 — consistent with i behaving like a 90° rotation, the same geometric fact that makes i^2=-1 (two quarter-turns) sensible in the first place.
Check yourself in code
Compute e using 10 terms of its Maclaurin series (as a fraction), and
compare it to Python's math.e.
Print exactly this:
10-term approximation = 2.7182815255731922
math.e = 2.718281828459045
from fractions import Fraction
from math import e, factorial
approx = sum(Fraction(1, factorial(k)) for k in range(10))
print("10-term approximation = ...")
print("math.e = ...")
from fractions import Fraction
from math import e, factorial
approx = sum(Fraction(1, factorial(k)) for k in range(10))
print(f"10-term approximation = {float(approx)}")
print(f"math.e = {e}")
Taylor series aren't just a theoretical device — plugging x=1 into e^x's series computes e directly, and the \arctan series computes \pi, though where you evaluate it matters enormously: Machin's formula reaches double-digit precision in the same number of terms Leibniz's needs thousands for, purely by staying closer to the series' center where §8.2's remainder bound is strongest. Substituting an imaginary argument into the e^x series, and watching it split exactly into the \cos and \sin series from §8.1, produces Euler's formula — and Euler's identity falls out as the single case \theta=\pi.
Next: the technical question quietly assumed throughout this whole module — that differentiating and evaluating a series term by term is always legitimate — examined properly.