hp41programs

Heron

Heron's Formula and its Generalizations for the HP-41


Overview
 

 1°)  Area of a Triangle

  a)   Heron's Formula & Another one
  b)   M-Code Routines ( Heron & Brahmagupta Formulae )
  c)   Heronian Triangles

 2°)  Volume of a Tetrahedron

  a)   Francesca's Formula & Another one
  b)   2 M-Code Routines
  c)   6 Edges-lengths ---> 30 Volumes ?

 3°)  Hypervolume of a Simplex
 

Latest Updates:    programs in §1°)a) & 2°)a)

-In all these programs - except §1°-c) - we assume that all the edge lengths are known.
 

1°)  Area of a Triangle
 

     a)  Heron's Formula & Another one
 

-Let a triangle ABC with 3 known sides  a , b , c

-Heron's formula is     Area = [ p(p-a)(p-b)(p-c) ]1/2      where  p = (a+b+c)/2 = semiperimeter
 

Data Registers: /
Flags: /
Subroutines: /

-Lines 08-09-10-11 may be replaced by the M-Code function  X/2
 
 

 01 LBL "HERON"
 02 ENTER
 03 R^
 04 ST+ Z
 05 R^
 06 ST+ T
 07 R^
 08 SIGN
 09 ST+ X
 10 ST/ L
 11 X<> L
 12 ST- Y
 13 ST- Z
 14 ST- T
 15 *
 16 *
 17 *
 18 CHS
 19 SQRT
 20 END

 
 ( 38 bytes / SIZE 000 )
 
 

      STACK        INPUTS      OUTPUTS
           Z             a             /
           Y             b             /
           X             c         Area

 
Example:      a = 2    b = 3    c = 4

   2   ENTER^
   3   ENTER^
   4   XEQ "HERON"  >>>>   Area = 2.904737510

Note:

-The formula may be re-written in a more stable one, that is given in reference [2]
-There will be less roundoff-errors in "difficult cases"
 
 

 01  LBL "HERON"
 02  STO T
 03  X<>Y
 04  STO M
 05  ST+ T
 06  X<> Z
 07  ST+ Z
 08  ST+ T
 09  ST- M
 10  RDN
 11  ST- Y
 12  ENTER^
 13  X<> M
 14  ST- Y
 15  ST+ M
 16  CLX
 17  X<> M
 18  *
 19  *
 20  *
 21  SQRT
 22  4
 23  /
 24  END

 
 ( 46 bytes / SIZE 000 )
 
 

      STACK        INPUTS      OUTPUTS
           Z             a             /
           Y             b             /
           X             c         Area

 
Example:      a = 2    b = 3    c = 4

   2   ENTER^
   3   ENTER^
   4   XEQ "HERON"  >>>>   Area = 2.904737510 


Note:

-The formula may also be re-written to have a smaller program:

    4 Area2 = a2 c2 - ( a2 + c2 - b2 )2 / 4


 01 LBL "ATR"
 02 X^2
 03 ENTER
 04 R^
 05 X^2
 06 ST* Z
 07 +
 08 R^
 09 X^2
 10 -
 11 2
 12 /
 13 X^2
 14 -
 15 SQRT
 16 2
 17 /
 18 END

 
 ( 27 bytes / SIZE 000 )
 
 

           STACK           INPUTS         OUTPUTS
               Z                a               b
               Y                b               b
               X                c           Area

 
Example:      a = 2    b = 3    c = 4

   2   ENTER^
   3   ENTER^
   4   XEQ "ATR"  >>>>   Area = 2.904737510 


     b)  M-Code Routines ( Heron & Brahmagupta Formulae )

 

-"HERON" and "BRHM" use the Héron's & Brahmagupta's formulae.
-They are so similar that it is preferable to combine them.

-There is no check for alpha data and synthetic register Q is used.
-13-digit routines also give a better precision...
 

08E  "N"
00F  "O"
012  "R"
005  "E"
008  "H"
104   CLRF 8
033   JNC+06
08D  "M"
008   "H"
012   "R"
002   "B"
108   SETF 8
2A0  SETDEC
0F8  C=X
128   L=C
2BE  C=-C
10E  A=C ALL
0B8  C=Y
01D  C=
060   A+C
078   C=Z
025   C=
060   AB+C
04E   C=0 ALL
10C  ?FSET 8
01B   JNC+03
270   RAMSLCT
038   READATA
0E8   X=C
025   C=
060   AB+C
089   AB
064   STO Q+
0B8  C=Y
2BE  C=-C
10E   A=C ALL
138   C=L
01D  C=
060   A+C
078   C=Z
025   C=
060   AB+C
0F8   C=X
025   C=
060   AB+C
0D1  RCL
064   Q+
149   C=
060   AB*CM
089   AB
064   STO Q+
078   C=Z
2BE  C=-C
10E   A=C ALL
138   C=L
01D  C=
060   A+C
0F8   C=X
025   C=
060   AB+C
0B8  C=Y
025   C=
060   AB+C
0D1  RCL
064   Q+
149   C=
060   AB*CM
089   AB
064   STO Q+
0F8   C=X
2BE   C=-C
10E   A=C ALL
138   C=L
01D  C=
060   A+C
0B8  C=Y
025   C=
060   AB+C
078   C=Z
025   C=
060   AB+C
0D1  RCL
064   Q+
149   C=
060   AB*CM
04E   C
35C
050   =
190
226   16
269   C=
060   AB/C
305   C=
060   sqrt(AB)
0E8   X=C
3E0   RTN

( 96 words )
 
 

      STACK        INPUTS      OUTPUTS
           T             T             T
           Z             a             a
           Y             b             b
           X             c         Area
           L             /             c

 
Example1:      a = 2    b = 3    c = 4

   2   ENTER^
   3   ENTER^
   4   XEQ "HERON"  >>>>   Area = 2.904737510

