Lecture 6: PI
Watch on YouTube →
Overview
Professor Rosen uses the Madhava–Leibniz series, π/4 = 1 − 1/3 + 1/5 − 1/7 + ···, to teach Python loops, ranges, accumulators, and alternating signs. The lecture then connects the series’ slow convergence and floating-point limits to binary representation, addition, and two’s-complement subtraction.
Key takeaways
- The Madhava–Leibniz series approximates π by summing alternating odd reciprocals and multiplying the result by 4: π ≈ 4 × Σ((-1)^k / (2k + 1)).
- Python’s `range(start, stop, step)` excludes `stop`; `range(1, 10, 2)` is therefore a direct way to generate the odd denominators 1, 3, 5, 7, and 9.
- A running-sum accumulator should start at the operation’s identity—0 for addition—and be updated inside the loop, with final output placed after the loop.
- The alternating-sign Leibniz series converges slowly, so a large increase in term count may produce only a small improvement in the number of accurate digits of π.
- Binary represents values using powers of 2, and two’s complement represents negative integers by bit inversion followed by adding 1, allowing subtraction to use addition hardware.
- Repeated printing can significantly slow a program; print intermediate values for debugging or explanation, then remove or disable those prints for larger computations.
Chapters
0:00
Pi as the Circumference-to-Diameter Ratio
- Pi is the ratio of a circle’s circumference to its diameter.
- A circle with diameter 1 unrolls to a length of π, approximately 3.14159.
- The familiar 3.14 approximation is sufficient for most everyday calculations.
2:10
How Computing Accelerated Pi-Digit Calculations
- A logarithmic graph shows the number of calculated digits rising from a few early digits to millions and beyond with computing.
- By the 1400s, calculations had reached about 10 digits—already enough for most practical applications.
- The graph’s vertical axis advances by powers of 10 to make the dramatic growth visible.
4:00
Madhava and the History of Pi Approximations
- Ancient approximations included 22/7 and the Babylonian value 25/8; Chinese and Indian mathematicians also developed accurate methods.
- Madhava of Sangamagrama discovered the series later associated with Leibniz, illustrating how mathematical discoveries can be independently found or poorly preserved.
- Professor Rosen uses Josephus as an example of how historical accounts depend on people who record events, not simply on who wins conflicts.
7:20
Reading the Madhava–Leibniz Series for Pi
- The series states that π/4 equals 1 − 1/3 + 1/5 − 1/7 + 1/9 − ···.
- The denominators increase by 2, while the signs alternate between positive and negative.
- Multiplying a partial sum by 4 gives an approximation of π; adding more terms moves the estimate closer to π/4.
11:30
Breaking the Pi Program into Smaller Tasks
- Rosen proposes building the calculation incrementally: first sum values, then generate odd numbers, convert terms to fractions, and alternate signs.
- He presents mathematical notation as a precise description of a pattern, even when the symbols initially look unfamiliar.
- The series gives an intro-to-computer-science programming goal: approximate a known constant by repeating a clear operation.
13:20
Python for Loops and the Three Forms of range()
- A `for` loop repeats its body once for each value produced by a sequence such as `range()`.
- `range(10)` produces integers 0 through 9; the stop value is excluded, so the number of values is 10.
- `range(5, 10)` produces 5 through 9, while `range(5, 20, 2)` advances by a step of 2.
- A negative step can count downward, as in `range(100, 0, -1)`, which stops before 0.
21:30
Summing a Range with an Accumulator
- An accumulator stores a running result; for addition, initialize `total` to 0, the additive identity.
- Inside the loop, update it with `total = total + x`; after the loop, print the final total.
- For `range(10)`, the loop adds 0 through 9, and moving the final print outside the loop avoids printing every intermediate result.
- Printing repeatedly is relatively expensive: Rosen contrasts a visibly slow printed run with a fast calculation that produces no loop output.
30:50
Using range() to Generate Odd Denominators
- The series needs odd denominators, so use a range beginning at 1 with a step of 2.
- For example, `range(1, 10, 2)` generates 1, 3, 5, 7, and 9.
- The running sum can be adapted to add each generated odd value, building on the earlier accumulator pattern.
34:00
Turning Odd Numbers into Fractional Terms
- Change each contribution from `x` to `1 / x` to create the terms 1, 1/3, 1/5, and so on.
- Parentheses make the intended operation explicit when updating the total.
- The series without alternating signs, 1 + 1/3 + 1/5 + ···, diverges, so merely generating odd reciprocals does not approximate π/4.
38:10
Alternating Signs with a Sign Variable
- Initialize a `sign` variable and multiply each reciprocal by it before adding the term to the total.
- Flip the sign each iteration by multiplying it by −1, producing the sequence +1, −1, +1, −1.
- The resulting terms match 1 − 1/3 + 1/5 − 1/7 + ···, so the partial sum approximates π/4.
44:00
Testing Convergence and Understanding Float Precision
- The lecture multiplies the accumulated π/4 estimate by 4 and compares it with Python’s `math.pi`.
- The alternating series converges slowly: even hundreds of thousands of terms yield only a limited number of reliable digits.
- A displayed result can contain a wrong digit followed by apparently correct digits because each term is added or subtracted and then the sum is multiplied by 4.
- Floating-point values have finite precision; conversions between decimal notation and binary can introduce small numerical discrepancies.
49:00
Decimal Place Value and Binary Representation
- In base 10, 354 means 3 × 10² + 5 × 10¹ + 4 × 10⁰.
- Binary uses only 0 and 1, with place values that are powers of 2; for example, 1001₂ represents 9.
- Digital circuits use binary because distinguishing two voltage states is simpler than reliably distinguishing among ten states.
- Numbers move between human-readable decimal and machine-oriented binary representations during computation and display.
56:10
Binary Addition, Two’s Complement, and Low-Level Computing
- Binary addition uses ordinary carry logic: 1 + 1 produces 10₂, meaning write 0 and carry 1.
- Computers can perform subtraction by adding a negative number; one’s complement flips every bit but creates a redundant negative-zero representation.
- Two’s complement forms a negative value by flipping the bits and adding 1; overflow beyond a fixed-width value, such as five bits, is discarded.
- Rosen connects these ideas to low-level coursework in circuitry, assembly, and C, and explains that multiplication can be viewed as repeated addition and division as repeated subtraction.
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Professor Rosen.