There are different ways to calculate the value of the number $$\pi$$ by means of a computer. You can, for example, calculate the partial sums of the Gregory-Leibniz sequence: \[ \pi = \frac41 - \frac43 + \frac45 - \frac47 + \dots \] The Indian mathematician Madhaya of Sangamagrama proposed an alternative sequence development in the fourteenth century: \[ \pi = \sqrt{12}\,\left( 1 - \frac1{3\cdot 3} + \frac1{5\cdot 3^2} - \frac1{7\cdot 3^3} + \cdots \right) \]
Write a function GL that prints the sum of the first n terms of the Gregory-Leibniz sequence. The amount of terms $$n$$ should be given to the function as a parameter.
Write a function MvS that calculates the sum of the first n terms of the Madhava of Sangamagrama sequence. The number of terms $$n$$ should be given to the function as a parameter.
Examine which of both sequences converges the fastest. Use this sequence to write a function approach_pi. This function should allow to determine an approached value of $$\pi$$, that is precise to $$n$$ decimals. The value $$n$$ should be given as an argument of the function. To determine the accuracy of the approach, you should check whether the difference between two consecutive terms in the sequence is smaller than $$10^{-n-1}$$. When the difference between the ($$i-1$$)th and the $$i$$th term becomes smaller than $$10^{-n-1}$$, the $$i$$th partial sum forms an approach of $$\pi$$ to $$n$$ decimals precisely. The function should give the tuple $$(i, p)$$ as a result, $$i$$ is the number of calculated terms and $$p$$ is the approached value of $$\pi$$.
>>> GL(2)
2.666666666666667
>>> MvS(2)
3.0792014356780038
>>> approach_pi(2)
(8, 3.141568715941784)