Example2:     a = 4 , b = 5 , c = 6 , d = 7

   4  ENTER^
   5  ENTER^
   6  ENTER^
   7  XEQ "BRHM"  >>>>  Area = 28.98275349
 

     c)  Heronian Triangles
 

-The followig routine takes 3 integers in the stack and returns the integer edges lengths a , b , c  and area  A  of a Heronian triangle ( cf reference [3] ).
 

  Formula:

-With 3 integers  m , n , k  such that m.n > k2

   a = n ( m2 + k2 )
   b = ( m + n ) ( m.n - k2 )           Area = A = k m n ( m + n ) ( m.n - k2 )
   c = m ( n2 + k2 )
 

Data Registers:  R00 thru R03: temp
Flags: /
Subroutines: /
 
 

 01  LBL "HETR"
 02  STO 00 
 03  X^2
 04  STO 03       
 05  X<>Y
 06  STO 02
 07  X^2
 08  +
 09  X<>Y
 10  STO 01       
 11  ST* Y
 12  RCL 02
 13  ST+ Y
 14  RCL 01       
 15  *
 16  ST* 00
 17  RCL 03
 18  -
 19  *
 20  ST* 00
 21  RCL 01       
 22  X^2
 23  RCL 03
 24  +
 25  RCL 02
 26  *
 27  RCL 00       
 28  RDN
 29  END

 
    ( 42 bytes / SIZE 000 )
 
 

      STACK        INPUTS      OUTPUTS
           T             /          Area
           Z             m             c
           Y             n             b
           X             k             a

    With  m.n > k2

Example:      m = 67    n = 41    k = 13

   67   ENTER^
   41   ENTER^
   13   XEQ "HETR"   >>>>   a =  190978
                                  RDN    b =  278424
                                  RDN    c =  123950
                                  RDN   A =  9942799464

Note:

-"HETR" does not check that m.n > k2 , so it may return ( meaningless ) negative results.
-Add for instance  X<0?  SF 99  after line 18.
 

2°)  Volume of a Tetrahedron
 

     a)  Francesca's Formula & Another one
 

-The volume of a tetrahedron may be calculated by the formula:

   V = [ 4 a2 b2 c2 - a2 ( b2 + c2 - d2 )2 - b2 ( a2 + c2 - e2 )2 - c2 ( a2 + b2 - f2 )2 + ( b2 + c2 - d2 )( a2 + c2 - e2 )( a2 + b2 - f2 ) ]1/2 / 12

 • • •  provided  the edges a , b , c intersect at the same vertex and  the edges d , e , f  are respectively opposite to the edges a , b , c

-So, a and d ( respectively b and e , c and f ) must be non-coplanar.
 
 
 


                                         *
                                     *      *
                                  *             *
                               *         d *     *
                        c  *                           * e
                         *                                 *
                      *                         *              *
                   *               b *               *          *
                *            *                              f *      *
             *      *                                                *   *
      V *   *   *   *   *   *   *   *   *   *   *   *   *   *   *
                                         a

  

Data Registers:              R00: temp                                              ( Registers R01 thru R06 are to be initialized before executing "THV" )

                                      •  R01 = a       •  R04 = d
                                      •  R02 = b       •  R05 = e
                                      •  R03 = c       •  R06 =  f
Flags: /
Subroutines: /
 
 

 01 LBL "THV"
 02 RCL 03
 03 X^2
 04 4
 05 RCL 02          
 06 X^2
 07 STO 00
 08 ST* Y
 09 RCL 04
 10 X^2
 11 -
 12 R^
 13 ST* Z
 14 +
 15 STO Z
 16 X^2
 17 -
 18 RCL 01          
 19 X^2
 20 ST+ 00
 21 ST* Y
 22 R^
 23 +
 24 RCL 05          
 25 X^2
 26 -
 27 ST* Z
 28 RCL 02
 29 *
 30 X^2
 31 -
 32 RCL 00          
 33 RCL 06
 34 X^2
 35 -
 36 ST* Z
 37 RCL 03          
 38 *
 39 X^2
 40 -
 41 +
 42 SQRT
 43 12
 44 /
 45 END

 
    ( 61 bytes / SIZE 007 )


-Another program ( 60 bytes ) with the same formula:

Data Registers:              R00 & R07 temp                                           ( Registers R01 thru R06 are to be initialized before executing "THV" )

                                      •  R01 = a       •  R04 = d
                                      •  R02 = b       •  R05 = e
                                      •  R03 = c       •  R06 =  f
Flags: /
Subroutines: /
 
 

 01 LBL "THV"
 02 RCL 02
 03 X^2
 04 STO 00          
 05 RCL 03
 06 X^2
 07 STO 07
 08 +
 09 RCL 04
 10 X^2
 11 -
 12 ENTER
 13 X^2
 14 RCL 00          
 15 RCL 07
 16 *
 17 4
 18 *
 19 -
 20 RCL 01
 21 X^2
 22 ST+ 00
 23 ST* Y
 24 RCL 07          
 25 +
 26 RCL 05
 27 X^2
 28 -
 29 ST* Z
 30 RCL 02          
 31 *
 32 X^2
 33 +
 34 RCL 00
 35 RCL 06
 36 X^2
 37 -
 38 ST* Z
 39 RCL 03          
 40 *
 41 X^2
 42 +
 43 -
 44 SQRT
 45 12
 46 /
 47 END

 
    ( 60 bytes / SIZE 008 )


      STACK        INPUT       OUTPUT
           X             /        Volume

 
Example1:    a = 3   b = 5   c = 7   d = 6   e = 8   f = 4

-Store these 6 numbers into  R01 thru R06

  XEQ "THV"  >>>>   V = 8.426149775                        ---Execution time = 1.6s---

-The exact value is sqrt(71)

