Alan Turing and a trig puzzle

Here’s a puzzle based on a passing remark in Andrew Hodges’ biography of Alan Turing.

Hodges says that in 1927,  Alan Turing, then 15 years old, discovered the Taylor series for arctangent “starting from the trigonometrical formula for tan(x/2).”

\tan^{-1}(x) = x - \frac{x^3}{3} + \frac{x^5}{5} - \frac{x^7}{7} + \cdots

Deriving the series could be a homework exercise in a calculus class, but Turing discovered the series without using calculus.

Hodge doesn’t give any further information about what Turing’s derivation. What might it have been? How could you derive the series expansion for arctangent from a trig identity?

More puzzles

Calculating pi with AGM and mpmath

This post gives an algorithm based on the arithmetic-geometric mean that rapidly converges to pi. I’ll use it to illustrate multiple precision arithmetic using Python’s mpmath module.

Given two non-negative numbers a and b, their arithmetic mean is (a + b)/2 and their geometric mean is √(ab). Suppose you start with two non-negative numbers and take their arithmetic mean and geometric mean. Then take the arithmetic and geometric mean of the results. Keep doing this repeatedly, and the result converges to what is called the arithmetic-geometric mean (AGM).

There is a formula for calculating pi based on the AGM that goes back to Gauss.

\pi = \frac{ 4M^2\left(1, \frac{1}{\sqrt{2}}\right)}{1 - \sum_{n=1}^\infty 2^{n+1} (a_n^2 - b_n^2)}

Here M(a, b) is the AGM of a and b, and an and bn are the values after n steps of the iteration.

The process defining the AGM converges quickly, and so the formula is practical for computing pi. I found it in a paper from 1988, and at that time the formula had been used to compute pi to over 29 million decimal places. In the code below, we compute pi to 997 decimal places in only 10 iterations.

from mpmath import mp, sqrt, mpf, pi, log10, fabs

# carry out calculations to 1000 decimal places
decimal_places = 1000
mp.dps = decimal_places
epsilon = 1/mpf(10**decimal_places)

# set a = 1 and b = 1/sqrt(2) as multi-precision numbers
a = mpf(1)
b = 1/sqrt(mpf(2))

diff = a - b
series = mpf(0)

n = 0
while diff > epsilon:
    n += 1
    arith = (a + b)/2
    geom = sqrt(a*b)
    a, b = arith, geom
    series += 2**(n+1) * (a*a - b*b)
    diff = a - b

# a and b have converged to the AGM
my_pi = 4*a*a/(1 - series)

error = fabs(pi - my_pi)
decimal_places = int(-log10(error))

print "Number of steps used: %d" % n
print "Number of correct decimal places: %d" % decimal_places

The code can be used to calculate pi out further. The number of correct decimal places roughly doubles with each iteration. For example, computing pi to 10,000 places takes only 3 more iterations.

More posts on computing pi

Post minimalism

I’m suspicious of terms that start with “post-“. Often these terms are pretentious and inaccurate. When someone says, for example, that we’ve moved into a post-X era, they often mean that they didn’t like X and would like to think it’s gone.

Despite my misgivings about post-this and post-that, I smiled the other day when I heard some music described as “post-minimalist chamber music.” What I like about it is the idea of pursuing things to their most basic elements, then backing off a bit. Minimalist music drives me crazy after a while, but minimalist-inspired music with a little more variety can be pleasant to listen to.

In general, minimalism tends to be harsh. For example, minimalist architecture can be stark and cold. But not-quite-so-minimal architecture, spare but humane, can be beautiful. You could say the same of minimalism as a lifestyle. Taken to extremes, it’s ugly. But a more moderate and graceful form of minimalism is beautiful.

More posts on moderating minimalism

Humbled by a debugging book

I started developing software for Windows in 1995, but I hardly know anything about Windows. I feel like my understanding of Windows peaked around the turn of the millennium and has declined since. I was reminded of the depth of my ignorance by a new book I received recently, Inside Windows Debugging.

This book goes way beyond stepping through code with the Visual Studio debugger. It dives into the architecture of Windows and the details of how different kinds of debugging work: user-mode, kernel-mode, native, managed, remote, etc. It has a little to say about Visual Studio, but focuses on more advanced tools such as WinDbg.

Obviously Inside Windows Debugging is a book about debugging. But it’s also a good book for someone wanting to know more about Windows internals.

When a good author writes a bad book

The other day I read a terribly bland book by an author I’ve previously enjoyed. (I’d rather not name the book or the author.) The book was remarkably unremarkable.

It reminded me that even the best strike out now and then. You have to evaluate someone by their best work, not their worst. If someone produces one masterpiece and a dozen clunkers, then they’ve produced a masterpiece. And that puts them ahead of people who crank out nothing but inoffensive mediocrities.

I also thought about how the author is likely to make a lot of money off his terrible book. That’s oddly encouraging. Even when you put out a clunker, not everyone will think it’s a clunker. It’s not necessary to do great work in order to make money, though doing great work is more satisfying.