hp41programs

square-roots&powers

Square-Roots and Integer Powers for the HP-41


Overview
 

 1°)  ( a + b sqrt(n) ) k
 2°)  ( a + b sqrt(m) + c sqrt(n) ) k
 

1°)  ( a + b sqrt(n) ) k
 

-Sometimes, we have to find the integers  x and y  so that  x + y Ö n  =  ( a + b Ö n ) k    where  a , b , n , k  are integers  and  k > 1   ( Ö  =  square-root )
-The routine "POW" solves this problem.
 

Data Registers:    R00 = k , k-1 , .... , 0

                                R01 = a           R03 = n
                                R02 = b           R04 , R05: temp
Flags: /
Subroutines: /
 
 

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

 
    ( 41 bytes / SIZE 006 )
 
 

      STACK        INPUTS      OUTPUTS
           T             a             /
           Z             b             /
           Y             n             y
           X             k             x

  with  a , b , n , k  integers  &  k > 1    ( a + b Ö n ) k  =   x + y Ö n              ( Ö  =  square-root )

Examples:

   3   ENTER^
  -4  ENTER^
   5   ENTER^
   6   XEQ "POW"  >>>>   x = 1473929   ( in 3 seconds )
                              RDN    y =  -639432   thus,   ( 3 - 4 Ö 5 ) 6  =  1473929 - 639432 Ö 5         ( Ö  =  square-root )

-This routine also works if n < 0 , so it can be applied to complex numbers, for instance:

   2   ENTER^
   3   ENTER^
  -1  ENTER^
   7     R/S       >>>>   6554
                       RDN   4449     whence   ( 2 + 3 i ) 7  = 6554 + 4449 i

-There will be no roudoff error, provided all the arguments involved in the calculations are smaller than 1 + 1010 in magnitude.
 

2°)  ( a + b sqrt(m) + c sqrt(n) ) k
 

-The previous exercise may be generalized to find  x , y , z , t  so that  x + y Ö m  + z Ö n + t Ö (m.n)  =  ( a + b Ö m + c Ö n ) k
 
 

Data Registers:           •  R00 = k      ( R00 = 0 at the end )                 ( Registers R00 thru R05 are to be initialized before executing "POW2" )

                                      •  R01 = a     •  R04 = m
                                      •  R02 = b     •  R05 = n        R06 thru R10: temp
                                      •  R03 = c
Flags: /
Subroutines: /
 
 

01  LBL "POW2"
02  RCL 01
03  STO 06
04  RCL 02
05  STO 07
06  RCL 03
07  STO 08
08  CLX
09  STO 09
10  DSE 00
11  LBL 01
12  RCL 01
13  RCL 06
14  *
15  RCL 02
16  RCL 07
17  *
18  RCL 04        
19  *
20  +
21  RCL 03
22  RCL 08
23  *
24  RCL 05
25  *
26  +
27  STO 10
28  RCL 01
29  RCL 07
30  *
31  RCL 02
32  RCL 06
33  *
34  +
35  RCL 03        
36  RCL 09
37  *
38  RCL 05
39  *
40  +
41  X<> 07
42  RCL 03
43  *
44  RCL 01
45  RCL 09
46  *
47  +
48  RCL 02
49  RCL 08        
50  *
51  +
52  ENTER^
53  X<> 09
54  RCL 02
55  *
56  RCL 04
57  *
58  RCL 01
59  RCL 08
60  *
61  +
62  RCL 03
63  RCL 06        
64  *
65  +
66  STO 08
67  RCL 07
68  RCL 10
69  STO 06
70  DSE 00
71  GTO 01
72  END

 
   ( 86 bytes / SIZE 011 )
 
 

      STACK        INPUTS      OUTPUTS
           T             /             t
           Z             /             z
           Y             /             y
           X             /             x

  with    k > 1  and   ( a + b Ö m + c Ö n ) k   =   x + y Ö m  + z Ö n + t Ö (m.n)             ( Ö  =  square-root )

Example:

     8  STO 00

     2   STO 01      5   STO 04
     3   STO 02      7   STO 05
    -4   STO 03

  XEQ "POW2"   >>>>    5542917857 = R06        ( execution time = 13 seconds )
                            RDN    1810176816 = R07
                            RDN   -1530146752 = R08
                            RDN     -936804192 = R09

  whence  ( 2 + 3 Ö 5 - 4 Ö 7 ) 8  =  5542917857 + 1810176816 Ö 5 - 1530146752 Ö 7 - 936804192 Ö 35

Notes:

-Registers R01 thru R05 are unchanged, but when the program stops,  R00 = 0
-So, if you want to calculate another power of the same argument, store the exponent into R00 and R/S. For example:

   3  STO 00   R/S  gives  ( 2 + 3 Ö 5 - 4 Ö 7 ) 3  =  950 + 1179 Ö 5 - 1036 Ö 7 - 144 Ö 35   ( in 4 seconds )

-Like "POW", "POW2" produces  no roudoff error, provided all the integers involved in the calculations are smaller than 1 + 1010 in magnitude.