How to resolve the algorithm Continued fraction step by step in the REXX programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Continued fraction step by step in the REXX programming language

Table of Contents

Problem Statement

The task is to write a program which generates such a number and prints a real representation of it. The code should be tested by calculating and printing the square root of 2, Napier's Constant, and Pi, using the following coefficients: For the square root of 2, use

a

0

= 1

{\displaystyle a_{0}=1}

then

a

N

= 2

{\displaystyle a_{N}=2}

.

b

N

{\displaystyle b_{N}}

is always

1

{\displaystyle 1}

. For Napier's Constant, use

a

0

= 2

{\displaystyle a_{0}=2}

, then

a

N

= N

{\displaystyle a_{N}=N}

.

b

1

= 1

{\displaystyle b_{1}=1}

then

b

N

= N − 1

{\displaystyle b_{N}=N-1}

. For Pi, use

a

0

= 3

{\displaystyle a_{0}=3}

then

a

N

= 6

{\displaystyle a_{N}=6}

.

b

N

= ( 2 N − 1

)

2

{\displaystyle b_{N}=(2N-1)^{2}}

.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Continued fraction step by step in the REXX programming language

Source code in the rexx programming language

/*REXX program  calculates and displays  values of  various  continued fractions.       */
parse arg terms digs .
if terms=='' | terms==","  then terms=500
if  digs=='' |  digs==","  then  digs=100
numeric digits digs                              /*use  100  decimal digits for display.*/
b.=1                                             /*omitted ß terms are assumed to be  1.*/
/*══════════════════════════════════════════════════════════════════════════════════════*/
a.=2;                                                           call tell '√2',      cf(1)
/*══════════════════════════════════════════════════════════════════════════════════════*/
a.=1;  do N=2  by  2  to terms; a.N=2; end;                     call tell '√3',      cf(1)     /*also:  2∙sin(π/3) */
/*══════════════════════════════════════════════════════════════════════════════════════*/
a.=2                  /*              ___ */
      do N=2  to 17   /*generalized  √ N  */
      b.=N-1;                          NN=right(N, 2);          call tell 'gen √'NN, cf(1)
      end   /*N*/
/*══════════════════════════════════════════════════════════════════════════════════════*/
a.=2;   b.=-1/2;                                                call tell 'gen √ ½', cf(1)
/*══════════════════════════════════════════════════════════════════════════════════════*/
  do j=1 for terms; a.j=j;  if j>1   then b.j=a.p; p=j; end;    call tell 'e',       cf(2)
/*══════════════════════════════════════════════════════════════════════════════════════*/
a.=1;                                                           call tell 'φ, phi',  cf(1)
/*══════════════════════════════════════════════════════════════════════════════════════*/
a.=1;    do j=1 for terms;  if j//2  then a.j=j;        end;    call tell 'tan(1)',  cf(1)
/*══════════════════════════════════════════════════════════════════════════════════════*/
         do j=1 for terms;                a.j=2*j+1;    end;    call tell 'coth(1)', cf(1)
/*══════════════════════════════════════════════════════════════════════════════════════*/
         do j=1 for terms;                a.j=4*j+2;    end;    call tell 'coth(½)', cf(2)    /*also:  [e+1]÷[e-1] */
/*══════════════════════════════════════════════════════════════════════════════════════*/
                     terms=100000
a.=6;    do j=1  for terms;  b.j=(2*j-1)**2;            end;    call tell 'π, pi',   cf(3)
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cf:      procedure expose a. b. terms;  parse arg C;     !=0;    numeric digits 9+digits()
                                          do k=terms  by -1  for terms;  d=a.k+!;  !=b.k/d
                                          end   /*k*/
         return !+C
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell:    parse arg ?,v;   $=left(format(v)/1,1+digits());    w=50    /*50 bytes of terms*/
         aT=;     do k=1;  _=space(aT a.k);  if length(_)>w  then leave;  aT=_;  end /*k*/
         bT=;     do k=1;  _=space(bT b.k);  if length(_)>w  then leave;  bT=_;  end /*k*/
                          say right(?,8)   "="    $     '  α terms='aT  ...
         if b.1\==1  then say right("",12+digits())     '  ß terms='bT  ...
         a=;   b.=1;  return       /*only 50 bytes of  α & ß terms  ↑   are displayed.  */


/* REXX **************************************************************
* Derived from PL/I with a little "massage"
* SQRT2=     1.41421356237309505              <- PL/I Result
*            1.41421356237309504880168872421  <- REXX Result 30 digits
* NAPIER=    2.71828182845904524
*            2.71828182845904523536028747135
* PI=        3.14159262280484695
*            3.14159262280484694855146925223
* 06.09.2012 Walter Pachl
**********************************************************************/
  Numeric Digits 30
  Parse Value '1 2 3 0 0' with Sqrt2 napier pi a b
  Say left('SQRT2=' ,10) calc(sqrt2,  200)
  Say left('NAPIER=',10) calc(napier, 200)
  Say left('PI='    ,10) calc(pi,     200)
  Exit

