Friday, March 13, 2015

Cooking Up Pi

Happy Pi Day! One day out of the year we math geeks get to show off some Pi-related trivia we've dug up. Personally I love the artsy, the musical and even the food-related Pi-ponderings out there. But I'm a "Pi-thon guy" and I'm interested in the techy aspects of Pi Day.

Everybody knows Pi is the ratio of the circumference of a circle to its diameter. The Greeks were surprised that a nice perfect 1-unit wide wheel didn't roll out a beautiful whole number in one rotation. It always rolled just over 3 units. 3 and a half? Not even close. 3 and a quarter? Not quite. 3 and an eighth? Pretty close. 3 and a seventh is a good approximation, and in textbooks for centuries 22/7 stood in as the simple version of pi.

2,000 years ago Archimedes used the ratio of the perimeter of a 96-sided polygon to its diameter to get a really accurate approximation: between 3 + 10/71 and 3 + 10/70. Around 3.14185

About 600 years later in China, Zu Chongzhi used a 12,288-sided polygon (!) to improve the approximation to 3.1415929. How good is this approximation for all practical purposes? Well, the orbit of Jupiter is pretty close to a circle with a radius of 483,800,000 miles. So the 3 ten-millionths of error in Zu's value of pi corresponds to about 240 miles in arc of the orbit of Jupiter.

But at this very second a bunch of supercomputers somewhere are cranking out millions and millions more digits of pi. How do we get so precise? A history lesson is in order.

A few years ago some lucky homeschoolers attended my Math Through Technology course and they learned how Issac Newton extended Pascal's Triangle backwards (!) to get a formula for
(a + b)n

when n is negative. It was an infinite series that worked for numbers less than 1:

1/(1 + x) = 1 - x + x2 - x3 + ...

When the methods of Calculus got out, mathematicians found a formula for the derivative of the arctangent of a number:

(d/dx) atan(x) = 1/(1 +x2)

Looks similar! We can just replace x with x2 , integrate the terms one by one and we have an infinite series for arctangent:

(d/dx) atan(x) = 1/(1 +x2)

(d/dx) atan(x) = 1 - x2 + x4 - x6 + ...


If you've ever gotten up to Precalculus or Trigonometry you might wonder why all of a sudden they stop using degrees and start using radians. It's actually supposed to make finding arc lengths easier, but it can be confusing to see all those pi's. For example, instead of 45 degrees, you say "pi/4". The tangent of 45 degrees, I mean pi/4, is 1, so the arctangent of 1 is pi/4. The European mathematicians thought they were so bright in changing their series to:


Just add up a few fractions, multiply by 4 and we clever Europeans have pi to as many places as we want! Two problems: this formula was already well known to the folks in India (perhaps the Jesuits brought it back?) and it converged so slowly that it was almost useless. I illustrate this in my book Hacking Math Class Using Python. You can write a Python program to use a certain number of terms to calculate pi using the series:



Only 2 correct decimal places after 1,000 terms? A better series was needed. In the 1700s John Machin was playing around with tangent formulas. He started with the 45-degree right triangle. The tangent here is 1.




Machin inserted 4 triangles with a tangent ratio of 1/5 and nearly got to pi/4:




It was just a tiny bit off. Some tangent formulas made it easy to calculate just how far off.




Machin's improvement on the pi formula is as follows:


In Hacking Math Class I show you how to adapt the Python code above to return the arctan and then call it from a "machin" function.

def arctan(x,depth):
    x = float(x)
    runningSum = 0
    for i in range(depth):
        term = (-1)**i*(x**(2*i+1)/(2*i+1))
        runningSum += term
    return runningSum

def machin(depth):
    return 4 *( 4*arctan(1./5.,depth)- arctan(1./239.,depth))

Now using only 10 terms of the series (not an unreasonable amount of effort for an 18th century mathematician), we get 10 correct digits of pi!


Of course in the centuries since Machin there have been many more series found to approximate pi. Check them out on Wolfram Mathworld and code them in Python!

Monday, March 2, 2015

Hacking Math Class: The Book

It seems like a century ago I learned from Seymour Papert that everything worth doing in math can be done using a computer. I learned enough Logo to make turtles walk around a screen and make geometrical figures, and I knew I was onto something every math student could use. After that every math class I taught had a computer component, much to the confusion of administrators and even my fellow math department members.

Years later I learned Python so I could help a computer-philic homeschooled kid explore math topics by writing programs. He excelled and his family was very supportive of my methods. I kept all the explorations we did and was inspired to do more exploring on my own.

The outcome of all this activity is Hacking Math Class With Python: Exploring Math Through Computer Programming, a 130-page book available for download on my website. It starts with an introduction to the Python programming language and its excellent turtle module. Those hardworking turtles are made to do everything from simple geometry to graphing polynomials to drawing fractals to transforming figures using matrices. Unique in its approach to math education, the book contains all the code necessary to explore math topics from arithmetic to differential equations.

Derivatives? Integrals? There are explorations in the book to help visualize Calculus problems and to show how easy it is to find numerical solutions. Vectors? They're the key to interactive graphics and animation, so why not have students write programs to make objects fly around the screen?

Every math textbook has a picture of a Mandelbrot Set, but until now none would actually teach the reader to draw one. That's because it would take you a year to iterate all those complex numbers without a computer. Hacking Math Class leads you step-by-step through the program.

Python is an excellent first language to learn, and there are plenty of people making a fine living programming in Python. The book presents the basic tools of programming and shows how powerful those tools can be. The reader is expected to install Python and code along. It's not "easy," but it rewards a bit of effort! Enjoy.