Example2:    a = 120   b = 160   c = 153   d = 25   e = 39   f = 56

-Store these 6 numbers into  R01 thru R06

  XEQ "THV"  >>>>   V = 8063.997493   whereas the exact result is 8064


-But if we store   39  56  120  160  153  25           into       R01  R02  R03  R04  R05  R06

  XEQ "THV"  >>>>   V = 8063.999990   much better precision !

Notes:

-The second tetrahedron is a heronian tetrahedron: the edges lengths, the faces areas & the volume are all integers.

-In difficult cases like example2, this formula may produce large roundoff-errors
-The method described in reference [2] is employed hereunder
 

 Data Registers:              R00-R07-R11: temp                                  ( Registers R01 thru R06 are to be initialized before executing "THV" )

                                      •  R01 = a       •  R04 = d
                                      •  R02 = b       •  R05 = e
                                      •  R03 = c       •  R06 =  f
Flags: /
Subroutines: /
 
 

 01 LBL "THV"
 02 RCL 02
 03 RCL 03
 04 +
 05 STO 00
 06 RCL 04
 07 ST- 00
 08 +
 09 ST* 00
 10 RCL 01
 11 RCL 03
 12 +
 13 STO 07          
 14 RCL 05
 15 ST- 07
 16 +
 17 ST* 07
 18 RCL 01
 19 RCL 02
 20 +
 21 STO 08
 22 RCL 06
 23 ST- 08
 24 +
 25 ST* 08
 26 RCL 04
 27 STO 09
 28 RCL 02
 29 RCL 03
 30 -
 31 ST- 09
 32 +
 33 ST* 09
 34 RCL 05          
 35 STO 10
 36 RCL 01
 37 RCL 03
 38 -
 39 ST- 10
 40 +
 41 ST* 10
 42 RCL 06
 43 RCL 06
 44 RCL 01
 45 RCL 02
 46 -
 47 ST- Z
 48 +
 49 *
 50 STO 11
 51 RCL 10
 52 RCL 09
 53 *
 54 *
 55 SQRT
 56 RCL 09          
 57 RCL 08
 58 RCL 07
 59 ST* 11
 60 *
 61 *
 62 SQRT
 63 RCL 08
 64 RCL 00
 65 ST* 11
 66 *
 67 RCL 10
 68 *
 69 SQRT
 70 STO 09
 71 R^
 72 ST- 09
 73 +
 74 RCL X
 75 RCL 11          
 76 SQRT
 77 STO 11
 78 R^
 79 ST+ 11
 80 -
 81 ST- Z
 82 +
 83 *
 84 RCL 11
 85 RCL 11
 86 RCL 09
 87 ST- Z
 88 +
 89 *
 90 *
 91 SQRT
 92 RCL 01
 93 RCL 02          
 94 RCL 03
 95 *
 96 *
 97 192
 98 *
 99 /
100 END

 
    ( 128 bytes / SIZE 012 )
 
 

            STACK             INPUT           OUTPUT
                X                 /            Volume

 
Example:    a = 120   b = 160   c = 153   d = 25   e = 39   f = 56

-Store these 6 numbers into  R01 thru R06

  XEQ "THV"  >>>>   V = 8063.999986                                 ---Execution time = 3.4s---

Notes:

-It's not yet the exact result ( 8064 ), but the roundoff-errors are significantly smaller.

-We can also compute V  with   V = (1/3) Area x height


Data Registers:              R00-R07-R08-R09: temp                      ( Registers R01 thru R06 are to be initialized before executing "THV" )

                                      •  R01 = a       •  R04 = d
                                      •  R02 = b       •  R05 = e
                                      •  R03 = c       •  R06 =  f
Flags: /
Subroutines: /
 
 

 01 LBL "THV"
 02 RCL 05
 03 X^2
 04 RCL 04          
 05 X^2
 06 -
 07 STO Y
 08 RCL 06
 09 X^2
 10 STO 08
 11 ST+ Z
 12 -
 13 STO 00
 14 RCL 02
 15 X^2
 16 RCL 01          
 17 X^2
 18 STO 09
 19 -
 20 ST- 08
 21 ST+ X
 22 +
 23 *
 24 RCL 09
 25 RCL 03
 26 X^2
 27 -
 28 RCL 06          
 29 ST+ X
 30 STO 07
 31 ST/ 08
 32 X^2
 33 *
 34 +
 35 RCL 04
 36 RCL 07
 37 *
 38 X^2
 39 RCL 00          
 40 X^2
 41 -
 42 STO Z
 43 SQRT
 44 ST/ Y
 45 +
 46 RCL 07
 47 ST+ X
 48 /
 49 X^2
 50 CHS
 51 RCL 09          
 52 +
 53 RCL 08
 54 X^2
 55 -
 56 *
 57 SQRT
 58 12
 59 /
 60 END

 
    ( 78 bytes / SIZE 010 )
 
 

            STACK              INPUT           OUTPUT
                X                  /            Volume

 
Example:    a = 120   b = 160   c = 153   d = 25   e = 39   f = 56

-Store these 6 numbers into  R01 thru R06

  XEQ "THV"  >>>>   V = 8064     exact result !


-Another program with the formula:

   144 V2 = a2 b2 ( d2 + e2 - f2 ) + a2 c2 ( d2 - e2 + f2 ) + b2 c2 ( -d2 + e2 + f2 ) + a2 d2 ( -d2 + e2 + f2 - a2 ) + b2 e2 ( d2 - e2 + f2 - b2 ) + c2 f2 ( d2 + e2 - f2 - c2 ) - d2 e2 f2
 

Data Registers:              R00-R07-R08-R09-R10-R11-R12-R13-R14: temp                      ( Registers R01 thru R06 are to be initialized before executing "THV" )

                                      •  R01 = a       •  R04 = d
                                      •  R02 = b       •  R05 = e
                                      •  R03 = c       •  R06 =  f
