SansTitre

# Real Numbers -> Integer Roots for the HP-41

Overview

1°)  Real -> Square-Roots ?
2°)  Real -> Cube Roots ?
3°)  Real -> Solution of a Cubic Equation ?

a)  X^3+p.X+q = 0
b)  X^3+p.X^2+q.X+r = 0

-The following routines try to express a real number with square roots or cube roots of integrers.

1°)  Real -> Square-Roots ?

"SR?" tries to find integers a , b , c such that a real number x = [ a +/- sqrt(b) ] / c  or  sqrt [ a +/- sqrt(b) ] / c

Data Registers:   R00 = x , R01 = a , R02 = c   R03-R04: temp

Flag:  F02   CF 02 <->  x = ( a +/- sqrt(b) ) / c
SF 02 <->  x = sqrt [ a +/- sqrt(b) ] / c

Subroutines: /

 01 LBL "SR?"  02 STO 00      03 CF 02  04 1  05 STO 04  06 LBL 01  07 RCL 04  08 STO 01  09 ENTER  10 SIGN  11 STO 02  12 +  13 STO 04  14 LBL 02  15 RCL 00  16 RCL 02  17 *  18 ENTER 19 X^2  20 STO 03  21 LASTX  22 RCL 01      23 ST+ 03  24 ST+ T  25 ST- Z  26 -  27 X^2  28 RND  29 FRC  30 X=0?  31 GTO 11  32 RDN  33 X^2  34 RND  35 FRC  36 X=0? 37 GTO 12  38 X<>Y  39 X^2  40 RND  41 FRC  42 X=0?  43 GTO 13  44 RCL 03      45 X^2  46 RND  47 FRC  48 X=0?  49 GTO 14  50 RCL 01  51 ENTER  52 SIGN  53 ST+ 02  54 - 55 STO 01  56 X>0?  57 GTO 02  58 GTO 01  59 LBL 13  60 RCL 01  61 CHS  62 STO 01      63 LBL 11  64 LASTX  65 RCL 00  66 RCL 02  67 *  68 GTO 00  69 LBL 14  70 RCL 01  71 CHS 72 STO 01  73 LBL 12  74 SF 02  75 LASTX  76 RCL 00  77 RCL 02      78 *  79 X^2  80 LBL 00  81 RCL 01  82 -  83 SIGN  84 *  85 RCL 02  86 X<>Y  87 RCL 01  88 END

( 109 bytes / SIZE 005 )

 STACK INPUTS OUTPUTS Z / c Y / +/- b X x a

If  CF 02   x = ( a +/- sqrt(b) ) / c
If  SF 02   x = sqrt [ a +/- sqrt(b) ] / c

Examples:   Let's imagine that we remember that  Sin 72°  may be expressed with square-roots of integrers but we don't remember the exact formula:

DEG  72   SIN   FIX 7   XEQ "SR?"   >>>>    10                              ---Execution time = 93s---
RDN    20
RDN     4      with    SF 02

-Thus the HP41 suggests that  Sin 72° =  sqrt ( 10 + sqrt (20) ) / 4

-Likewise,   72  COS  FIX 7  R/S   >>>>   -1
RDN     5
RDN     4        with    CF 02

-So   Cos 72° = ( -1 + sqrt(5) ) / 4

Notes:

-All the results given by this program don't prove anything !
-Since there is only a finite number of decimals, it's only a suggestion.

-I've used FIX 7 in the above example, but another choice may be better according to the magnitude of x.

2°)  Real -> Cube-Roots ?

"SR3?" tries to find integers a , b , c such that a real number x = ( a + cbrt(b) ) / c  or  cbrt [ a + cbrt(b) ] / c

Data Registers:   R00 = x , R01 = a , R02 = c   R03-R04: temp

Flag:  F03   CF 03 <->  x = ( a + cbrt(b) ) / c
SF 03 <->  x = sqrt [ a + cbrt(b) ] / c

Subroutines: /

 01 LBL "SR3?"  02 STO 00        03 CF 03  04 1  05 STO 04  06 LBL 01  07 RCL 04  08 STO 01  09 ENTER  10 SIGN  11 STO 02  12 +  13 STO 04  14 LBL 02  15 RCL 00  16 RCL 02  17 * 18 ENTER  19 X^2  20 LASTX  21 *  22 STO 03        23 LASTX  24 RCL 01  25 ST+ 03  26 ST+ T  27 ST- Z  28 -  29 ENTER  30 X^2  31 *  32 RND  33 FRC  34 X=0? 35 GTO 11  36 RDN  37 ENTER  38 X^2  39 *  40 RND  41 FRC  42 X=0?  43 GTO 12  44 X<>Y  45 ENTER        46 X^2  47 *  48 RND  49 FRC  50 X=0?  51 GTO 13 52 RCL 03  53 ENTER  54 X^2  55 *  56 RND  57 FRC  58 X=0?  59 GTO 14  60 RCL 01        61 ENTER  62 SIGN  63 ST+ 02  64 -  65 STO 01  66 X>0?  67 GTO 02  68 GTO 01 69 LBL 13  70 RCL 01  71 CHS  72 STO 01  73 GTO 11  74 LBL 14  75 RCL 01        76 CHS  77 STO 01  78 LBL 12  79 SF 03  80 LBL 11  81 RCL 02  82 LASTX  83 RCL 01  84 END

( 106 bytes / SIZE 005 )

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

If  CF 02   x = ( a + sqrt(b) ) / c
If  SF 02   x = sqrt [ a + sqrt(b) ] / c

Examples:

•  0.9174802104  FIX 7   XEQ "SR3?"   >>>>    3                       ---Execution time = 38s---
RDN    4
RDN    5     with   CF 03

