0

I would like a method for computing $\pi$ for which I have an explicit bound on the error, and that has the property that if I do the calculation up to a certain accuracy, but then need an arbitrarily finer accuracy later, I can pick up the calculation process where I left off rather than having to start over from scratch. In light of the second requirement, I don't want to have to deal with, say, square roots, because whatever accuracy I calculate the roots to in order to reach some desired accuracy for $\pi$, there is some finer accuracy for which the root approximations already used would have needed to be more accurate. I believe I'll want to stick to algorithms for which the operations required are limited to arithmetic on rationals in order to avoid such issues.

The Maclaurin series for $4\arctan(1)$ is an example of an algorithm that obeys these properties: $$\pi=4\arctan(1)=4\sum_{n=0}^\infty\frac{(-1)^{n-1}}{2n+1}$$ Provided I save the value of $n$ for the last term I added, it's easy to resume the calculation, and I always know that the magnitude of the error is less than that of the next term.

However, I know that this algorithm converges very slowly. What contenders are there for faster algorithms that obey the desired constraints? It sounds like an unbounded spigot algorithm would work, but if I'm not mistaken, those are subject to more constraints than I care about, so maybe it's possible to do better.

Carl Christian
  • 12,583
  • 1
  • 14
  • 37
  • 1
    Would any of these match what you're looking for? https://math.stackexchange.com/questions/14113/series-that-converge-to-pi-quickly – Alex D May 01 '18 at 06:23
  • @AlexD Though I'd ultimately like a rational representation, I suppose it would be easy to covert the result of the BBP formula to one, and the magnitude of the last place value calculated would give the error bound. If an error bound is known for the second answer, though, that would be perfect. – Alex Kindel May 01 '18 at 06:35
  • @AlexD Oh, now that I look closer, I guess the BBP formula as stated already returns a rational number, so as long as I understand correctly that the kth partial sum is within 1/16^k of the correct value, it's exactly what I need. – Alex Kindel May 01 '18 at 07:02
  • I changed the title of your question to more closely reflect what you are looking for. The use of the word "unbounded" sent the wrong message. – Carl Christian May 01 '18 at 14:24
  • You have received a two answers which precisely fit your requirements. One algorithm converges linearly, the other triples the number of correct digits of $\pi$ with every iteration. Your next step is vote/accept or explain why these answers are not suitable for you application. – Carl Christian May 08 '18 at 10:45
  • @CarlChristian I've settled on one of the ones AlexD linked to in their comment. What is the appropriate way to handle voting in this case? – Alex Kindel May 08 '18 at 12:51
  • Write your own solution where you include the formula and the explicit error bound which you wanted in the first place. It is not included in the answer referenced by AlexD. After a grace period, you will be able to accept your own answer, see https://math.stackexchange.com/help/self-answer In general, you are encouraged to vote answers up or down, see https://math.stackexchange.com/help/why-vote – Carl Christian May 08 '18 at 13:45

4 Answers4

1

Below follows an analysis that will allow you to compute $\pi$ rapidly using only basic arithmetic operations. If the initial guess for $\pi$ is rational, then all numbers are rational. The function $x \rightarrow \sin(x)$ appears early on, but a suitable approximation is derived at the end.

Fixed point iterations allow you to refine an approximation. Consider the iteration given by $$x_{n+1} = g(x_n),$$ where $$g(x) = x + \sin(x)$$ and $$x_0 \approx \pi$$ will be chosen later. It is easy to see that $\pi$ is a fixed point for $g$, i.e., $$g(\pi) = \pi.$$ We will now show that $x_n$ convergences to $\pi$ cubically, provided that $x_0$ is sufficiently close to $\pi$. Let $x \in \mathbb{R}$ be given. By Taylor's formula, there exists at least one $\xi$ between $x$ and $\pi$, such that $$ g(x) - \pi = g(x) - g(\pi) = g'(\pi)(x - \pi) + \frac{g''(\pi)}{2}(x - \pi)^2 + \frac{g^{(3)}(\xi)}{6}(x-\pi)^3.$$ However, since $$g'(x) = 1 +\cos(x), \quad g''(x) = - \sin(x),$$ this reduces to $$ g(x) - \pi = \frac{g^{(3)}(\xi)}{6}(x-\pi)^3.$$ It follows that $$ |\pi - g(x)| \leq \frac{1}{6}|\pi-x|^3,$$ because $$g^{(3)}(t) = - \cos(t)$$ is bounded by unity everywhere. In terms of the fixed point iteration we have $$|\pi - x_{n+1}| \leq \frac{1}{6} |\pi - x_n|^3.$$ By induction, we discover that $$ |\pi - x_n| \leq \left(\frac{1}{6}\right)^{m(n)} |\pi - x_0|^{3^n}, \quad m(n) = \frac{3^n - 1}{2}.$$ Convergence is assured if $x_0$ is chosen such that $$|\pi - x_0|^3 < 1.$$