Get_Coeffs: procedure Expose a b Sqrt2 napier pi
  Parse Arg form, n
  select
    when form=Sqrt2 Then do
      if n > 0 then a = 2; else a = 1
      b = 1
      end
    when form=Napier Then do
      if n > 0 then a = n; else a = 2
      if n > 1 then b = n - 1; else b = 1
      end
    when form=pi Then do
      if n > 0 then a = 6; else a = 3
      b = (2*n - 1)**2
      end
    end
  Return

Calc: procedure Expose a b Sqrt2 napier pi
  Parse Arg form,n
  Temp=0
  do ni = n to 1 by -1
    Call Get_Coeffs form, ni
    Temp = B/(A + Temp)
    end
  call Get_Coeffs  form, 0
  return (A + Temp)


/* REXX *************************************************************
* The task description specifies a continued fraction for pi
* that gives a reasonable approximation.
* Literature shows a better CF that yields pi with a precision of
* 200 digits.
* http://de.wikipedia.org/wiki/Kreiszahl
*                    1
* pi = 3 + ------------------------
*                      1
*          7 + --------------------
*                         1
*              15 + ---------------
*                            1
*                   1 + -----------
*
*                       292 + ...
*
* This program uses that CF and shows the first 50 digits
* PI =3.1415926535897932384626433832795028841971693993751...
* PIX=3.1415926535897932384626433832795028841971693993751...
* 201 correct digits
* 18.09.2012 Walter Pachl
**********************************************************************/
  pi='3.1415926535897932384626433832795028841971'||,
     '693993751058209749445923078164062862089986280348'||,
     '253421170679821480865132823066470938446095505822'||,
     '317253594081284811174502841027019385211055596446'||,
     '229489549303819644288109756659334461284756482337'||,
     '867831652712019091456485669234603486104543266482'||,
     '133936072602491412737245870066063155881748815209'||,
     '209628292540917153643678925903600113305305488204'||,
     '665213841469519415116094330572703657595919530921'||,
     '861173819326117931051185480744623799627495673518'||,
     '857527248912279381830119491298336733624'
  Numeric Digits 1000
  al='7 15 1 292 1 1 1 2 1 3 1 14 2 1 1 2 2 2 2 1 84 2',
     '1 1 15 3 13 1 4 2 6 6 99 1 2 2 6 3 5 1 1 6 8 1 7 1 2',
     '3 7 1 2 1 1 12 1 1 1 3 1 1 8 1 1 2 1 6 1 1 5 2 2 3 1',
     '2 4 4 16 1 161 45 1 22 1 2 2 1 4 1 2 24 1 2 1 3 1 2',
     '1 1 10 2 5 4 1 2 2 8 1 5 2 2 26 1 4 1 1 8 2 42 2 1 7',
     '3 3 1 1 7 2 4 9 7 2 3 1 57 1 18 1 9 19 1 2 18 1 3 7',
     '30 1 1 1 3 3 3 1 2 8 1 1 2 1 15 1 2 13 1 2 1 4 1 12',
     '1 1 3 3 28 1 10 3 2 20 1 1 1 1 4 1 1 1 5 3 2 1 6 1 4'
  a.=3
  Do i=1 By 1 while al<>''
    Parse Var al a.i al
    End
  pix=calc(194)
  Do e=1 To length(pi)
    If substr(pix,e,1)<>substr(pi,e,1) Then Leave
    End
  Numeric Digits 50
  Say 'PI ='||(pi+0)||'...'
  Say 'PIX='||(pix+0)||'...'
  Say (e-1) 'correct digits'
  Exit

Get_Coeffs: procedure Expose a b a.
  Parse Arg n
  a=a.n
  b=1
  Return

Calc: procedure Expose a b a.
  Parse Arg n
  Temp=0
  do ni = n to 1 by -1
    Call Get_Coeffs ni
    Temp = B/(A + Temp)
    end
  call Get_Coeffs 0
  return (A + Temp)


  

You may also check:How to resolve the algorithm Mutex step by step in the Read write mutex programming language
You may also check:How to resolve the algorithm Linear congruential generator step by step in the Ruby programming language
You may also check:How to resolve the algorithm Doubly-linked list/Element definition step by step in the Nim programming language
You may also check:How to resolve the algorithm Problem of Apollonius step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Copy a string step by step in the Harbour programming language