Flags: /
Subroutines: /
 
 

 01 LBL "THV"
 02 RCL 04
 03 X^2
 04 STO 09
 05 STO 12          
 06 RCL 06
 07 X^2
 08 STO 11
 09 -
 10 RCL 05
 11 X^2
 12 STO 10
 13 ST- 12
 14 +
 15 STO 14
 16 RCL 02
 17 X^2
 18 STO 07          
 19 *
 20 RCL 12
 21 RCL 11
 22 ST- 12
 23 +
 24 STO 13
 25 RCL 03
 26 X^2
 27 STO 08
 28 ST- 14
 29 *
 30 +
 31 RCL 12          
 32 RCL 01
 33 X^2
 34 STO 00
 35 +
 36 RCL 09
 37 *
 38 -
 39 RCL 00
 40 *
 41 RCL 13
 42 RCL 07          
 43 ST* 12
 44 ST- Y
 45 *
 46 RCL 09
 47 RCL 11
 48 ST* 14
 49 *
 50 -
 51 RCL 10
 52 *
 53 +
 54 RCL 14
 55 RCL 12          
 56 -
 57 RCL 08
 58 *
 59 +
 60 SQRT
 61 12
 62 /
 63 END

 
    ( 78 bytes / SIZE 015 )
 
 

            STACK              INPUT           OUTPUT
                X                  /            Volume

 
Example1:       a = 3   b = 5   c = 7   d = 6   e = 8   f = 4

-Store these 6 numbers into  R01 thru R06

  XEQ "THV"  >>>>   V = 8.426149775                        ---Execution time = 2.1s---

Example2:       a = 120   b = 160   c = 153   d = 25   e = 39   f = 56

-Store these 6 numbers into  R01 thru R06

  XEQ "THV"  >>>>   V = 8063.999990
 

     b)  2 M-Code Routines
 

-This program employs Francesca's formula with 13-digit routines
-The alpha "register" and registers Z , Y , L are cleared.

-The existence of data register R06 is checked but there is no check for alpha data.
-Register R00 is unused.
 

Data Registers:              R00: unused                             ( Registers R01 thru R06 are to be initialized before executing "THV" )

                                      •  R01 = a       •  R04 = d
                                      •  R02 = b       •  R05 = e
                                      •  R03 = c       •  R06 =  f
Flags: /
Subroutines: /
 

