In 1995, Stanley Rabinowitz and Stan Wagon found an interesting algorithm to generate the digits of \$\pi\$ one by one without storing the previous results. The algorithm is called the spigot algorithm. It is based on the following formula:
$$ \pi = 2 \left(1 + \frac{1}{3} \left(1 + \frac{2}{5} \left(1 + \frac{3}{7} \left(1 + \cdots \frac{k}{2k+1} \left(1 + \cdots\right)\right)\right)\right)\right) $$
This formula can be derived from the Leibniz formula for \$\pi\$ using the Euler's transformation.
(The formula was already well-known before the algorithm was found, but I can't find a name for it. So I call it the Rabinowitz-Wagon \$\pi\$ formula as @aeh5040 suggested in a sandbox comment.)
Using this formula, we can approximate \$\pi\$ by a sequence of fractions. The first few terms are:
$$\begin{aligned} 2 &= 2, \\ \frac{8}{3} &= 2 \left(1 + \frac{1}{3}\right), \\ \frac{44}{15} &= 2 \left(1 + \frac{1}{3} \left(1 + \frac{2}{5}\right)\right), \\ \frac{64}{21} &= 2 \left(1 + \frac{1}{3} \left(1 + \frac{2}{5} \left(1 + \frac{3}{7}\right)\right)\right), \\ \dots&\dots \end{aligned}$$
Task
In this challenge, you need to output this sequence of fractions.
You can output a fraction \$\frac{a}{b}\$ in any reasonable format, e.g. a built-in rational type, a pair of integers \$(a, b)\$, or a string a/b
. You don't need to simplify the fraction. For example, you can output \$\frac{8}{3}\$ as \$\frac{40}{15}\$.
As with standard sequence challenges, you may choose to:
- Take an integer \$n\$ as input and output the \$n\$th term of the sequence.
- Take an integer \$n\$ as input and output the first \$n\$ terms of the sequence.
- Take no input and output the sequence indefinitely.
The indices can be 0-based or 1-based.
This is code-golf, so the shortest code in bytes in each language wins.
Test cases
0 -> 2
1 -> 8/3
2 -> 44/15
3 -> 64/21
4 -> 976/315
5 -> 10816/3465
6 -> 141088/45045
7 -> 47104/15015
8 -> 2404096/765765
9 -> 45693952/14549535
10 -> 45701632/14549535
11 -> 80863232/25741485
12 -> 5256312832/1673196525
13 -> 3153846272/1003917915
14 -> 457311809536/145568097675
15 -> 833925152768/265447707525
16 -> 4725585805312/1504203675975
17 -> 14176771899392/4512611027925
18 -> 524540820979712/166966608033225
19 -> 104908189597696/33393321606645