-Thus  0.9174802104  ~  ( 3 + cbrt(4) ) / 5

•  -0.2565030277   FIX 7   R/S   >>>>    1
RDN   -9
RDN    4     with   SF 03

-So,  -0.2565030277  ~   ( cbrt ( 1 - cbrt(9) ) ) / 4

3°)  Real -> Solution of a Cubic Equation ?

a) X^3+p.X+q = 0

-Given a real number x ,  "P3?"  searches integers  p , q  such that x  is a solution of  x3 + p x + q = 0

Data Registers:   R00 = x , R01 = p
Flags:  /
Subroutines: /

 01 LBL "P3?"  02 STO 00     03 1  04 STO 01  05 LBL 01  06 RCL 00  07 X^2  08 STO Y  09 RCL 01 10 ST+ Z  11 -  12 RCL 00     13 ST* Z  14 *  15 RND  16 FRC  17 X=0?  18 GTO 11 19 X<>Y  20 RND  21 FRC  22 X=0?  23 GTO 12     24 ISG 01  25 CLX  26 GTO 01  27 LBL 11 28 RCL 01  29 CHS  30 GTO 00  31 LBL 12  32 RCL 01     33 LBL 00  34 LASTX  35 2  36 / 37 ENTER      38 X^2  39 RCL Z  40 3  41 ST/ Y  42 Y^X  43 +  44 X<>Y  45 END

( 63 bytes / SIZE 002 )

 STACK INPUTS OUTPUTS Y / B X x A

Whence  x ~ ( A + sqrt(B) )1/3 + ( A - sqrt(B) )1/3   ( perhaps... )

Example:

-0.3221853551  FIX 7  XEQ "P3?"  >>>>   -0.5                            ---Execution time = 2s---
X<>Y   1.25

-So  x ~  cbrt[ -1/2 + sqrt(5/4) ] + cbrt[ -1/2 - sqrt(5/4) ]

Notes:

-Tartaglia's formula gives a possible answer if B > 0: in this case, the polynomial has a unique real root.
-Otherwise, it involves complex numbers and the cubic equation has 2 or 3 real roots:
-You will have to check which of these roots corresponds to x

-The real number B may always be written  N / 108 because  B = (q^2)/4 + p^3 / 27  and  A = -q/2

b) X^3+p.X^2+q.X+r = 0

-This variant is a slightly more general program:

Data Registers:   R00 = x , R01 = p , R02 = q , R03 = r
Flags:  /
Subroutines: /

 01 LBL "P3?"  02 STO 00     03 CLX  04 STO 03  05 LBL 01  06 RCL 03  07 STO 01  08 ENTER  09 SIGN  10 +  11 STO 03  12 CLX  13 STO 02  14 LBL 02  15 RCL 00  16 RCL 00  17 RCL 00  18 RCL 01  19 +  20 *  21 RCL 02  22 +  23 *  24 RND 25 FRC  26 X=0?  27 GTO 11  28 CLX  29 RCL 01     30 +  31 *  32 RCL 02  33 -  34 *  35 RND  36 FRC  37 X=0?  38 GTO 12  39 CLX  40 RCL 01  41 -  42 *  43 RCL 02  44 +  45 *  46 RND  47 FRC  48 X=0? 49 GTO 13  50 CLX  51 RCL 01  52 -  53 *  54 RCL 02     55 -  56 *  57 RND  58 FRC  59 X=0?  60 GTO 14  61 RCL 01  62 ENTER  63 SIGN  64 ST+ 02  65 -  66 STO 01  67 CHS  68 X<=0?  69 GTO 02  70 GTO 01  71 LBL 14  72 RCL 02 73 CHS  74 STO 02  75 LBL 13  76 RCL 01  77 CHS  78 STO 01     79 GTO 11  80 LBL 12  81 RCL 02  82 CHS  83 STO 02  84 LBL 11  85 LASTX  86 CHS  87 STO 03  88 RCL 01  89 RCL 02  90 *  91 3  92 /  93 -  94 RCL 01  95 3  96 ST/ Y 97 Y^X  98 ST+ X  99 + 100 2 101 / 102 CHS 103 ENTER 104 X^2 105 RCL 01    106 X^2 107 3 108 / 109 RCL 02 110 X<>Y 111 - 112 3 113 ST/ Y 114 Y^X 115 + 116 X<>Y 117 RCL 01 118 3 119 / 120 CHS 121 END

( 140 bytes / SIZE 004 )

 STACK INPUTS OUTPUTS Z / C Y / B X x A

Whence  x ~ A + ( B + sqrt(C) )1/3 + ( B - sqrt(C) )1/3   ( perhaps... )

Example:

-6.279877039  FIX 7  XEQ "P3"  >>>>  -2.3333333                          ---Execution time = 130s---
RDN  -8.3703704
RDN  16.1481481

-In fact, if there were no roundoff-errors:  A = -7/3   B = -226/27  C = 436/27

-Thus,  x ~ -7/3 + cbrt [ -226/227 + sqrt (436/27 ) ]  +  cbrt [ -226/227 - sqrt (436/27 ) ]

Notes:

-Tartaglia's formula gives a possible answer if C > 0: in this case, the polynomial has a unique real root.
-Otherwise, it involves complex numbers and the cubic equation has 2 or 3 real roots:
-You will have to check which of these roots corresponds to x

-The real number C may always be written  N / 108 because  C = (Q^2)/4 + P^3 / 27  and  B = -Q/2
with  X^3 + P.X + Q = 0  after the change of variable  x - p/3  to remove the term in x^2