096  "V"
008  "H"
014  "T"
378  C=c
03C  RCR 3
226   C=C+1 S&X
106   A=C S&X
1BC  RCR 11
0A6  A<>C S&X
226  C=C+1 S&X
226  C=C+1 S&X
226  C=C+1 S&X
106   A=C S&X
1BC  RCR 11
0A6  A<>C S&X
226  C=C+1 S&X
226  C=C+1 S&X
070  N=C ALL
106  A=C S&X
130  LDI S&X
200  200h                         200h  is the correct value if you have an HP-41 CX, CV or C with a Quad memory module or 4 memory modules.
306  ?A<C S&X              If you have an HP-41C without any memory module, replace 200h by 100h
381  ?NCGO
00A  02E0                        if register R06  does not exist, the routine stops after displaying "NONEXISTENT"
0B0  C=N ALL
17C  RCR 6
270   RAMSLCT
038   READATA
10E  A=C ALL
0B0  C=N ALL
17C  RCR 6
226  C=C+1 S&X
270   RAMSLCT
038   READATA
2A0  SETDEC
135   C=
060   A*C
0B0   C=N ALL
03C   RCR 3
260   SETHEX
266  C=C-1 S&X
270   RAMSLCT
038   READATA
2A0  SETDEC
13D  C=
060   AB*C
04E  C
35C  =
090   2
13D  C=
060   AB*C
0AE  A<>C ALL
10E  A=C ALL
158   M=C ALL
0CE  C=B ALL
149   C=
060   AB*CM
081  0 ramslct
064  AB STO Q+
0B0  C=N ALL
17C  RCR 6
260  SETHEX
226  C=C+1 S&X
270   RAMSLCT
038   READATA
10E  A=C ALL
2A0  SETDEC
135   C=
060   A*C
046   C=0 S&X
270   RAMSLCT
0AE  A<>C ALL
128   L=C
0CE  C=B ALL
0E8  X=C
0B0  C=N ALL
03C  RCR 3
260  SETHEX
266  C=C-1 S&X
270   RAMSLCT
038   READATA
10E  A=C ALL
2A0  SETDEC
135  C=
060  A*C
046  C=0 S&X
270   RAMSLCT
138  C=L
158  M=C ALL
0F8  C=X
031  C=
060  AB+CM
0AE  A<>C ALL
128   L=C
0CE  C=B ALL
0E8  X=C
0B0  C=N ALL
03C  RCR 3
270   RAMSLCT
038   READATA
10E  A=C ALL
2BE  C=-C
135  C=
061  A*C
046  C=0 S&X
270   RAMSLCT
138  C=L
158  M=C ALL
0F8  C=X
031  C=
060  AB+CM
0AE  A<>C ALL
10E  A=C ALL
1A8  N=C
0CE  C=B ALL
168   M=C
0B0  C=N ALL
17C  RCR 6
270   RAMSLCT
038   READATA
13D  C=
060   AB*C
0AE  A<>C ALL
10E   A=C ALL
2BE  C=-C
158  M=C ALL
0CE  C=B ALL
149   C=
060   AB*CM
0C9  0 ramslct
064   RCL Q+
031  C=
060  AB+CM
089  AB
064  STO Q+
0B0  C=N ALL
17C  RCR 6
270   RAMSLCT
038   READATA
10E  A=C ALL
135   C=
060   A*C
046   C=0 S&X
270   RAMSLCT
0AE  A<>C ALL
128   L=C
0CE  C=B ALL
0E8  X=C
0B0  C=N ALL
03C  RCR 3
260  SETHEX
266  C=C-1 S&X
270   RAMSLCT
038   READATA
10E  A=C ALL
2A0  SETDEC
135  C=
060  A*C
046  C=0 S&X
270   RAMSLCT
138  C=L
158  M=C ALL
0F8  C=X
031  C=
060  AB+CM
0AE  A<>C ALL
128   L=C
0CE  C=B ALL
0E8  X=C
0B0  C=N ALL
260  SETHEX
266  C=C-1 S&X
270   RAMSLCT
038   READATA
10E  A=C ALL
2A0  SETDEC
2BE  C=-C
135  C=
061  A*C
046  C=0 S&X
270   RAMSLCT
138  C=L
158  M=C ALL
0F8  C=X
031  C=
060  AB+CM
0AE  A<>C ALL
10E  A=C ALL
228   P=C
0CE  C=B ALL
1E8   O=C
0B0  C=N ALL
17C  RCR 6
260  SETHEX
226  C=C+1 S&X
270   RAMSLCT
038   READATA
2A0  SETDEC
13D  C=
060   AB*C
0AE  A<>C ALL
10E   A=C ALL
2BE  C=-C
158  M=C ALL
0CE  C=B ALL
149   C=
060   AB*CM
0C9  0 ramslct
064   RCL Q+
031  C=
060  AB+CM
089  AB
064  STO Q+
0B0  C=N ALL
17C  RCR 6
270   RAMSLCT
038   READATA
10E  A=C ALL
135   C=
060   A*C
046   C=0 S&X
270   RAMSLCT
0AE  A<>C ALL
128   L=C
0CE  C=B ALL
0E8  X=C
0B0  C=N ALL
17C  RCR 6
260  SETHEX
226  C=C+1 S&X
270   RAMSLCT
038   READATA
10E  A=C ALL
2A0  SETDEC
135  C=
060  A*C
046  C=0 S&X
270   RAMSLCT
138  C=L
158  M=C ALL
0F8  C=X
031  C=
060  AB+CM
0AE  A<>C ALL
128   L=C
0CE  C=B ALL
0E8  X=C
0B0  C=N ALL
270   RAMSLCT
038   READATA
10E  A=C ALL
2BE  C=-C
135  C=
061  A*C
046  C=0 S&X
270   RAMSLCT
138  C=L
158  M=C ALL
0F8  C=X
031  C=
060  AB+CM
0AE  A<>C ALL
10E  A=C ALL
068   Z=C
0CE  C=B ALL
0A8  Y=C
0B0  C=N ALL
03C  RCR 3
260   SETHEX
266  C=C-1 S&X
270   RAMSLCT
038   READATA
2A0  SETDEC
13D  C=
060   AB*C
0AE  A<>C ALL
10E   A=C ALL
2BE  C=-C
158  M=C ALL
0CE  C=B ALL
149   C=
060   AB*CM
0C9  0 ramslct
064   RCL Q+
031  C=
060  AB+CM
089  AB
064  STO Q+
1B8  C=N
10E  A=C ALL
178  C=M
0EE  B<>C ALL
238  C=P
158  M=C ALL
1F8  C=O
149   C=
060   AB*CM
078  C=Z
158  M=C ALL
0B8  C=Y
149   C=
060   AB*CM
0D1  RCL
064  Q+
031  C=
060  AB+CM
04E  C=0 ALL
128  L=C
0A8  Y=C
068   Z=C
35C  C
050
110   =
110
226
226   144
269  C=
060  AB/C
305  C=
060  sqrt(AB)
0E8  X=C
345  ?NCGO
042  CLA

( 323 words )
 
 

      STACK        INPUTS     OUTPUTS
           T            T            T
           Z            /            0
           Y            /            0
           X            /        Volume
           L            /            0

        With   R01 = a  , ................. , R06 = f

Example1:    a = 3   b = 5   c = 7   d = 6   e = 8   f = 4

-Store these 6 numbers into  R01 thru R06

  XEQ "THV"  >>>>   V = 8.426149773

-The exact value is sqrt(71), so all the digits are correct.

Example2:    a = 120   b = 160   c = 153   d = 25   e = 39   f = 56

-Store these 6 numbers into  R01 thru R06

  XEQ "THV"  >>>>   V = 8063.999998
 

Notes:

-Register T is saved.
-With the second ( difficult ) example, even 13-digit routines don't give the exact result !

-Three steps are very similar in the calculations, so this routine may be simplified as follows ( 187 words instead of 323 ):

-Change the three ?NCXQ written in red according to the place of the subroutine in your own ROM
 

