Post · 9 min read· pp. 90–94

Cook’s Class Contains Pi

Cook’s class and the BBP algorithm for Pi

images8

Steve Cook won the Turing Award for his famous paper on SAT that started the P=NP problem. He has done many other wonderful things in many areas of computational complexity, I especially love his work on proof systms. I am proud that my Cook number is 1{1}.

Cook is also famous for having a complexity class named after him: SC{\mathsf {SC}}. Steve’s Class is the set of languages accepted in polynomial time and poly-logarithmic space. Think of SC{\mathsf {SC}} as the set of languages that can be recognized efficiently, but with very small space. Today as problems studied begin to contain vast amounts of data, as the size of objects that we wish to understand grows, Steve’s Class has become even more important.

When I was an assistant professor at Yale University, David Dobkin and I would often argue about what is better having X{X} named after you or having Y{Y} named after you. For example, would you like a constant (Euler) or an inequality (Jensen) or an equation (Schrödinger) named for you, and so on? We finally decided the best was a “universe”. If you do not know the universe we were thinking about it is called Herbrand’s Universe named after the logician Jacques Herbrand. Having a complexity class named after you, like Steve has, is still pretty cool.

Computing π{\pi}

Computing π{\pi} fast involves two ideas. First, a fast converging summation and second the use of efficient algorithms for high precision arithmetic operations. For example, the methods use series like the following, due to S. Ramanujan,

1π=229801∑k=0∞(4k)!(1103+26390k)(k!)43964k.\displaystyle \frac{1}{\pi} = \frac{2\sqrt 2}{9801} \sum_{k=0}^{\infty} \frac{(4k)!(1103+26390k)}{(k!)^{4}396^{4k}}.

These and other remarkable formulas are the basis of modern computations of π{\pi}: you might ask how does one find such formulas? I have no idea, that is definitely not my area of expertise.

The problem with these fast algorithms is not speed, they run in almost linear in the number of bits that you want of π{\pi}. The problem is the amount of space that they require. Prior to 1995, algorithms used linear space. So if you wanted to compute the first trillion bits of π{\pi}, the computation cost was reasonable; however, the space cost was huge.

In 1995, David Bailey, Peter Borwein, Simon Plouffe discovered a beautiful algorithm that changed all this. Their algorithm–called BBP–computes the nth{n^{th}} bit of the number π=3.14…{\pi=3.14\dots} without first computing any of the previous bits. This went against the conventional wisdom . Before their work most thought that you needed to compute the earlier bits of π{\pi} to get the later bits. They shattered the conventional wisdom. If you have read any other parts of my blog, then you know this is one of my main ideas. We all guess wrong sometimes. What about P=NP?

From the viewpoint of complexity theory they showed that the problem of computing the nth{n^{th}} bit of π{\pi} is in SC{\mathsf{SC}}. The BBP algorithm has an interesting history and some controversy surrounding it. See this for a view from Plouffe about it . I only point this out to show you that the story behind results can be quite interesting. Indeed.

The BBP Algorithm

There are two things about the BBP algorithm that I find intriguing and want to share with you:

  • The algorithm uses the exponential trick that we sometimes take for granted as computer scientists;
  • The algorithm has a bug. It is not really an algorithm. Well it is almost an algorithm.

The first point is that the algorithm uses the following:

Lemma 1 The value of aM{a^{M}} modulo N{N} can be computed in time polynomial in the number of bits in a,M,N{a,M,N}.

This well known fact–to computer scientists–is of central importance to many areas. It is used in cryptography, in number theory, and elsewhere. It is interesting that they felt the need to explain the exponential trick in some detail . For example, they not only give the detailed code of this trick, they give the following example:

317=3((((32)2)2)2)\displaystyle 3^{17} = 3((((3^{2})^{2})^{2})^{2})

“thus producing the result in only 5 multiplications, instead of the usual 16”. I would like to talk more about this important idea in a later post.

Let’s now turn to finally explaining how their algorithm works. Rather than compute the bits of π{\pi} it is easier to explain the algorithm for the simpler case of the number ln⁡2{\ln 2}. We first note that

ln⁡2=∑k=1∞1k2k\displaystyle \ln 2 = \sum_{k=1}^{\infty} \frac{1}{k2^{k}}

is a well known summation–to those who know such sums. Their idea is this: to compute the nth{n^{th}} bit of a number α{\alpha} one needs only to compute the fractional part of 2nα{2^{n}\alpha}. Denote the fractional part of x{x} by {x}{\{ x \}}. Suppose that β={2nα}{\beta = \{ 2^{n}\alpha \} }. Now let

α=m+∑k=0∞ak2k\displaystyle \alpha = m+ \sum_{k=0}^{\infty} \frac{a_{k}}{2^{k}}

where m{m} in an integer and ak∈{0,1}{a_{k} \in \{0,1\}}. Then, β{\beta} is equal to

{an+12+an+222+… }\displaystyle \{ \frac{a_{n+1}}{2} + \frac{a_{n+2}}{2^{2}} + \dots \}

since all the other terms are integers: we use that {x+1}={x}{ \{ x + 1 \} = \{ x \}}. Suppose that β≤0.5−ϵ{\beta \le 0.5 - \epsilon }, then an+1=0{a_{n+1} = 0}; if β≥0.5+ϵ{\beta \ge 0.5 + \epsilon }, then an+1=1{a_{n+1} = 1}. Here ϵ\epsilon will be selected later on.

Thus, to find the nth{n^{th}} bit of ln⁡2{\ln 2} we need only compute the factional part of 2nln⁡2{2^{n}\ln 2}:

