How to resolve the algorithm Check Machin-like formulas step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Check Machin-like formulas step by step in the XPL0 programming language

Table of Contents

Problem Statement

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

π

{\displaystyle \pi }

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: and confirm that the following formula is incorrect by showing   tan   (right hand side)   is not   1: These identities are useful in calculating the values:

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 p i

4

{\displaystyle {-3pi \over 4}}

< right hand side <

5 p i

4

{\displaystyle {5pi \over 4}}

due to

tan ⁡ ( )

{\displaystyle \tan()}

periodicity.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Check Machin-like formulas step by step in the XPL0 programming language

Source code in the xpl0 programming language

code ChOut=8, Text=12;  \intrinsic routines
int  Number(18);        \numbers from equations
def  LF=$0A;            \ASCII line feed (end-of-line character)

func Parse(S);          \Convert numbers in string S to binary in Number array
char S;
int  I, Neg;

        proc GetNum;    \Get number from string S
        int  N;
        [while S(0)<^0 ! S(0)>^9 do S:= S+1;
        N:= S(0)-^0;  S:= S+1;
        while S(0)>=^0 & S(0)<=^9 do
                [N:= N*10 + S(0) - ^0;  S:= S+1];
        Number(I):= N;  I:= I+1;
        ];

[while S(0)#^= do S:= S+1;      \skip to "="
I:= 0;
loop    [Neg:= false;           \assume positive term
        loop    [S:= S+1;       \next char
                case S(0) of
                  LF:   [Number(I):= 0;  return S+1];   \mark end of array
                  ^-:   Neg:= true;                     \term is negative
                  ^a:   [Number(I):= 1;  I:= I+1; quit] \no coefficient so use 1
                other if S(0)>=^0 & S(0)<=^9 then       \if digit
                        [S:= S-1;  GetNum;  quit];      \backup and get number
                ];
        GetNum;                                         \numerator
        if Neg then Number(I-1):= -Number(I-1);         \tan(-a) = -tan(a)
        GetNum;                                         \denominator
        ];
];


func GCD(U, V);         \Return the greatest common divisor of U and V
int  U, V;
int  T;
[while V do             \Euclid's method
    [T:= U;  U:= V;  V:= rem(T/V)];
return abs(U);
];

proc Verify;            \Verify that tangent of equation = 1 (i.e: E = F)
int  E, F, I, J;

    proc Machin(A, B, C, D);
    int  A, B, C, D;
    int  Div;
    \tan(a+b) = (tan(a) + tan(b)) / (1 - tan(a)*tan(b))
    \tan(arctan(A/B) + arctan(C/D))
    \   = (tan(arctan(A/B)) + tan(arctan(C/D))) / (1 - tan(arctan(A/B))*tan(arctan(C/D)))
    \   = (A/B + C/D) / (1 - A/B*C/D)
    \   = (A*D/B*D + B*C/B*D) / (B*D/B*D - A*C/B*D)
    \   = (A*D + B*C) / (B*D - A*C)
    [E:= A*D + B*C;  F:= B*D - A*C;
    Div:= GCD(E, F);    \keep integers from getting too big
    E:= E/Div;  F:= F/Div;
    ];

[E:= 0;  F:= 1;  I:= 0;
while Number(I) do
    [for J:= 1 to Number(I) do
        Machin(E, F, Number(I+1), Number(I+2));
    I:= I+3;
    ];
Text(0, if E=F then "Yes  " else "No   ");
];


char S, SS;  int I;
[S:= "pi/4 = arctan(1/2) + arctan(1/3) 
pi/4 = 2*arctan(1/3) + arctan(1/7)
pi/4 = 4*arctan(1/5) - arctan(1/239)
pi/4 = 5*arctan(1/7) + 2*arctan(3/79)
pi/4 = 5*arctan(29/278) + 7*arctan(3/79)
pi/4 = arctan(1/2) + arctan(1/5) + arctan(1/8) 
pi/4 = 4*arctan(1/5) - arctan(1/70) + arctan(1/99) 
pi/4 = 5*arctan(1/7) + 4*arctan(1/53) + 2*arctan(1/4443)
pi/4 = 6*arctan(1/8) + 2*arctan(1/57) + arctan(1/239)
pi/4 = 8*arctan(1/10) - arctan(1/239) - 4*arctan(1/515)
pi/4 = 12*arctan(1/18) + 8*arctan(1/57) - 5*arctan(1/239)
pi/4 = 16*arctan(1/21) + 3*arctan(1/239) + 4*arctan(3/1042)
pi/4 = 22*arctan(1/28) + 2*arctan(1/443) - 5*arctan(1/1393) - 10*arctan(1/11018)
pi/4 = 22*arctan(1/38) + 17*arctan(7/601) + 10*arctan(7/8149)
pi/4 = 44*arctan(1/57) + 7*arctan(1/239) - 12*arctan(1/682) + 24*arctan(1/12943)
pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12943)
pi/4 = 88*arctan(1/172) + 51*arctan(1/239) + 32*arctan(1/682) + 44*arctan(1/5357) + 68*arctan(1/12944)
 ";                             \Python version of equations (thanks!)
for I:= 1 to 17 do
        [SS:= S;                \save start of string line
        S:= Parse(S);           \returns start of next line
        Verify;                 \correct Machin equation? Yes or No
        repeat ChOut(0, SS(0)); SS:= SS+1 until SS(0)=LF;  ChOut(0, LF); \show equation
        ];
]

  

You may also check:How to resolve the algorithm Towers of Hanoi step by step in the Quackery programming language
You may also check:How to resolve the algorithm Vigenère cipher step by step in the Delphi programming language
You may also check:How to resolve the algorithm UPC step by step in the Ksh programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the GML programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Potion programming language