A14 · Lesson 9 · Lab: sequences and series in code
Thresholds, tail brackets, accelerated sums, pairings and root tests
Lessons 1 to 8 were about sequences and series in the abstract: ε and N, bounds on tails, rearrangements, countable sets, limsup. In this lab you'll make each idea concrete in code. You'll find ε–N thresholds by search and compare them with the formulas from proofs, bracket with integrals, accelerate a slowly converging alternating series, check that the Cantor pairing really is a bijection, and watch the root test's k-th roots settle where the ratio test can't decide.
- The stems are for (k even) and (k odd). They alternate between exactly ½ and exactly ⅓.
- The limsup is ½ < 1, so the root test says the series converges. The ratios , by contrast, alternate between values going to 0 and to ∞: the ratio test is silent.
- In the lab you'll compute both sequences and confirm what each test sees.
Predict
A proof shows . For ε = 0.0003, what N does a computer search find?
16 665: the first n with , since . Search and formula agree exactly here because the formula is exact; when a proof uses inequalities, its N is larger than the true threshold, and a search shows by how much.
Name it
Searching for N
To find the least N with for all n ≥ N, record the last n that fails, and stop once you've gone far past it (for monotone tails, a few times further is plenty). A search can't prove "for all n", but it checks a proof's N and exposes a wrong one.
Brackets and acceleration
For a decreasing positive f, . The midpoint of the bracket is usually far more accurate than . For alternating series, repeatedly average neighboring partial sums (lesson 7's example, done systematically).
Pairing in code
math.isqrt gives exact integer square roots, so the inverse pairing , , can be computed without floating-point error.
Worked example
Bracket using ten terms.
Show the solution
- Partial sum.
- Bound the tail with integrals.
- Bracket. Adding these to gives the bracket [1.201664, 1.202532], whose midpoint is 1.202098.
- Compare with the true value. The true value is 1.2020569, so the midpoint's error is , against 's 0.0045.
- Going further. The same idea with more terms or with the Euler–Maclaurin correction gives many digits from a short sum.
Answer lies in [1.201664, 1.202532], and the midpoint 1.202098 is off by about .
Worked example
Accelerate the first 20 partial sums of .
Show the solution
- The raw partial sum. which is 0.024 below .
- Average neighbors three times. Using the last few partial sums, this leaves an error of , more than 8000 times smaller than before, for no extra terms.
- Why it works. The partial sums alternate around the limit with slowly shrinking swings; each averaging cancels most of the current swing.
Answer three rounds of averaging cut the error from 0.024 to .
Check
For , what is the least N with for all n ≥ N?
Check
Inverting the Cantor pairing at k = 100, which diagonal w = m + n is it on?
Check
Write first_N(a, L, eps) that searches for the least N with for all n ≥ N (track the last failing n; stop when n exceeds ten times that plus 100). Set N1 for , L = 3, ε = 0.0003, and N2 for , L = 0, ε = 0.001.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set lo and hi to the integral-test bracket for from , and mid to its midpoint.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Set S to the first 20 partial sums of (a NumPy array), then average neighbors three times (A = (A[:-1] + A[1:]) / 2) and set err_raw and err_acc to the errors of S[-1] and the last averaged value against ln 2.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write pair(m, n) and unpair(k) for the Cantor pairing (use math.isqrt). Set ok to whether pair(*unpair(k)) == k for all k below 10 000, and distinct to the number of different values of pair(m, n) for 0 ≤ m, n < 100.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
For (k even) and (k odd), set roots to for k = 1 to 200, and ratios to for k = 1 to 30. Set limsup_est to the largest root among k ≥ 100, and ratio_max to the largest ratio.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why can a computer search check a proof's N but never replace the proof?
A search can only examine finitely many n, while the definition of a limit makes a claim about every n beyond N. If the sequence behaved well up to a billion and then misbehaved, no feasible search would notice. What makes a search trustworthy is extra knowledge, usually from a proof, that the tail is monotone or otherwise controlled, so that once the terms are inside the ε band and moving the right way they stay there. Used that way, search is a strong check: a proof's N that's smaller than the searched threshold is wrong, and one much larger shows where the proof's inequalities were loose.
Practice
Keep practicing
4 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.