{2nln⁡2}=∑k=1n{2n−kk}+∑k=n+1∞{2n−kk}\displaystyle \{ 2^{n} \ln 2 \} = \sum_{k=1}^{n} \{ \frac{2^{n-k}}{k} \} + \sum_{k=n+1}^{\infty} \{ \frac{2^{n-k}}{k} \}

Call the first sum A{A} and the second B{B}. They key insight is that each of these sums can be computed to high precision in polynomial time and small space.

The sum A{A} is equal to,

∑k=1nakk\displaystyle \sum_{k=1}^{n} \frac{a_{k}}{k}

for ak≡2n−k mod k{a_{k} \equiv 2^{n-k} \bmod k}. Note, this uses the fact that

{xk}={x mod kk}.\displaystyle \{ \frac{x}{k} \} = \{ \frac{x \bmod k}{k} \}.

Clearly, this can be computed in polynomial time to p{p} bits of precision in space at most p{p}. Note, this is where they need to use the exponential trick: it is important that the values of ak{a_{k}} are small and easy to compute. The sum B{B} is even easier to compute. It is almost a geometric series so the terms decay off fast: thus, to compute it to p{p} bits of precision is easy in polynomial time and in p{p} space.

They then have the factional part of 2nln⁡2{2^{n} \ln 2}: if it is larger than 0.5+ϵ{0.5+\epsilon} they know that the nth{n^{th}} bit is 1{1}; if it is smaller than 0.5−ϵ{0.5-\epsilon} they know that the nth{n^{th}} bit is 0{0}. A very pretty idea.

A “small detail” what is the value of p and ϵ{p \text{ and } \epsilon}: recall p{p} is the precision of the calculation. In practice they have found that p=128{p=128} is quite enough, with ϵ\epsilon set to about 2−p.2^{-p}. From a complexity point of view we need to argue that making p{p} poly-logarithmic in n{n} suffices. They do not supply this argument. We will get back to this issue shortly, it is the reason the algorithm may not work.

What About π{\pi}

The BBP method is the same for π{\pi}, they replace the summation for ln⁡2{\ln 2} by the following summation for π{\pi}:

π=∑k=0∞(48k+1−28k+4−18k+5−18k+6).\displaystyle \pi = \sum_{k=0}^{\infty} \big( \frac{4}{8k+1} - \frac{2}{8k+4} - \frac{1}{8k+5} - \frac{1}{8k+6} \big).

Part of the exciting story of their algorithm is how they found this summation. It was not known to anyone, they had to go out and discover the summation. How they did this is a long story that I will talk about another day. They used guessing and a clever search method based on lattice algorithms.

Note, the structure of their summation is of the same type as that for ln⁡2{\ln 2}. It particular, it is of the form:

∑k=0∞r(k)s(k)bk\displaystyle \sum_{k=0}^{\infty} \frac{r(k)}{s(k)b^{k}}

where r(k){r(k)} and s(k){s(k)} are polynomials and b>1{b>1} is an integer. Such sums all can be used to get the nth{n^{th}} b{b} base digit of the value of the sum.

A Bug?

The beautiful algorithm of BBP does not actually work. The problem is simple. In a nutshell: what if the fractional part they get lies between 0.5−ϵ0.5 - \epsilon and 0.5+ϵ0.5 +\epsilon. Is the bit 00 or is it 11? They cannot tell. This is the problem. Of course they could increase the precision and lower ϵ\epsilon, but how far do they have to go? In practice, as I said earlier, this issue seems to be a non-issue. However, to prove that their algorithm is in SC\mathsf{SC} requires a bound. They do not have one.

The problem has to do with long runs of 11‘s in the number they are computing the bits of. For example, suppose that ln⁡2{\ln 2} had a huge number of 1{1}‘s in a row somewhere in its binary expansion. Then, how could we tell what is the correct bit? The fractional part must be determined whether is larger or smaller than 0.5{0.5}: what happens if it is extremely close to 0.5{0.5}? The answer is this is the bug in the algorithm. The problem is this:

0.0111111111111111111…\displaystyle 0.0111111111111111111\dots

vs

0.1000000000000000…\displaystyle 0.1000000000000000\dots

how do you tell them apart if the run of 1{1} is very long? The following remakable quote is in Wikipedia:

”There is a vanishingly small possibility that a particular computation targets a section that is subject to small round-off errors being carried for many digits (e.g. in base 10, a long string of zeros or string of 9’s within the constant) and that the error will propagate to the most significant digit, but being near this situation is obvious in the final value produced.”

Excuse me, this is nonsense. The statement “vanishingly small possibility” is a statement of probability theory. There is nothing random here at all. I agree that it seems that the problem may never arise in practice, but that is different. The last point of the quote is correct. If the value of the fractional part is very close to 0.5{0.5} we will know that we are in trouble. But that means that the BBP is not really an algorithm.

Open Problems

The obvious open problem is fix BBP and make it a real algorithm. I have a strong belief that the following should hold:

“Theorem” 2 Computing the nth{n^{th}} bit of π{\pi} is in SC{\mathsf{SC}}.

The plan of a proof is to use their basic BBP method. However, when one detects a long run of 1{1}‘s, then use the fact that π{\pi} satisfies a transcendence measure. . This means that π{\pi} satisfies:

∣π−rs∣>1sc\displaystyle \mid \pi - \frac{r}{s} \mid > \frac{1}{s^{c}}

for integers r,s{r,s} large enough and some c>0{c>0}. I believe that this can be used to show that the above theorem will work. Roughly, a long run of 1{1}‘s would violate the above inequality for π.\pi.

I may write a post working out the details of this argument, or you can take a try and work it out.