096  "V"                              @E143 in my ROM
008  "H"
014  "T"
378  C=c
03C  RCR 3
226   C=C+1 S&X
106   A=C S&X
1BC  RCR 11
0A6  A<>C S&X
226  C=C+1 S&X
106   A=C S&X
1BC  RCR 11
0A6  A<>C S&X
226  C=C+1 S&X
106   A=C S&X
1BC  RCR 11
0A6  A<>C S&X
226  C=C+1 S&X
070  N=C ALL                  CPU register N now contains the addresses of R01.R02.R03.R04
106  A=C S&X
130  LDI S&X
1FE  1FEh                         1FEh  is the correct value if you have an HP-41 CX, CV or C with a Quad memory module or 4 memory modules.
306  ?A<C S&X
381  ?NCGO
00A  02E0                         if register R06  does not exist, the routine stops after displaying "NONEXISTENT"
0B0  C=N ALL
27C  RCR 9
270   RAMSLCT
038   READATA
10E  A=C ALL
0B0  C=N ALL
17C  RCR 6
270   RAMSLCT
038   READATA
2A0  SETDEC
135   C=
060   A*C
0B0   C=N ALL
03C   RCR 3
270   RAMSLCT
038   READATA
13D  C=
060   AB*C
04E  C
35C  =
090   2
13D  C=
060   AB*C
0AE  A<>C ALL
10E  A=C ALL
158   M=C ALL
0CE  C=B ALL
149   C=
060   AB*CM
081  0 ramslct
064  AB STO Q+
04E  C=0 ALL
228   P=C
35C  C=
050   1
1E8  O=C
2A5  ?NCXQ        ?NCXQ                     Change these 2 words according to the address of the subroutine
384   E1A9             subroutine                  in your own ROM
260   SETHEX
0B0  C=N ALL
226   C=C+1 S&X
17C  RCR 6
266   C=C-1 S&X
23A  C=C+1 M
13C  RCR 8
070  N=C ALL
2A5  ?NCXQ        ?NCXQ                     Change these 2 words according to the address of the subroutine
384   E1A9             subroutine                  in your own ROM
260  SETHEX
0B0  C=N ALL
226  C=C+1 S&X
27A  C=C-1 M
17C  RCR 6
23A  C=C+1 M
13C  RCR 8
070  N=C ALL
2A5  ?NCXQ        ?NCXQ                     Change these 2 words according to the address of the subroutine
384   E1A9             subroutine                  in your own ROM
0D1  RCL
064  Q+
031  C=
060  AB+CM
04E  C=0 ALL
128  L=C
35C  C
050
110   =
110
226
226   144
269  C=
060  AB/C
305  C=
060  sqrt(AB)
0E8  X=C
345  ?NCGO
042  CLA
2A0  SETDEC          subroutine @E1A9 in my ROM
0B0  C=N ALL
17C  RCR 6
270   RAMSLCT
038   READATA
10E  A=C ALL
135   C=
060   A*C
046   C=0 S&X
270   RAMSLCT
0AE  A<>C ALL
1A8   N=C
0CE  C=B ALL
168  M=C
0B0  C=N ALL
270   RAMSLCT
038   READATA
10E  A=C ALL
2BE  C=-C
135  C=
061  A*C
046  C=0 S&X
270   RAMSLCT
1B8  C=N
158  M=C ALL
178  C=M
031  C=
060  AB+CM
0AE  A<>C ALL
1A8  N=C
0CE  C=B ALL
168  M=C
0B0  C=N ALL
03C  RCR 3
270   RAMSLCT
038   READATA
10E  A=C ALL
135  C=
060  A*C
046  C=0 S&X
270   RAMSLCT
1B8  C=N
158  M=C ALL
178  C=M
031  C=
060  AB+CM
0AE  A<>C ALL
10E  A=C ALL
0E8  X=C
0CE  C=B ALL
128   L=C
0B0  C=N ALL
27C  RCR 9
270   RAMSLCT
038   READATA
13D  C=
060   AB*C
0AE  A<>C ALL
10E   A=C ALL
2BE  C=-C
158  M=C ALL
0CE  C=B ALL
149   C=
060   AB*CM
0C9  0 ramslct
064   RCL Q+
031  C=
060  AB+CM
089  AB
064  STO Q+
238   C=P
10E  A=C ALL
1F8  C=O
0EE  B<>C ALL
0F8  C=X
158  M=C ALL
138  C=L
149  C=
060  AB*CM
0AE  A<>C ALL
10E  A=C ALL
228   P=C
0CE  C=B ALL
1E8  O=C
3E0  RTN                                    @E1FD in my ROM

( 187 words )
 
 

      STACK        INPUTS     OUTPUTS
           T            T            T
           Z            Z            Z
           Y            Y            Y
           X            /        Volume
           L            /            0

        With   R01 = a  , ................. , R06 = f

Notes:

-Same examples >>> same results.
-The alpha register is cleared but registers Y Z T are saved.
 

     c)  6 Edges Lenths ---> 30 Volumes ?
 

-Unlike the previous paragraphs, we assume that the sides d , e , f  intersect at the same vertex
  but  the sides d , e , f  are still respectively opposite to the sides a , b , c

-So, a and d ( respectively b and e , c and f ) must be non-coplanar.
 
 


                                         *
                                     *      *
                                  *             *
                               *         d *     *
                        c  *                           * b
                         *                                 *
                      *                        *              *
                   *               e *               *          *
                *            *                              f *      *
             *      *                                                 *  *
          *   *   *   *   *   *   *   *   *   *   *   *   *   *  *
                                         a

 
-Given 6 values for the edges lengths, there are at most 30 different volumes for 30 tetrahedrons.
-The 30 volumes are stored into registers R40 to R11 and the corresponding configurations are:
 

     R40 =  a b c d e f    ( initial configuration )                R30 =  a b d c f e                  R20 = a c d b e f
     R39 =  a b c d f e                                                    R29 =  a b d c e f                  R19 = a c d b f e
     R38 =  a b c e f d                                                    R28 = a b f c e d                   R18 = a c e b f d
     R37 =  a b c f e d                                                    R27 = a b f e c d                   R17 = a b e c f d
     R36 =  a b c f d e                                                    R26 = a b f e c d                   R16 = a b e f c d
     R35 =  a b c e d f                                                    R25 = a b f d e c                   R15 = a b e f d c
     R34 =  a b d e c f                                                    R24 = a b f d c e                   R14 = a b e d f c
     R33 =  a b d e f c                                                    R23 = a b f c d e                   R13 = a b e d c f
     R32 =  a b d f e c                                                    R22 = a c f b d e                   R12 = a b e c d f
     R31 =  a b d f c e                                                    R21 = a c f b e d                   R11 = a c e b d f
 

 Data Registers:              R00 & R07 to R10 : temp                 ( Registers R01 thru R06 are to be initialized before executing "30THV" )

                                      •  R01 = a       •  R04 = d
                                      •  R02 = b       •  R05 = e                       R11 thru R40:  the 30 volumes corresponding to the 30 configurations above.
                                      •  R03 = c       •  R06 =  f
