8

Context: Sorry for a weird question, I'm kind of a technical person, but I'm not good at math -- so I'd be grateful if you could point at what I could read about the following.

I was designing an interface to add cumulative calculations to my python lib and stumbled across a thing, which surprised me. Not to bring the whole problem, here is a piece of trivial Python code:

value = 1
for i in range(2, 1000):
    value = i / value
    print(i, value)

It just goes over a sequence of numbers from 2 to 1000 and calculates cumulative division, here are first and last 10 lines the output:

2 2.0
3 1.5
4 2.6666666666666665
5 1.875
6 3.2
7 2.1875
8 3.657142857142857
9 2.4609375
10 4.063492063492063
...
990 39.44456819288283
991 25.123864841263767
992 39.48437098621571
993 25.149191317918266
994 39.52413369617162
995 25.174492315219993
996 39.563856443604955
997 25.19976790991399
998 39.60353934876404
999 25.2250181783608

I was curious about the ratio: 39.60353934876404 / 25.2250181783608 = 1.5700103392883897. For some reason it was close to $\frac{\pi}{2}$.

I increased the number of iterations:

import math
value = 1
for i in range(2, 1000000):
    value_ = i / value
    if i % 1000 == 0:
        x = (value_ / value if value_ > value else value / value_) * 2
        print(i, x, abs(math.pi - x))
    value = value_

990000 3.14159424025328 1.5866634868189067e-06 991000 3.141594238652194 1.585062400888404e-06 992000 3.1415942370543477 1.5834645545886872e-06 993000 3.1415942354597197 1.5818699266034741e-06 994000 3.141594233868303 1.5802785098273375e-06 995000 3.1415942322800725 1.5786902793912816e-06 996000 3.141594230695048 1.5771052548352316e-06 997000 3.1415942291131915 1.5755233984116046e-06 998000 3.141594227534505 1.5739447118967576e-06 999000 3.1415942259589693 1.5723691761948544e-06

and it made it even more interesting :) Not only the value became more precise, but its error got digits from $\frac{\pi}{2}$. What is a name of this thing? (so I could go and search the net for this)

Thank you!

3 Answers3

9

Your construction is easy to track if you don't do the operations:

$$1,\frac{2}{1},\frac{1\times 3}{2},\frac{2\times 4}{1\times 3},\frac{1\times 3\times 5}{2\times 4},\frac{2\times 4\times 6}{1\times 3\times 5},\cdots$$

Each step you calculate value = i/value = 1/value*i which invert the previous fraction and multiply it by the next integer.

After some steps, when you take the quotient of two consecutive terms you'll get, for example $$\frac{\frac{2\times 4 \times 6 \times \cdots \times 998}{1\times 3\times 5\times \cdots\times 997}}{\frac{1\times 3\times 5\cdots\times 999}{2\times 4\times 6\times \cdots\times998} } = \frac{2\times 2\times 4\times 4 \times\cdots \times 996\times 996\times 998\times 998}{1\times 3\times 3\times 5\times\cdots\times 995\times 997\times 997\times 999}$$

This quotients converge to an infinite product known as the Wallis Product:

$$\frac{2\times 2\times 4\times 4 \times 6\times 6\times \cdots}{1\times 3\times 3\times 5\times 5\times 7\times \cdots} = \frac{\pi}{2}$$

jjagmath
  • 18,214
2

Continuing off the comments, if you look at the double factorial wikipedia page at the end of the linked section, there is an approximation

$$\frac{(2n)!!}{(2n-1)!!} \approx \sqrt{\pi n}$$

So, $$\frac{x_{2n}}{x_{2n-1}} = \frac{1}{2n} \left ( \frac{(2n)!!}{(2n-1)!!} \right )^2 \approx \frac{1}{2n} \cdot \sqrt{\pi n}^2 = \frac \pi 2$$

which is what you observed that the ratios were converging to. I am not sure about the error term, but you might have a look at the wikipedia page to see how the approximation is derived.

jacob
  • 473
2

Starting from @Jacob's answer, using Stirling-like expansions $$x_n=\frac{(2n)!!}{(2n-1)!!}= \sqrt{n} \,\,\left(\frac{\pi }{2}\right)^{\frac{(-1)^n}{2}}\,\,\Bigg[1+\frac{1}{4 n}+\frac{1}{32 n^2}+O\left(\frac{1}{n^3}\right) \Bigg]$$

Using the above formula $$x_{100}\sim \frac{320801 }{32000}\sqrt{\frac{\pi }{2}}\quad\quad\quad \text{absolute error}=4.908\times 10^{-7}$$ $$x_{101}\sim \frac{327241}{1616 \sqrt{202 \pi }}\quad\quad\quad \text{absolute error}=3.048\times 10^{-7}$$

So

$$\frac{x_{2n}}{x_{2n-1}} = \frac{1}{2n} \left ( \frac{(2n)!!}{(2n-1)!!} \right )^2= \frac \pi 2 \Bigg[ 1+\frac{1}{4 n}+\frac{1}{32 n^2}+O\left(\frac{1}{n^3}\right) \Bigg]$$