Lecture 6: PI
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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Transcript excerpt
0:01 So probably one of the most important mathematical terms uh that we have or constants in is pi which we've been deal which we touched on a bit last lecture when we figured out uh basically how to draw a circle by not actually drawing a circle. So um they've got a static version of this now. Cool. Yeah, this is one of my favorite illustrations that we've that that illustrates really what pi is. Pi is the ratio of a circle circumference to its diameter. So the idea here is that if we take a circle that's of unit length, in other words, has a diameter of one, and we unroll it, then the amount it's going to unroll is going to
0:49 be equal to pi, 3.14159. Now pi is this ratio uh or this constant that has a this ratio this rational irrational number it means that it can't be expressed as a fraction of two integers um it means it goes on essentially forever but before we understood that it was an irrational number and before we understood a lot of other things about it we und stood we we were still you using a formula to create the uh for this ratio or trying to calculate this ratio as best we could. Um so we had various ways of approximating uh pi
1:38 in other words figuring because again uh you know when you were maybe in third grade or whatever you pro might have learned pi or fourth grade or I don't know when they teach this kind of stuff but at some point you learned pi and you started out by learning pi is 3.14 which is an it's three digits so that's easy enough to remember right so um is that and it's honestly good enough
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
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.