Flags: /
Subroutines: /
 
 

  01  LBL "30THV"
  02  4
  03  STO 09 
  04  12
  05  STO 10         
  06  41
  07  STO 11
  08  XEQ 01
  09  RCL 05 
  10  X<> 06
  11  STO 05
  12  XEQ 01
  13  RCL 04
  14  X<> 06
  15  STO 04
  16  XEQ 01
  17  RCL 04
  18  X<> 05
  19  STO 04
  20  XEQ 01
  21  RCL 05
  22  X<> 06
  23  STO 05
  24  XEQ 01
  25  RCL 04
  26  X<> 06
  27  STO 04
  28  XEQ 01
  29  RCL 03
  30  X<> 05
  31  STO 03
  32  XEQ 01
  33  RCL 05 
  34  X<> 06
  35  STO 05         
  36  XEQ 01
  37  RCL 04
  38  X<> 05
  39  STO 04 
  40  XEQ 01
  41  RCL 05
  42  X<> 06
  43  STO 05
  44  XEQ 01
  45  RCL 04
  46  X<> 05
  47  STO 04
  48  XEQ 01
  49  RCL 05
  50  X<> 06
  51  STO 05
  52  XEQ 01
  53  RCL 03
  54  X<> 06
  55  STO 03
  56  XEQ 01
  57  RCL 04
  58  X<> 05
  59  STO 04
  60  XEQ 01
  61  RCL 05
  62  X<> 06
  63  STO 05 
  64  XEQ 01
  65  RCL 04         
  66  X<> 05
  67  STO 04
  68  XEQ 01
  69  RCL 05 
  70  X<> 06
  71  STO 05
  72  XEQ 01
  73  RCL 04
  74  X<> 05
  75  STO 04
  76  XEQ 01
  77  RCL 02
  78  X<> 04
  79  STO 02
  80  XEQ 01
  81  RCL 05
  82  X<> 06
  83  STO 05
  84  XEQ 01
  85  RCL 03
  86  X<> 06
  87  STO 03
  88  XEQ 01
  89  RCL 05
  90  X<> 06
  91  STO 05
  92  XEQ 01
  93  RCL 03 
  94  X<> 06
  95  STO 03         
  96  XEQ 01
  97  RCL 02
  98  X<> 04
  99  STO 02 
100  XEQ 01
101  RCL 04
102  X<> 05
103  STO 04
104  XEQ 01
105  RCL 05
106  X<> 06
107  STO 05
108  XEQ 01
109  RCL 04
110  X<> 05
111  STO 04
112  XEQ 01
113  RCL 05
114  X<> 06
115  STO 05
116  XEQ 01
117  RCL 04
118  X<> 05
119  STO 04
120  XEQ 01
121  RCL 02
122  X<> 04
123  STO 02 
124  LBL 01
125  RCL 09         
126  RCL 04
127  X^2
128  STO 07 
129  ST* Y
130  RCL 03
131  X^2
132  -
133  RCL 05
134  X^2
135  STO 08
136  ST* Z
137  +
138  STO Z
139  X^2
140  -
141  RCL 06
142  X^2
143  STO 00
144  ST* Y
145  RCL 01
146  X^2
147  -
148  RCL 08
149  +
150  ST* Z
151  X^2
152  RCL 07         
153  *
154  -
155  RCL 07 
156  RCL 02
157  X^2
158  -
159  RCL 00
160  +
161  ST* Z
162  X^2
163  RCL 08
164  *
165  -
166  +
167  SIGN
168  LASTX
169  ABS
170  SQRT
171  *
172  RCL 10
173  /
174  DSE 11
175  STO IND 11
176  END

 
    ( 283 bytes / SIZE 041 )
 
 

      STACK        INPUT       OUTPUT
           X             /       V30 = R11

 
Example:     a = 41   b = 42  c = 43  d = 44  e = 45  f = 46

   41  STO 01      44  STO 04
   42  STO 02      45  STO 05
   43  STO 03      46  STO 06

   XEQ "30THV"   >>>>>   V30 = 9549.651942 = R11                   ---Execution time = 65s---

-All the 30 volumes are

   R11 = 9549.651942     R16 = 9612.079808    R21 = 9572.860100    R26 = 9613.515867    R31 = 9613.268483    R36 = 9630.443600
   R12 = 9558.033600     R17 = 9599.960092    R22 = 9549.413017    R27 = 9592.100392    R32 = 9631.412675    R37 = 9634.972967
   R13 = 9560.585658     R18 = 9580.897258    R23 = 9557.078300    R28 = 9585.059600    R33 = 9626.096033    R38 = 9629.895233
   R14 = 9616.129400     R19 = 9581.612017    R24 = 9559.869292    R29 = 9587.918308    R34 = 9594.005417    R39 = 9621.832017
   R15 = 9625.694633     R20 = 9573.813792    R25 = 9609.267400    R30 = 9601.863525    R35 = 9619.220067    R40 = 9615.686350

Notes:

-The volume in R40 corresponds to the initial tetrahedron  41 - 42 - 43 - 44 - 45 - 46  ( a b c d e f )
-The volume in R11 corresponds to the tetrahedron  41 - 43 - 45 - 42 - 44 - 46  (  a c e b d f )

-Of course, all the configurations don't always lead to a real tetrahedron
-To avoid a data error if V2 < 0 , "30THV" returns in fact  sign(V2) x sqrt ( abs ( V2 ) )  Lines 167 to 173.
-So, the negative values must be read  sqrt(-1) | V |

-For instance, with  a = 6   b = 5  c = 2  d = 7  e = 8  f = 4  ,  R40 = 6 > 0  is a feasible tetrahedron
  but  R11 = -2 < 0 , R13 & R14 < 0 ... don't give real volumes.