This leaves us with the task of computing $x \rightarrow \sin(x)$ for $x$ close to $\pi$. For this problem we will utilize the trigonometric identity $$\sin(3\theta) = 3 \sin(\theta) - 4 \sin(\theta)^3.$$ Let $x$ be close to $\pi$ and let $k$ be a large integer, such $\theta_0 = x/3^k$ is close to $0$ and $$ \sin(\theta_0) \approx \theta_0 - \frac{1}{6} \theta_0^3 $$ is an acceptable approximation. Now let $\theta_j = 3 \theta_{j-1}$. Then by design $\theta_k = 3^k \theta_0 = x$ and since $$ \sin( \theta_j) = 3 \sin(\theta_{j-1}) - 4 \sin(\theta_{j-1})^3$$ we have an iteration for computing $x \rightarrow \sin(x)$.

Carl Christian
  • 12,583
  • 1
  • 14
  • 37
0

I am not sure if you like Ramanujan’s rapidly-converging series, since there is one square root...

$$\frac1\pi=\frac{2\sqrt2}{9801}\sum_{k=0}^\infty \frac{(4k)!(1103+26390k)}{(k!)^4 396^{4k}}$$

If you really hate the $\sqrt2$, try some approximation of it, e.g. $$\sqrt2\approx \frac{577}{408}$$

ADDED:

To make the higher order terms look like a geometric series, I can derive a very loose error bound.

Using Stirling’s approximation: $$\frac{(4k)!}{(k!)^4}\approx \frac1{\sqrt2}\frac1{\pi^{3/2}}\frac{4^{4k}}{k^{3/2}}<\frac1{\sqrt2}\frac1{\pi^{3/2}}\frac{4^{4k}}{k}$$ Also, $$1103+26390k<26391k$$

Thus, the error $E$ for $\frac1\pi$ is bounded by $$E<C\sum^{\infty}_k\frac1{99^k}=\frac{C}{98\cdot99^{k-1}}$$ where $k$ is the number of terms you take and $$C=\frac{17594}{3267\pi^{3/2}}\approx 1$$ Or, $$E\approx\frac1{99^k}\approx100^{-k}$$

Szeto
  • 11,159
  • I suppose this one would never require me to start over "from scratch" because the root is outside the sum; I'd only have to recompute the root itself. Is there a known error bound for the algorithm? – Alex Kindel May 01 '18 at 06:31
  • @AlexKindel I only know that it converges nearly exponentially fast...I searched for the error bound in the internet but didn’t find anything useful... – Szeto May 01 '18 at 06:38
  • That's what I've found to be the case with most algorithms; people talk about what the convergence rate is, but if a absolute error bound is known, no-one bothers to specify what it is. It makes me wonder if there's usually a trivial way to figure it out that I'm missing. – Alex Kindel May 01 '18 at 06:43
  • @AlexKindel You can also write $\sqrt{2}$ as a continued fraction $1 + 1/(2 + 1/(2 + 1/(2 + \dots)))$, which seems very rational:) Just take enough many steps. – Antoine May 01 '18 at 06:45
  • @AlexKindel See my edited post. – Szeto May 01 '18 at 07:23
  • @Szeto This seems messy to use, since the expression for the error bound can also only be approximated, and the problem is possibly circular since pi itself appears in the expression. It's good to see how one can go about finding a bound for a series though, in any case. – Alex Kindel May 01 '18 at 07:43
  • @Szeto I bet there is a way to use this bound in practice, but I've settled on the BBP algorithm instead for its simplicity. – Alex Kindel May 01 '18 at 07:51
  • @AlexKindel Indeed, you can use $3<\pi<4$ to get rid of the $\pi$ in the bound. Actually, you can take $C=1$. Also, I’m not sure if you are aware that the geometric sum can be expressed in closed form. However, I agree that this bound is quite messy. Glad to see you’ve found something right for you! – Szeto May 01 '18 at 08:10
  • @AlexKindel You might be interested in the new approximation of the error which I just added to the answer. – Szeto May 01 '18 at 08:39
