Harmonic Numbers for the HP-41
Overview
1°) Harmonic Numbers
a) Program#1
b) Program#2
2°) Generalized Harmonic Numbers
a) Program#1
b) Program#2
3°) A Touch of Quaternions
a) Program#1
b) Program#2
-All these programs are quite elementary !
1°) Harmonic Numbers
a) Program#1
-The n-th harmonic number is defined as Hn = 1 + 1/2
+ 1/3 + ........... + 1/n
where n is a positive integer.
Data Registers: /
Flags: /
Subroutines: /
01 LBL "HRM"
02 ENTER^ 03 ENTER^ 04 CLX 05 LBL 01 06 RCL Y 07 1/X 08 + 09 DSE Y 10 GTO 01 11 R^ 12 SIGN 13 RDN 14 END |
( 25 bytes / SIZE 000 )
STACK | INPUT | OUTPUT |
X | n | Hn |
L | / | n |
where n is a positive integer
Example:
41 XEQ "HRM" >>>> H41 = 4.302933282 ---Execution time = 9s---
Notes:
-Lines 11-12-13 are only useful to save n in L-register
-Otherwise, they could be deleted
-"HRM" is very slow for large n-values.
-If n is very large, Hn ~ Ln n + gamma + 1/(2n)
where gamma = Euler's constant = 0.5772156649....
b) Program#2
-For large n-values, we can use a better asymptotic series, namely:
Hn ~ Ln n + gamma + 1/(2 n) - 1/(12 n2) + 1/(120 n4) - 1/(256 n6) + 1/(240 n8) - 1/(132 n10) + ....
where gamma = Euler's constant = 0.5772156649....
Data Register: R00 = n
Flags: /
Subroutines: /
01 LBL "HRM+"
02 STO 00 03 X^2 04 1/X 05 ENTER^ 06 STO Z 07 11 |
08 /
09 CHS 10 20 11 1/X 12 + 13 * 14 21 |
15 1/X
16 - 17 * 18 .1 19 + 20 * 21 1 |
22 -
23 * 24 12 25 / 26 RCL 00 27 ST+ X 28 1/X |
29 +
30 RCL 00 31 LN 32 + 33 .5772156649 34 + 35 END |
( 61 bytes / SIZE
001 )
STACK | INPUT | OUTPUT |
X | n | Hn |
where n is a positive integer
Examples:
10 XEQ "HRM+" >>>> H10
= 2.928968254
---Execution time = 2s---
41
R/S >>>>
H41 = 4.302933282
1000 R/S
>>>> H1000 = 7.485470861
2°) Generalized Harmonic Numbers
a) Program#1
-The harmonic numbers may be generalized by the definition:
Hn,m = 1 + 1/2m + 1/3m
+ ........... + 1/nm
where n is a positive integer.
Data Registers: R00 = - m
Flags: /
Subroutines: /
01 LBL "HNM"
02 CHS 03 STO 00 04 CLX 05 LBL 01 06 RCL Y 07 RCL 00 08 Y^X 09 + 10 DSE Y 11 GTO 01 12 END |
( 23 bytes / SIZE 001 )
STACK | INPUTS | OUTPUTS |
Y | n | 0 |
X | m | Hn,m |
where n is a positive integer
Example:
41 ENTER^
0.7 XEQ "HNM" >>>> H41,0.7
= 7.414455630
---Execution time = 26s---
Notes:
-If m = 1 , we get the harmonic numbers Hn
-"HNM" is even slower than "HRM" for large n-values...
-The following M-Code routine is significantly - but not a lot - faster
08D "M"
00E "N"
008 "H"
2A0 SETDEC
3C8 CLRKEY
0F8 C=X
128 L=C
04E C=0 ALL
0E8 X=C
0B8 C=Y
070 N=C ALL
004 CLRF 3
Loop
084 C
115 =
06C Ln(C)
138 C=L
2BE C=-C
13D C=
061 AB*C
044 C
035 =
068 exp(AB)
0F8 C=X
025 C=
060 AB+C
0E8 X=C
0B0 C=N ALL
02E C
0FA ->
0AE AB
009 C=
060 AB-1
070 N=C ALL
3CC ?KEY
360 ?C RTN
2EE ?C#0 ALL
3A0 ?NC RTN
2FE ?C#0 MS
32B JNC-27d
Goto Loop
( 39 words )
STACK | INPUTS | OUTPUTS |
Y | n | n |
X | m | Hn,m |
L | / | m |
where n is a positive integer
Example:
41 ENTER^
0.7 XEQ "HNM" >>>> H41,0.7
= 7.414455628
---Execution time = 19s---
Notes:
-Roundoff-errors are smaller.
-Registers Z & T are saved
-There is no check for alpha data.
-Press any key to stop an infinite loop or if you think the routine
is too slow
b) Program#2
-A even more generalized harmonic number may be defined if we introduce an offset c
Hn,c,m = SUMk=1,2,...,n 1/(c+k)m
Data Registers: R00 = - m , R01
= c
Flags: /
Subroutines: /
01 LBL "HNCM"
02 CHS 03 STO 00 04 RDN 05 STO 01 06 CLX 07 LBL 01 08 RCL Y 09 RCL 01 10 + 11 RCL 00 12 Y^X 13 + 14 DSE Y 15 GTO 01 16 END |
( 28 bytes / SIZE 002 )
STACK | INPUTS | OUTPUTS |
Z | n | / |
Y | c | 0 |
X | m | Hn,c,m |
where n is a positive integer
Example:
41 ENTER^
3 1/X
2 SQRT XEQ "HNCM" >>>>
H41 , 1/3 , sqrt(2) = 2.016949427
---Execution time = 30s---
Note:
-If c = 0 , Hn,c,m = Hn,m
3°) A Touch of Quaternions
a) Program#1
-"HNQ" computes Hn,q = 1 + 1/2q
+ 1/3q + ........... + 1/nq
where n is a positive integer and q is a quaternion.
Data Registers: • R00 = n ( Register R00 is to be initialized before executing "HNQ" )
R01 thru R08: temp , When the program stops, R05 to R08
= Hn,q
Flags: /
Subroutine: "E^Q" ( cf "Quaternions
for the HP-41" paragraph 6 )
01 LBL "HNQ"
02 STO 01 03 RDN 04 STO 02 05 RDN 06 STO 03 07 X<>Y 08 STO 04 09 CLX |
10 STO 05
11 STO 06 12 STO 07 13 STO 08 14 LBL 01 15 RCL 00 16 LN 17 CHS 18 SIGN |
19 RCL 04
20 RCL 03 21 RCL 02 22 RCL 01 23 X<> L 24 ST* L 25 ST* Y 26 ST* Z 27 ST* T |
28 X<> L
29 XEQ "E^Q" 30 ST+ 05 31 RDN 32 ST+ 06 33 RDN 34 ST+ 07 35 X<>Y 36 ST+ 08 |
37 DSE 00
38 GTO 01 39 RCL 08 40 RCL 07 41 RCL 06 42 RCL 05 43 END |
( 67 bytes / SIZE 009 )
STACK | INPUTS | OUTPUTS |
T | t | t' |
Z | z | z' |
Y | y | y' |
X | x | x' |
Where q = x + y i + z j + t k ,
Hn,q = x' + y' i + z' j + t' k
and R00 = n = positive integer
Example: Calculate H12 , 1+2i+3j+4k
12 STO 00
4 ENTER^
3 ENTER^
2 ENTER^
1 XEQ "HNQ" >>>>
0.961265794 = R05
---Execution time = 35s---
RDN 0.116094882 = R06
RDN 0.174142323 = R07
RDN 0.232189764 = R08
Whence, H12 , 1+2i+3j+4k = 0.961265794
+ 0.116094882 i + 0.174142323 j +
0.232189764 k
b) Program#2
-Here, we compute Hn,q,m = SUMp=1,2,...,n 1/(q+p)m
where n is a positive integer , m is a real number
and q is a quaternion: q = x + y i + z j + t k
Data Registers: R00 = - m ( Registers R01 thru R04 are to be initialized before executing "HNQM" )
• R01 = x
• R02 = y
R05 thru R09: temp
• R03 = z
• R04 = t
When the program stops, R05 to R08 = Hn,q,m
Flags: /
Subroutine: "Q^R" ( cf "Quaternions
for the HP-41" paragraph 8-a) )
01 LBL "HNQM"
02 CHS 03 STO 00 04 X<>Y 05 STO 09 06 CLX 07 STO 05 |
08 STO 06
09 STO 07 10 STO 08 11 LBL 01 12 RCL 01 13 RCL 09 14 + |
15 RCL 04
16 RCL 03 17 RCL 02 18 R^ 19 XEQ "Q^R" 20 ST+ 05 21 RDN |
22 ST+ 06
23 RDN 24 ST+ 07 25 X<>Y 26 ST+ 08 27 DSE 09 28 GTO 01 |
29 RCL 08
30 RCL 07 31 RCL 06 32 RCL 05 33 END |
( 52 bytes / SIZE 010 )
STACK | INPUTS | OUTPUTS |
T | / | t' |
Z | / | z' |
Y | n | y' |
X | m | x' |
Where q = x + y i + z j + t k is stored in
R01 thru R04 ,
Hn,q,m = x' + y' i + z' j + t' k
and n = positive integer , m = real
number
Example: Calculate H41 , 1+2i+3j+4k , 0.7
1 STO 01
2 STO 02
3 STO 03
4 STO 04
41 ENTER^
0.7 XEQ "HNQM" >>>>
5.116889312 = R05
---Execution time = 128s---
RDN -0.643311229 = R06
RDN -0.964966843 = R07
RDN -1.286622458 = R08
-Whence H41 , 1+2i+3j+4k , 0.7 =
5.116889312 - 0.643311229 i - 0.964966843 j
- 1.286622458 k
Note:
-The programs listed in paragraph 3 are probably not very useful...
References:
[1] http://mathworld.wolfram.com/HarmonicNumber.html
[2] M. J. Kronenburg - "Some Generalized Harmonic Number Identities"