-With these edges lengths, only 4 configurations lead to real volumes.

-The configurations "ACEBDF" .... could also be stored in registers to get more complete informations but it would cost many bytes...
-Another variant:

  Replace lines 174-175 by    X>0?   STOP    and delete lines 06-07

-Thus, the program will stop each time  V > 0  and you can recall R01 to R06 to get the corresponding config.

-If you prefer that the edges  a , b , c  intersect at the same vertex,  exchange R01<>R04 , R02<>R05 , R03<>R06  in the listing above.
 

3°)  Hypervolume of a Simplex
 

-We have to calculate a determinant of order ( n + 2 ) to get the (hyper-)volume of a n-simplex in a n-dimensional space.
-Let (n+1) points Pi  ( i = 1 , 2 , .... , n+1 )

    Let  B the matrix defined by  Bi,j = PiPj = distance between the 2 points
    and  C the matrix obtained if we add a top row  [ 0   1   1   .............  1 ]  and a left column [ 0   1   1   .............   1 ]T  to matrix B
 

-The volume V of the simplex is then:    V = [ (-1)n+1 2 -n  det C ]1/2 / n!
 

Data Registers:              The coefficients of the matrix C
                                         When the routine stops, R00 = det C and R01 = n

Flag:  F29
Subroutine:  "LS3"  or  "LS2"  or  "LS"  ( cf" Linear and non-linear systems for the HP-41" )
                                               or  "DET"  ( cf "Determinants for the HP-41" )

-The append character is denoted  ~
-Line 91 may be replaced by  XEQ "LCL"       ( cf "Miscellaneous Short Routines for the HP-41" )
-Line 100, n is saved in R01
 
 

  01  LBL "SIMV"
  02  STO 00
  03  STO 01
  04  5
  05  +
  06  STO 02 
  07  ST+ X
  08  4
  09  -
  10  STO 03        
  11  CLX
  12  FIX 0
  13  CF 29
  14  LBL 01
  15  RCL 00 
  16  STO 06
  17  RCL 02
  18  STO 04
  19  RCL 03
  20  STO 05
  21  R^
  22  1
  23  +
  24  ENTER^
  25  ISG X
  26  LBL 02
  27  " D"
  28  ARCL Y
  29  "~-" 
  30  ARCL X
  31  "~?" 
  32  PROMPT
  33  X^2
  34  STO IND 04
  35  STO IND 05
  36  CLX
  37  RCL 01        
  38  2
  39  +
  40  ST+ 05
  41  SIGN
  42  ST+ 04 
  43  +
  44  DSE 06
  45  GTO 02
  46  RCL 01
  47  3
  48  +
  49  ST+ 02
  50  ST+ 03
  51  R^
  52  DSE 00
  53  GTO 01
  54  FIX 9
  55  SF 29
  56  RCL 01        
  57  2
  58  +
  59  STO 00 
  60  SIGN
  61  LBL 03
  62  STO IND L
  63  DSE L
  64  GTO 03
  65  RCL 00
  66  X^2
  67  LASTX
  68  -
  69  1
  70  +
  71  RCL 00
  72   E5
  73  /
  74  +
  75  SIGN
  76  LBL 04
  77  STO IND L
  78  DSE L
  79  GTO 04
  80  RCL 00        
  81  1
  82  ST+ Y
  83  %
  84  RCL 00 
  85  X^2
  86  +
  87   E3
  88  /
  89  1
  90  +
  91  CLRGX
  92  RCL 00 
  93  ENTER^
  94  XEQ "LS3"
  95  CHS
  96  R^
  97  INT
  98  2
  99  -
100  STO 01        
101  LASTX
102  CHS
103  X<>Y
104  Y^X
105  /
106  SQRT
107  RCL 01 
108  FACT
109  /
110  END

 
   ( 157 bytes / SIZE n2+4 n + 5 )
 
 

      STACK        INPUT      OUTPUT
           X          n > 1        Volume

 
Example1:   The tetrahedron of paragraph 2-a)

   Dim E = 3  XEQ "SIMV"  >>>    "D1-2?"

                        3      R/S      >>>    "D1-3?"
                        5      R/S      >>>    "D1-4?"
                        7      R/S      >>>    "D2-3?"
                        4      R/S      >>>    "D2-4?"
                        8      R/S      >>>    "D3-4?"
                        6      R/S      >>>    Volume = 8.426149767                           ---Execution time = 44s---

-The program is of course much slower: we have to calculate a determinant of order 5  instead of a relatively simple formula...
 

Example2:     In a 4-dimensional space, the simplex defined by 5 points  A , B , C , D , E  such that:

                      AB = 4  AC = 6  AD = 7  AE = 5
                      BC = 8  BD = 9  BE = 6
                      CD = 7  CE = 9
                      DE =10

    4  XEQ "SIMV"    >>>   "D1-2?"

           AB = 4  R/S   >>>   "D1-3?"
           AC = 6  R/S   >>>   "D1-4?"
           AD = 7  R/S   >>>   "D1-5?"
           AE = 5  R/S   >>>    "D2-3?"
           BC = 8  R/S   >>>    "D2-4?"
           BD = 9  R/S   >>>    "D2-5?"
           BE = 6  R/S   >>>     "D3-4?"
          CD = 7  R/S   >>>     "D3-5?"
          CE = 9  R/S   >>>     "D4-5?"
          DE =10 R/S   >>>     Volume = 27.22208051                              ---Execution time = 65s---

Note:

-If the distances are contradictory i-e if such a simplex cannot exist, there will be a DATA ERROR message line106
 

References:

[1]   http://mathworld.wolfram.com/Cayley-MengerDeterminant.html
[2]   Prof. W. Kahan - "What has the Volume of a Tetrahedron to do with Computer Programming Languages ?"
[3]   http://mathworld.wolfram.com/HeronianTriangle.html