0

I've decided to use the BBP formula, linked by Alex D in the comments: $$\pi=\sum_{k=0}^\infty\frac{R(k)}{16^k}\text{ where }R(k)=\frac{4}{8k+1}-\frac{2}{8k+4}-\frac{1}{8k+5}-\frac{1}{8k+6}$$ I get an error bound by noting that $$R(k)>\frac{4}{8k+1}-\frac{2}{8k+4}-\frac{1}{8k+4}-\frac{1}{8k+4}=\frac{4}{8k+1}-\frac{4}{8k+4}>0$$ so each term of the series is positive and the sequence of partial sums increases monotonically. Taking for granted that the series does in fact converge to $\pi$, this means that every partial sum is an underestimate, and the error of the $n$th partial sum is equal to its tail. $$R'(k)=\frac{16}{(8x+4)^2}+\frac{8}{(8x+5)^2}+\frac{8}{(8x+6)^2}-\frac{32}{(8x+1)^2}=-\frac{122880x^5+354816x^4+405504x^3+228616x^2+63328x+6853}{(2x+1)^2(4x+3)^2(8x+1)^2(8x+5)^2}$$ which is negative for all $k\ge0$, so $R(k)$ strictly decreases over that range, and we have this for the tail of the $n$th partial sum: $$\sum_{k=n+1}^\infty\frac{R(k)}{16^k}<R(1)\sum_{k=n+1}^\infty\frac{1}{16^k}=\frac{106}{819}\left(\sum_{k=0}^\infty\frac{1}{16^k}-\sum_{k=0}^n\frac{1}{16^k}\right)=\frac{106}{819}\left(\frac{1}{1-\frac{1}{16}}-\frac{1-\frac{1}{16^n}}{1-\frac{1}{16}}\right)=\frac{106}{16^{n-1}*12285}$$

0

If you want to find a formula that converges to $\pi$ faster than Leibnitz's formula$$\pi=4\sum\limits_{k\geq0}\frac {(-1)^k}{2k+1},$$then just perform an Euler Transform on the infinite sum to get a faster formula for $\pi$ in terms of the factorial and double factorial.$$\pi=2\sum\limits_{k\geq0}\frac {k!}{(2k+1)!!}$$

First off, the transform gives us$$\frac {\pi}4\sum\limits_{n\geq0}\frac 1{2^{n+1}}\sum\limits_{m=0}^n\binom nm\frac {(-1)^m}{2m+1}$$The inner sum can be simplified by expanding the right-hand side's inner finite sum and realizing that it takes the form $2^n\frac {n!}{(2n+1)!!}$.$$\begin{align*}\sum\limits_{m=0}^0\binom 0m\frac {(-1)^m}{2m+1} & =2^0\cdot\frac {0!}{1!!}\\\sum\limits_{m=0}^1\binom 1m\frac {(-1)^m}{2m+1} & =2^1\cdot\frac {1!}{3!!}\\\sum\limits_{m=0}^2\binom 2m\frac {(-1)^m}{2m+1} & =2^2\cdot\frac {2!}{5!!}\\\cdots\qquad\cdots\qquad\cdots & \qquad\cdots\end{align*}$$Going on and simplifying the fraction, it's evident that we can rewrite the formula as$$\frac {\pi}2=\sum\limits_{n\geq0}\frac {n!}{(2n+1)!!}$$

Frank W
  • 5,897