user
Machin-like formulas   are useful for efficiently computing numerical approximations for 

π
{\displaystyle {\displaystyle \pi }}

Task

Verify the following Machin-like formulas are correct by calculating the value of **tan**   (*right hand side)* for each equation using exact arithmetic and showing they equal **1**:

:   π
    4
    =
    arctan
    ⁡


    1
    2
    +
    arctan
    ⁡


    1
    3
    {\displaystyle {\displaystyle {\pi \over 4}=\arctan {1 \over 2}+\arctan {1 \over 3}}}
:   π
    4
    =
    2
    arctan
    ⁡


    1
    3
    +
    arctan
    ⁡


    1
    7
    {\displaystyle {\displaystyle {\pi \over 4}=2\arctan {1 \over 3}+\arctan {1 \over 7}}}
:   π
    4
    =
    4
    arctan
    ⁡


    1
    5
    −
    arctan
    ⁡


    1
    239
    {\displaystyle {\displaystyle {\pi \over 4}=4\arctan {1 \over 5}-\arctan {1 \over 239}}}
:   π
    4
    =
    5
    arctan
    ⁡


    1
    7
    +
    2
    arctan
    ⁡


    3
    79
    {\displaystyle {\displaystyle {\pi \over 4}=5\arctan {1 \over 7}+2\arctan {3 \over 79}}}
:   π
    4
    =
    5
    arctan
    ⁡


    29
    278
    +
    7
    arctan
    ⁡


    3
    79
    {\displaystyle {\displaystyle {\pi \over 4}=5\arctan {29 \over 278}+7\arctan {3 \over 79}}}
:   π
    4
    =
    arctan
    ⁡


    1
    2
    +
    arctan
    ⁡


    1
    5
    +
    arctan
    ⁡


    1
    8
    {\displaystyle {\displaystyle {\pi \over 4}=\arctan {1 \over 2}+\arctan {1 \over 5}+\arctan {1 \over 8}}}
:   π
    4
    =
    4
    arctan
    ⁡


    1
    5
    −
    arctan
    ⁡


    1
    70
    +
    arctan
    ⁡


    1
    99
    {\displaystyle {\displaystyle {\pi \over 4}=4\arctan {1 \over 5}-\arctan {1 \over 70}+\arctan {1 \over 99}}}
:   π
    4
    =
    5
    arctan
    ⁡


    1
    7
    +
    4
    arctan
    ⁡


    1
    53
    +
    2
    arctan
    ⁡


    1
    4443
    {\displaystyle {\displaystyle {\pi \over 4}=5\arctan {1 \over 7}+4\arctan {1 \over 53}+2\arctan {1 \over 4443}}}
:   π
    4
    =
    6
    arctan
    ⁡


    1
    8
    +
    2
    arctan
    ⁡


    1
    57
    +
    arctan
    ⁡


    1
    239
    {\displaystyle {\displaystyle {\pi \over 4}=6\arctan {1 \over 8}+2\arctan {1 \over 57}+\arctan {1 \over 239}}}
:   π
    4
    =
    8
    arctan
    ⁡


    1
    10
    −
    arctan
    ⁡


    1
    239
    −
    4
    arctan
    ⁡


    1
    515
    {\displaystyle {\displaystyle {\pi \over 4}=8\arctan {1 \over 10}-\arctan {1 \over 239}-4\arctan {1 \over 515}}}
:   π
    4
    =
    12
    arctan
    ⁡


    1
    18
    +
    8
    arctan
    ⁡


    1
    57
    −
    5
    arctan
    ⁡


    1
    239
    {\displaystyle {\displaystyle {\pi \over 4}=12\arctan {1 \over 18}+8\arctan {1 \over 57}-5\arctan {1 \over 239}}}
:   π
    4
    =
    16
    arctan
    ⁡


    1
    21
    +
    3
    arctan
    ⁡


    1
    239
    +
    4
    arctan
    ⁡


    3
    1042
    {\displaystyle {\displaystyle {\pi \over 4}=16\arctan {1 \over 21}+3\arctan {1 \over 239}+4\arctan {3 \over 1042}}}
:   π
    4
    =
    22
    arctan
    ⁡


    1
    28
    +
    2
    arctan
    ⁡


    1
    443
    −
    5
    arctan
    ⁡


    1
    1393
    −
    10
    arctan
    ⁡


    1
    11018
    {\displaystyle {\displaystyle {\pi \over 4}=22\arctan {1 \over 28}+2\arctan {1 \over 443}-5\arctan {1 \over 1393}-10\arctan {1 \over 11018}}}
:   π
    4
    =
    22
    arctan
    ⁡


    1
    38
    +
    17
    arctan
    ⁡


    7
    601
    +
    10
    arctan
    ⁡


    7
    8149
    {\displaystyle {\displaystyle {\pi \over 4}=22\arctan {1 \over 38}+17\arctan {7 \over 601}+10\arctan {7 \over 8149}}}
:   π
    4
    =
    44
    arctan
    ⁡


    1
    57
    +
    7
    arctan
    ⁡


    1
    239
    −
    12
    arctan
    ⁡


    1
    682
    +
    24
    arctan
    ⁡


    1
    12943
    {\displaystyle {\displaystyle {\pi \over 4}=44\arctan {1 \over 57}+7\arctan {1 \over 239}-12\arctan {1 \over 682}+24\arctan {1 \over 12943}}}
:   π
    4
    =
    88
    arctan
    ⁡


    1
    172
    +
    51
    arctan
    ⁡


    1
    239
    +
    32
    arctan
    ⁡


    1
    682
    +
    44
    arctan
    ⁡


    1
    5357
    +
    68
    arctan
    ⁡


    1
    12943
    {\displaystyle {\displaystyle {\pi \over 4}=88\arctan {1 \over 172}+51\arctan {1 \over 239}+32\arctan {1 \over 682}+44\arctan {1 \over 5357}+68\arctan {1 \over 12943}}}

