Compare Calculation-time for two methods

173 Views Asked by At

Method $1$ (Machin): $$ \pi = 16 \left(\frac{1}{5} - \frac{1}{3\cdot 5^3} + \frac{1}{5\cdot 5^5} - \frac{1}{7\cdot 5^7} + \ldots\right) -4\left(\frac{1}{239} - \frac{1}{3\cdot 239^3} + \frac{1}{5\cdot 239^5} - \frac{1}{7\cdot 239^7} + \ldots\right) $$

Method $2$: $$ \pi = 2 \left(1 + \frac{1}{3} + \frac{1\cdot 2}{3\cdot 5} + \frac{1\cdot 2 \cdot 3}{3 \cdot 5 \cdot 7} + \frac{1\cdot 2 \cdot 3 \cdot 4}{3 \cdot 5 \cdot 7 \cdot 9} + \ldots\right) $$

For each iteration method $1$ get closer to pi rather than method $2$ but the computation time for method $2$ is better. Is there a way to calculate and compare the computation time for method $1$ and $2$ ?

1

There are 1 best solutions below

4
On BEST ANSWER

To apply methods, it is comfortable to keep pre-calculed values for current term:

Method $1$:

term = 1/5;
sign = 1;
x = 1;
y = 5;
value = term; (for pi/16)
for loop := 1 .. n
   sign := -sign; 
   x := x + 2;
   y := 25 * y;
   term := 1 / (x * y);
   value = value + sign * term;

value = value * 16;  

$1$ loop: $1$ division, $0-1$ branching, $2-3$ multiplications, $3$ simple arithmetic ops.

Method $2$:

term = 1;
x = 1;  dx = 0;
y = 1;  dy = 1;
value = term; (for pi/2)
for loop := 1 .. n
    dx = dx + 1;
    dy = dy + 2;
    x := x * dx;
    y := y * dy;
    term = term * x / y;
    value = value + term;

value = value * 2;  

$1$ loop: $1$ division, $3$ multiplications, $3$ simple arithmetic ops.

Since division is the slowest operation, methods are close by speed of calculation $n$ terms (method $2$ can be a bit slower).

How fast terms are decreasing for each method?

Method $1$: $\left|\dfrac{u_{n+1}}{u_n}\right| = \dfrac{(2n+1) 5^{2n+1}}{(2n+3)5^{2n+3}} = \dfrac{(2n+1)}{(2n+3)25} \approx \dfrac{1}{25}$.

Method $2$: $\dfrac{u_{n+1}}{u_n} = \dfrac{n}{2n+1} \approx \dfrac{1}{2}$.

So "term multiplier" $\dfrac{1}{25}$ in method $1$ is much better than $\dfrac{1}{2}$ in method $2$.

Since (for "term multipliers" not greater than $0.5$) we can estimate error by the last term roughly, $-$

error for method $1$: $\epsilon_1(n) \sim \dfrac{1}{25^n}$;

error for method $2$: $\epsilon_2(n) \sim \dfrac{1}{2^n}$.


So, method $1$ is more effective (due to this factor $\dfrac{1}{25}$).