HP Forums
(50g) Root with Euklidian Algorithm - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Software Libraries (/forum-10.html)
+--- Forum: General Software Library (/forum-13.html)
+--- Thread: (50g) Root with Euklidian Algorithm (/thread-1147.html)

(50g) Root with Euklidian Algorithm - peacecalc - 04-22-2014 09:09 AM

Hello friends,

for educational purpose I used the hp 50g to approximate a root with a continued fraction.

The main task of such a program is 1) to calculate integer part of the root,
2) the reciprocal of the non-integer part
3) find out the integer part of the expression in 2)

2) and 3) is repeated as long as you whish.

I made three programs:

anrat: splits every fraction in three parts: integer part, enumarator and denominator.
anirr: with the help of "anrat" this program calculates one step 2) and 3)
rooeu: the main program acts with "anirr", it calculates 1) and the repeated times of 2) and 3), last task is to display the continued fraction in the depth you wished, display the reduced fraction and the numerical value of the continued fraction.



Input: fraction or integer

Output: integer (level 3)
denumerator (level 2)
denominator (level 1)


%%HP: T(3)A(R)F(,);
\<< DUP
  IF   TYPE 9, ==                 @@it's a fraction?
       IF   1 >                   @@when yes, it a improper fraction
            PROPFRAC OBJ\-> DROP2 @@when yes, then change fraction in integer (level 2) 
                                  @@and rest fraction (level 1)
            0 SWAP                @@when no improper fraction, set integer to zero 
       OBJ\-> DROP2               @@split fraction into enumerator and denominator
       0 0                        @@if no fraction: level 1 and 2 is set to zero


Input: root which has to be estimated (level 3)
integer part of the root (level 2)
the reciprocal value of (level 1)
irrational rest of the root
smaller 1

Output: root which has to be estimated (level 4)
integer part of the root (level 3)
the new reciprocal value of (level 2)
irrational rest of the root,
smaller 1
integer for continued fraction (level 1)


%%HP: T(3)A(R)F(,);
  \<< RVAL FDISTRIB OBJ\-> DROP2 SWAP @@split reciprocal in an irrational and a rational part
                                      @@swap is necessary because the rational part is on level 2
      ANRAT DUP                       @@duplicate the last output from anrat 
                                      @@the denominator of the fraction
      IF    0 \=/
           / 'R' STO                  @@that builds the fraction together
                                      @but only the proper part of it
           DROP2                      @@clean up the stack, if there is only
      END                             @@an integer

      'G' STO                         @@Store the integer part for the continued fraction
      VAL SWAP / EVAL                 @@Divide the search root value with the irrational part
      'N' STO GVAL N /                @@that is necessary because hp 50g reduce fractions
                                      @@automatically, with this denominator the integer part
                                      @@of the root is divided    
      ANRAT DUP                       @@this fraction is analysed in respect 
                                      @@to its integer part

      IF    0 \=/
            / 'R' STO+                @@add the proper fraction to the former fraction rest
           DROP2                      @@clean up the stack, if there is only
      END                             @@an integer 

      'G' STO+                        @@add the integer part for the continued fraction 
      R EVAL                          @@New remainder is analysized for integer part
      IF   0 \=/
           /                          @@if new remainder is a proper fraction then take it
                                      @@as a new rational rest for the next stage calculating
                                      @@the reciprocal remainder of the searched root                       
           DROP2 0              @@if there are three zeros add zero it to the integer part for 
                                      @@the continued fraction (it is not necessary) 

      'R' STO 
      'G' STO+ 
      VAL GVAL DUP2 - N / R + INV     @@Calculate the newreciprocal value of
      EVAL                            @@irrational rest of the root, smaller 1                     


Input: root to estimate (level 2)
depth of continued fraction (level 1)

Output: numerical value of continued fraction (level 1)


%%HP: T(3)A(R)F(,);
\<< \-> V ANZ
  \<< V \->NUM IP \->Q DUP 1, \->LIST \-> GZ ELST @@Calculate and 
                                                  @@load first integer value into result list
    \<< V GZ DUP2 - INV EVAL                      @@calculate first reciprocal value
                                                  @@of the non integer remainder of root
        1, ANZ                                    @@loop for calculating the integers for the
        START                                     @@integers used in the continued fraction
             ANIRR 'ELST' STO+

        3, DROPN                                  @@clean up the stack levels
        0                                         @@first value in continued fraction
        1, ANZ                                    @@loop for building up the 
        START                                     @@the continued fraction
             ELST HEAD + INV               @@generate next level of fraction
             ELST TAIL                            @@reduce list with numbers and store it
             'ELST' STO

        ELST HEAD +                               @@add last value without fraction  
        HALT                                      @@display continued fraction
        HALT                                      @@display reduced fraction 
        \->NUM                                    @@display numerical value                

and some display screen shots:

pic1: in- (level 4) and output from anrat
pic2, 3 in- and output from anirr (here root from 52)
pic4 input for rooeu (here root from 52 and iteration depth 6)
pic5 first output: continued fraction
pic6: second output reduced fraction
pic7: numerical value


RE: HP 50g Root with Euklidian Algorithm - peacecalc - 04-22-2014 09:10 AM

.last three pics.