and confirm that the following formula is *incorrect* by showing   **tan**   (*right hand side)*   is *not*   **1**:

:   π
    4
    =
    88
    arctan
    ⁡


    1
    172
    +
    51
    arctan
    ⁡


    1
    239
    +
    32
    arctan
    ⁡


    1
    682
    +
    44
    arctan
    ⁡


    1
    5357
    +
    68
    arctan
    ⁡


    1
    12944
    {\displaystyle {\displaystyle {\pi \over 4}=88\arctan {1 \over 172}+51\arctan {1 \over 239}+32\arctan {1 \over 682}+44\arctan {1 \over 5357}+68\arctan {1 \over 12944}}}

These identities are useful in calculating the values:

:   tan
    ⁡
    (
    a
    +
    b
    )
    =

    tan
    ⁡
    (
    a
    )
    +
    tan
    ⁡
    (
    b
    )

    1
    −
    tan
    ⁡
    (
    a
    )
    tan
    ⁡
    (
    b
    )
    {\displaystyle {\displaystyle \tan(a+b)={\tan(a)+\tan(b) \over 1-\tan(a)\tan(b)}}}

:   tan
    ⁡

    (

    arctan
    ⁡


    a
    b
    )
    =


    a
    b
    {\displaystyle {\displaystyle \tan \left(\arctan {a \over b}\right)={a \over b}}}

:   tan
    ⁡
    (
    −
    a
    )
    =
    −
    tan
    ⁡
    (
    a
    )
    {\displaystyle {\displaystyle \tan(-a)=-\tan(a)}}

You can store the equations in any convenient data structure, but for extra credit parse them from human-readable text input.

Note: to formally prove the formula correct, it would have to be shown that *−
3
π
4
{\displaystyle {\displaystyle {-3\pi \over 4}}}
 < right hand side < 

5
π
4
{\displaystyle {\displaystyle {5\pi \over 4}}}* due to *tan
⁡
(
)
{\displaystyle {\displaystyle \tan()}}* periodicity.
assistant
(formerly Perl 6)

**Works with**: rakudo version 2018.03

The coercion to FatRat provides for exact computation for all input.

**Translation of**: Perl

```
sub taneval ($coef, $f) {
  return 0 if $coef == 0;
  return $f if $coef == 1;
  return -taneval(-$coef, $f) if $coef < 0;

  my $a = taneval($coef+>1, $f);
  my $b = taneval($coef - $coef+>1, $f);
  ($a+$b)/(1-$a*$b);
}

sub tans (@xs) {
  return taneval(@xs[0;0], @xs[0;1].FatRat) if @xs == 1;

  my $a = tans(@xs[0 .. (-1+@xs+>1)]);
  my $b = tans(@xs[(-1+@xs+>1)+1 .. -1+@xs]);
  ($a+$b)/(1-$a*$b);
}

sub verify (@eqn) {
  printf "%5s (%s)\n", (tans(@eqn) == 1) ?? "OK" !! "Error",
    (map { "[{.[0]} {.[1].nude.join('/')}]" }, @eqn).join(' ');
}

verify($_) for
   ([[1,1/2], [1,1/3]],
    [[2,1/3], [1,1/7]],
    [[4,1/5], [-1,1/239]],
    [[5,1/7], [2,3/79]],
    [[5,29/278], [7,3/79]],
    [[1,1/2], [1,1/5], [1,1/8]],
    [[4,1/5], [-1,1/70], [1,1/99]],
    [[5,1/7], [4,1/53], [2,1/4443]],
    [[6,1/8], [2,1/57], [1,1/239]],
    [[8,1/10], [-1,1/239], [-4,1/515]],
    [[12,1/18], [8,1/57], [-5,1/239]],
    [[16,1/21], [3,1/239], [4,3/1042]],
    [[22,1/28], [2,1/443], [-5,1/1393], [-10,1/11018]],
    [[22,1/38], [17,7/601], [10,7/8149]],
    [[44,1/57], [7,1/239], [-12,1/682], [24,1/12943]],
    [[88,1/172], [51,1/239], [32,1/682], [44,1/5357], [68,1/12943]],
    [[88,1/172], [51,1/239], [32,1/682], [44,1/5357], [68,1/21944]]
    );

```

Output:

```
   OK ([1 1/2] [1 1/3])
   OK ([2 1/3] [1 1/7])
   OK ([4 1/5] [-1 1/239])
   OK ([5 1/7] [2 3/79])
   OK ([5 29/278] [7 3/79])
   OK ([1 1/2] [1 1/5] [1 1/8])
   OK ([4 1/5] [-1 1/70] [1 1/99])
   OK ([5 1/7] [4 1/53] [2 1/4443])
   OK ([6 1/8] [2 1/57] [1 1/239])
   OK ([8 1/10] [-1 1/239] [-4 1/515])
   OK ([12 1/18] [8 1/57] [-5 1/239])
   OK ([16 1/21] [3 1/239] [4 3/1042])
   OK ([22 1/28] [2 1/443] [-5 1/1393] [-10 1/11018])
   OK ([22 1/38] [17 7/601] [10 7/8149])
   OK ([44 1/57] [7 1/239] [-12 1/682] [24 1/12943])
   OK ([88 1/172] [51 1/239] [32 1/682] [44 1/5357] [68 1/12943])
Error ([88 1/172] [51 1/239] [32 1/682] [44 1/5357] [68 1/21944])
```