.if SYSPAGE = ODD .pn pref B- .pn 1

Appendix B: Some notes on the EPe programs

This appendix describes a method to increase the speed of the EPe programs, at the expense of finding a smaller number of digits of "e". Also included is a discussion on the accuracy of the HP-41 MOD function.

Description of the method

To find "e" to N places, it is necessary to find a factorial which has at least N digits. At first sight this seems extremely difficult, but in reality it is quite simple!

Let's take a look at this (well known) formula to make a (fairly accurate) estimate about the magnitude of X!: .sk .fo off .ce X! = (X/e)&UAR.X &ML &SQR.(2&PI.X) .fo on

The above formula, generally known as Stirling's Formula, has a relative accuracy of at least 99% for X\>\9. When we take the (base 10) logarithm -\the integer part of it will give us the number of digits [minus 1] of X!\- we get: .sk .fo off .ce &S1.&S0.Log(X!) = [(X + .5) &ML Ln(X) - X + .5 &ML Ln(2&PI.)] / Ln(10)

If we simplify this formula by making the following assignments,

we get: .sk .ce N + 1 = X &ML Log(X) + 0.5 &ML Log(X) - 0.434 &ML X + 0.4 .fo on

Solving this equation for X directly is not possible, so I tried an iterative approach by rearranging the terms. Doing so I arrived at this formula: .sk .fo off .ce X' = (N + 0.6 + 0.434 &ML X - Log(X) / 2) / Log(X) .fo on

I was pleasantly surprised to find out that this formula converges very fast. The program on the next page uses the above formula to find X when N, the number of digits in X!, is given. :h3 id=eprog.A program for finding X when Log(X!) is given

User Instructions

The instructions for the program are:

  1. Enter the required number of digits in X! and press A.
    The program will stop with a first approximation of X.
  2. Now press R/S repeatedly, until the integer part of the displayed value doesn't change anymore.

Once you get a number with a constant integer part in the display, you can enter this integer part in the program on the next page, to make absolutely sure that the factorial of this number has at least N digits.

Listing of the program

000  76 LBL         010  85  +          020  01  01         030  28 LOG
001  11  A          011  93  .          021  75  -          031  54  )
002  42 STO         012  06  6          022  43 RCL         032  42 STO
003  00  00         013  85  +          023  01  01         033  01  01
004  42 STO         014  93  .          024  34 ûX          034  92 RTN
005  01  01         015  04  4          025  28 LOG         035  61 GTO
006  53  (          016  03  3          026  54  )          036  00  00
007  53  (          017  04  4          027  55  ö          037  06  06
008  43 RCL         018  65  x          028  43 RCL
009  00  00         019  43 RCL         029  01  01
<\pre>

A Program to calculate X! using Stirling's Method

User Instructions

The instructions for this program are:

  1. Enter X and press A.
    The calculator will stop with the first 10 digits of X! in the display. These 10 digits will be correct for all X\>\4.
  2. Press X:T to find the exponent of X! The exponent plus one gives you the number of digits in X!

Listing of the program

000  76 LBL         025  02  2          050  05  5          075  00  0
001  11  A          026  65  x          051  75  -          076  23 LNX
002  42 STO         027  89  ã          052  43 RCL         077  75  -
003  00  00         028  54  )          053  00  00         078  59 INT
004  53  (          029  34 ûX          054  45 Y^X         079  32 X:T
005  53  (          030  23 LNX         055  07  7          080  54  )
006  53  (          031  85  +          056  55  ö          081  22 INV
007  69 OP          032  53  (          057  01  1          082  59 INT
008  20  20         033  43 RCL         058  04  4          083  22 INV
009  85  +          034  00  00         059  00  0          084  28 LOG
010  93  .          035  75  -          060  85  +          085  92 RTN
011  05  5          036  29 CP          061  43 RCL
012  54  )          037  45 Y^X         062  00  00
013  65  x          038  03  3          063  45 Y^X
014  43 RCL         039  55  ö          064  09  9
015  00  00         040  03  3          065  55  ö
016  23 LNX         041  00  0          066  09  9
017  75  -          042  85  +          067  09  9
018  43 RCL         043  43 RCL         068  54  )
019  00  00         044  00  00         069  55  ö
020  35 1/X         045  45 Y^X         070  01  1
021  48 EXC         046  05  5          071  02  2
022  00  00         047  55  ö          072  54  )
023  85  +          048  01  1          073  55  ö
024  53  (          049  00  0          074  01  1
<\pre>
:note.This program can also be used in cooperation
with EPF1 and EPF2.

Speeding up the EPe programs

Using the information in the two previous sections, we can now make the necessary modifications to EPe1, EPe2 and EPe3.

Modifications to EPe1

If you want to use EPe1 to calculate "e" to 10N [N=2..97] places, you must make the following modifications to it:

The above changes are valid only for the TI-59[/PC-100] versions of the program, but similar modifications can be made to the TI-58 and TI-66 versions of the program.

Modifications to EPe2

If you want to use EPe2 to calculate "e" to 13N [N=2..99] places, you must make the following modifications to it:

The above changes are valid only for the TI-59 version of the program, but similar modifications can be made to the TI-58 and TI-66 versions of the program.

Modifications to EPe3

If you want to use EPe3 to calculate "e" to 10N [N=2..86] places, you must make the following modifications to it:

The above changes are valid only for the TI-59[/PC-100] version of the program, but similar modifications can be made to the TI-58 version of the program.

A better solution....

Shortly after having sent Palmer O. Hanson an advance copy of this book, I received a flood of letters from him. In one of them he wrote the following: :lq.For most of the programs it would be helpful to be able to calculate to different levels of precision. For finding e, that is what your pages B-1 through B-5 are all about. I think the solution on V11N1P17 [of TI PPC Notes] is much better, and certainly much more user friendly. :elq

Of course Palmer was right, but when I started with the book, I didn't know that I would discover the method to find N when the number of digits in N! In fact I only discovered it just a few days before I finished. At that particular moment it did occur to me that the method could be incorporated into EPe1 and EPe2, but knowing that most users are mainly interested in as many digits as possible, I put it in an appendix.

However, the following pages include revisions of EPe1, EPe2 and EPe3. Only the TI-59 versions are modified, as modifications to the TI-58 and TI-66 versions would lead to significant loss of available memory. .*

EPe1: The Modified TI-59[/PC-100] Version

User Instructions

The instructions for this version of the program are:

  1. Enter the program.
  2. Enter the number of required digits and press A.
    The number of digits you enter will be rounded up to the next multiple of 10, and if you enter a number greater than 980, the TI-59 will assume that you entered 980.
    The calculator will now perform some preliminary calculations, to determine the factorial it needs for the required precision. After a little while it will stop with a flashing\1 in the display.
  3. Now press 7 EE to start the program in Fast Mode.

When the program has ended, the paper advances and directly after that the result, "e" to the entered number of places, is printed.

Listing of the Modified TI-59[/PC-100] Version

000  92 RTN         040  22 INV         080  32 X:T         120  43 RCL
001  82 HIR         041  52 EE          081  53  (          121  00  00
002  18  18         042  52 EE          082  09  9          122  85  +
003  42 STO         043  01  1          083  08  8          123  93  .
004  00  00         044  00  0          084  00  0          124  06  6
005  01  1          045  85  +          085  22 INV         125  75  -
006  52 EE          046  73 RC*         086  77 XòT         126  43 RCL
007  09  9          047  00  00         087  00  00         127  01  01
008  22 INV         048  75  -          088  90  90         128  34 ûX
009  52 EE          049  53  (          089  32 X:T         129  28 LOG
010  74 SM*         050  24 CE          090  55  ö          130  54  )
011  00  00         051  55  ö          091  01  1          131  55  ö
012  97 DSZ         052  43 RCL         092  00  0          132  43 RCL
013  99  99         053  99  99         093  85  +          133  01  01
014  00  00         054  54  )          094  03  3          134  28 LOG
015  35  35         055  59 INT         095  42 STO         135  54  )
016  22 INV         056  72 ST*         096  02  02         136  97 DSZ
017  64 PD*         057  00  00         097  93  .          137  02  02
018  00  00         058  65  x          098  09  9          138  01  01
019  76 LBL         059  43 RCL         099  54  )          139  10  10
020  15  E          060  99  99         100  59 INT         140  53  (
021  82 HIR         061  54  )          101  53  (          141  59 INT
022  18  18         062  97 DSZ         102  82 HIR         142  85  +
023  42 STO         063  00  00         103  08  08         143  69 OP
024  00  00         064  00  00         104  65  x          144  17  17
025  98 ADV         065  39  39         105  01  1          145  01  1
026  73 RC*         066  61 GTO         106  00  0          146  54  )
027  00  00         067  00  00         107  54  )          147  47 CMS
028  99 PRT         068  01  01         108  42 STO         148  42 STO
029  97 DSZ         069  00  0          109  00  00         149  99  99
030  00  00         070  00  0          110  53  (          150  22 INV
031  00  00         071  00  0          111  53  (          151  57 ENG
032  26  26         072  00  0          112  42 STO         152  22 INV
033  00  0          073  00  0          113  01  01         153  58 FIX
034  81 RST         074  00  0          114  65  x          154  60 DEG
035  43 RCL         075  00  0          115  93  .          155  04  4
036  99  99         076  00  0          116  04  4          156  05  5
037  66 PAU         077  00  0          117  03  3          157  30 TAN
038  00  0          078  76 LBL         118  04  4          158  33 Xý
039  53  (          079  11  A          119  85  +          159  86 STF
<\pre>
.*

EPe2: The Modified TI-59 Version

User Instructions

The instructions for this version of the program are:

  1. Enter the program.
  2. Enter the number of required digits and press A.
    The number of digits you enter will be rounded up to the next multiple of 13, and if you enter a number greater than 1300, the TI-59 will assume that you entered 1300.
    The calculator will now perform some preliminary calculations, to determine the factorial it needs for the required precision. After a little while it will stop with a flashing\1 in the display.
  3. Now press 7 EE to start the program in Fast Mode.
  4. When the program has ended, you can find the result in Rxx [MSD] through R00\[LSD], 13\digits per register. The program does not print the result.

    To find xx you have to divide the number you entered in step 2 by 13, subtract 0.92 from it and then take the integer part of it.

  5. The result can be displayed performing the following steps for every register, starting with Rxx, down to R00:
    • Divide the recalled register by 1.E7.
    • Record the 6 digits to the left of the decimal point.
    • Press INV INT.
    • Record the 7 digits to the right of the decimal point.
      Think about leading and trailing zeroes!
  6. To record the result, you can press E, and insert four card-sides.
If you consider the above method for displaying the result not very "Anwenderfreundlich", you are right. To make things a little easier, you can enter the following short program from the keyboard when the program has finished. After having entered it, you can press E, repeatedly followed by R/S to display the result. (If you have a PC-100, you can of course change steps 009 and 016 into PRT) The read-out is complete when a flashing zero appears.

Listing of the read-out program

000  76 LBL         015  07  7          030  09  09         045  54  )
001  15  E          016  22 INV         031  82 HIR         046  58 FIX
002  50 IxI         017  57 ENG         032  17  17         047  07  07
003  82 HIR         018  75  -          033  53  (          048  91 R/S
004  18  18         019  59 INT         034  42 STO         049  22 INV
005  48 EXC         020  91 R/S         035  00  00         050  58 FIX
006  00  00         021  54  )          036  55  ö          051  00  0
007  82 HIR         022  58 FIX         037  01  1          052  69 OP
008  07  07         023  07  07         038  52 EE          053  68  68
009  53  (          024  91 R/S         039  07  7          054  92 RTN
010  73 RC*         025  22 INV         040  22 INV
011  00  00         026  58 FIX         041  57 ENG
012  55  ö          027  97 DSZ         042  75  -
013  01  1          028  00  00         043  59 INT
014  52 EE          029  00  00         044  91 R/S
<\pre>

Listing of the Modified TI-59 Version

000  92 RTN         060  82 HIR         120  81 RST         180  08  08
001  32 X:T         061  15  15         121  76 LBL         181  65  x
002  69 OP          062  75  -          122  15  E          182  01  1
003  17  17         063  53  (          123  50 IxI         183  03  3
004  47 CMS         064  46 INS         124  01  1          184  85  +
005  82 HIR         065  55  ö          125  96 WRT         185  01  1
006  06  06         066  82 HIR         126  02  2          186  03  3
007  82 HIR         067  17  17         127  96 WRT         187  54  )
008  18  18         068  54  )          128  03  3          188  42 STO
009  42 STO         069  59 INT         129  96 WRT         189  00  00
010  00  00         070  82 HIR         130  04  4          190  53  (
011  01  1          071  05  05         131  96 WRT         191  53  (
012  82 HIR         072  65  x          132  00  0          192  42 STO
013  57  57         073  82 HIR         133  92 RTN         193  01  01
014  52 EE          074  17  17         134  00  0          194  65  x
015  01  1          075  85  +          135  00  0          195  93  .
016  02  2          076  32 X:T         136  00  0          196  04  4
017  22 INV         077  54  )          137  00  0          197  03  3
018  52 EE          078  65  x          138  00  0          198  04  4
019  74 SM*         079  01  1          139  00  0          199  85  +
020  00  00         080  52 EE          140  00  0          200  43 RCL
021  82 HIR         081  03  3          141  00  0          201  00  00
022  17  17         082  82 HIR         142  00  0          201  85  +
023  66 PAU         083  45  45         143  00  0          203  93  .
024  29 CP          084  75  -          144  00  0          204  06  6
025  67 X=T         085  53  (          145  00  0          205  75  -
026  01  01         086  46 INS         146  00  0          206  43 RCL
027  15  15         087  55  ö          147  00  0          207  01  01
028  69 OP          088  82 HIR         148  76 LBL         208  34 ûX
029  20  20         089  17  17         149  11  A          209  28 LOG
030  86 STF         090  54  )          150  32 X:T         210  54  )
031  00  00         091  59 INT         151  03  3          211  55  ö
032  53  (          092  82 HIR         152  42 STO         212  43 RCL
033  97 DSZ         093  35  35         153  02  02         213  01  01
034  00  00         094  65  x          154  02  2          214  28 LOG
035  00  00         095  82 HIR         155  06  6          215  54  )
036  45  45         096  17  17         156  22 INV         216  97 DSZ
037  22 INV         097  54  )          157  77 XòT         217  02  02
038  86 STF         098  22 INV         158  01  01         218  01  01
039  00  00         099  52 EE          159  61  61         219  90  90
040  82 HIR         100  52 EE          160  32 X:T         220  53  (
041  16  16         101  01  1          161  01  1          221  59 INT
042  61 GTO         102  00  0          162  03  3          222  85  +
043  00  00         103  32 X:T         163  00  0          223  01  1
044  47  47         104  82 HIR         164  00  0          224  54  )
045  73 RC*         105  15  15         165  53  (          225  82 HIR
046  00  00         106  72 ST*         166  22 INV         226  07  07
047  55  ö          107  00  00         167  77 XòT         227  09  9
048  01  1          108  87 IFF         168  01  01         228  69 OP
049  52 EE          109  00  00         169  71  71         229  17  17
050  03  3          110  00  00         170  32 X:T         230  22 INV
051  75  -          111  32  32         171  55  ö          231  57 ENG
052  59 INT         112  61 GTO         172  01  1          232  22 INV
053  82 HIR         113  00  00         173  03  3          233  58 FIX
054  05  05         114  05  05         174  75  -          234  60 DEG
055  54  )          115  82 HIR         175  35 1/X         235  04  4
056  53  (          116  16  16         176  54  )          236  05  5
057  53  (          117  42 STO         177  59 INT         237  30 TAN
058  32 X:T         118  00  00         178  53  (          238  33 Xý
059  85  +          119  25 CLR         179  82 HIR         239  86 STF
<\pre>
.*

EPe1: The Modified TI-59[/PC-100] Version

User Instructions

The instructions for this version of the program are:

  1. Enter the preprocessor.
  2. Enter the number of digits required and press A.
    The number of digits you enter will be rounded up to the next multiple of 10, and if you enter a number greater than 870, the TI-59 will assume that you entered 870.
    The calculator will now perform some preliminary calculations, to determine the factorial it needs for the required precision. After a little while it will stop with the faint "[" in the display, waiting for the card containing the main program to be inserted into the card-reader, so...
  3. Insert the card containing the main program into the card-reader.
    After having read-in the main program, the calculator stops with a flashing 1 in the display. :note.The main program has to be recorded in a 9\OP\17 partitioning!
  4. Now press 7 EE to start the program in Fast Mode.
  5. When the program has ended, you recall the result by pressing E, repeatedly followed by R/S. The read-out is complete when a flashing zero appears. (If you have a PC-100, you can of course change step 205 into PRT)

Listing of the Modified TI-59[/PC-100] Version [Preprocessor]

000  76 LBL         020  42 STO         040  93  .          060  54  )
001  11  A          021  02  02         041  04  4          061  97 DSZ
002  32 X:T         022  93  .          042  03  3          062  02  02
003  09  9          023  09  9          043  04  4          063  00  00
004  69 OP          024  54  )          044  85  +          064  35  35
005  17  17         025  59 INT         045  43 RCL         065  59 INT
006  53  (          026  53  (          046  00  00         066  47 CMS
007  08  8          027  82 HIR         047  85  +          067  42 STO
008  07  7          028  08  08         048  93  .          068  89  89
009  00  0          029  65  x          049  06  6          069  22 INV
010  22 INV         030  01  1          050  75  -          070  57 ENG
011  77 XòT         031  00  0          051  43 RCL         071  22 INV
012  00  00         032  54  )          052  01  01         072  58 FIX
013  15  15         033  42 STO         053  34 ûX          073  01  1
014  32 X:T         034  00  00         054  28 LOG         074  22 INV
015  55  ö          035  53  (          055  54  )          075  96 WRT
016  01  1          036  53  (          056  55  ö          076  61 GTO
017  00  0          037  42 STO         057  43 RCL         077  02  02
018  85  +          038  01  01         058  01  01         078  30  30
019  03  3          039  65  x          059  28 LOG
<\pre>

Listing of the Modified TI-59[/PC-100] Version [Main Program]

000  92 RTN         060  82 HIR         120  00  00         180  82 HIR
001  01  1          061  03  03         121  82 HIR         181  18  18
002  82 HIR         062  22 INV         122  44  44         182  77 XòT
003  04  04         063  64 PD*         123  53  (          183  01  01
004  82 HIR         064  00  00         124  82 HIR         184  87  87
005  05  05         065  82 HIR         125  15  15         185  82 HIR
006  44 SUM         066  44  44         126  65  x          186  07  07
007  89  89         067  32 X:T         127  53  (          187  97 DSZ
008  52 EE          068  53  (          128  35 1/X         188  89  89
009  08  8          069  53  (          129  65  x          189  00  00
010  32 X:T         070  82 HIR         130  82 HIR         190  01  01
011  02  2          071  14  14         131  14  14         191  25 CLR
012  94 +/-         072  55  ö          132  54  )          192  81 RST
013  42 STO         073  82 HIR         133  59 INT         193  76 LBL
014  00  00         074  15  15         134  74 SM*         194  15  E
015  43 RCL         075  54  )          135  00  00         195  82 HIR
016  89  89         076  59 INT         136  54  )          196  18  18
017  82 HIR         077  65  x          137  82 HIR         197  42 STO
018  45  45         078  82 HIR         138  54  54         198  89  89
019  69 OP          079  15  15         139  82 HIR         199  00  0
020  20  20         080  54  )          140  13  13         200  42 STO
021  82 HIR         081  82 HIR         141  82 HIR         201  00  00
022  15  15         082  54  54         142  34  34         202  69 OP
023  77 XòT         083  53  (          143  97 DSZ         203  20  20
024  00  00         084  82 HIR         144  88  88         204  73 RC*
025  33  33         085  11  11         145  00  00         205  00  00
026  97 DSZ         086  22 INV         146  55  55         206  91 R/S
027  89  89         087  77 XòT         147  53  (          207  97 DSZ
028  00  00         088  01  01         148  82 HIR         208  89  89
029  15  15         089  07  07         149  14  14         209  02  02
030  01  1          090  75  -          150  55  ö          210  01  01
031  44 SUM         091  53  (          151  82 HIR         211  00  0
032  89  89         092  69 OP          152  15  15         212  69 OP
033  53  (          093  30  30         153  82 HIR         213  68  68
034  43 RCL         094  55  ö          154  46  46         214  92 RTN
035  89  89         095  32 X:T         155  85  +          215  00  0
036  82 HIR         096  54  )          156  93  .          216  00  0
037  65  65         097  59 INT         157  05  5          217  00  0
038  85  +          098  74 SM*         158  54  )          218  00  0
039  02  2          099  00  00         159  59 INT         219  00  0
040  85  +          100  65  x          160  74 SM*         220  00  0
041  82 HIR         101  82 HIR         161  00  00         221  00  0
042  44  44         102  13  13         162  82 HIR         222  00  0
043  01  1          103  54  )          163  16  16         223  00  0
044  82 HIR         104  53  (          164  32 X:T         224  00  0
045  34  34         105  69 OP          165  01  1          225  00  0
046  97 DSZ         106  20  20         166  77 XòT         226  00  0
047  00  00         107  63 EX*         167  01  01         227  00  0
048  00  00         108  00  00         168  87  87         228  00  0
049  40  40         109  75  -          169  82 HIR         229  00  0
050  54  )          110  59 INT         170  37  37         230  82 HIR
051  82 HIR         111  82 HIR         171  52 EE          231  06  06
052  17  17         112  34  34         172  01  1          232  82 HIR
053  42 STO         113  54  )          173  00  0          233  07  07
054  88  88         114  82 HIR         174  94 +/-         234  60 DEG
055  69 OP          115  43  43         175  82 HIR         235  04  4
056  20  20         116  01  1          176  46  46         236  05  5
057  01  1          117  52 EE          177  82 HIR         237  30 TAN
058  52 EE          118  05  5          178  17  17         238  33 Xý
059  05  5          119  64 PD*         179  32 X:T         239  86 STF
<\pre>

The HP-41 MOD function

In the description of EPe3 you can read that the MOD function of the HP-41 is more accurate than a synthesized MOD function on the TI-59.

I already indicated that this may be very hard to believe, but from the trouble I had translating the original HP-41 program I can only conclude that it is true. [A second source for support is the article "Personal Calculator Algorithms II: Trigonometric Functions", which appeared on pages 17-20 of the HP Journal of June 1977]

An example will provide more clarity: :lq.Suppose we want to calculate X MOD Y with X = 12345678900000 and Y\=\23456789.

Using the "normal" TI-59 MOD function, assuming that X and Y have been stored in R0 and R1, .sk .ce ( RCL 0 - ( CE &DV RCL 1 ) INT &ML RCL 1 ) :pc.we get 18997460.

This answer is wrong, as may easily be verified by hand calculation. [The correct answer is 18997465] The difference between these two answers comes from the fact that INT(X/Y) has 6 digits. Because Y\has 8 digits the result of the multiplication of Y and INT(X/Y) has 14 digits, which is one more than the TI-59 can hold.

The HP-41 uses a different approach to obtain X MOD Y. Instead of using the equivalent of the above TI-59 method, it repeatedly subtracts Y\&ML.\10&UAR.N [= Y'] from X. In this "equation" N is the smallest number such that Y' is less than X. Once the result of the subtractions becomes negative, Y' is added again. Next N is decreased by one and this process is repeated until the original Y is greater than the reduced X.

It may seem that this method is just as (in-)accurate as the TI-59 method, because it will at some stage lead to numbers with 11 significant digits. Fortunately the HP-41 can internally work with these numbers, so accuracy is maintained during the whole process! :elq

To obtain the same accuracy on a TI-59, we would have to use the following program, .sk

000  76 LBL         012  53  (          024  01  1          036  65  x
001  11  A          013  59 INT         025  00  0          037  01  1
002  53  (          014  55  ö          026  00  0          038  00  0
003  43 RCL         015  01  1          027  00  0          039  00  0
004  00  00         016  00  0          028  65  x          040  00  0
005  75  -          017  00  0          029  43 RCL         041  65  x
006  53  (          018  00  0          030  01  01         042  43 RCL
007  24 CE          019  54  )          031  75  -          043  01  01
008  55  ö          020  42 STO         032  43 RCL         044  54  )
009  43 RCL         021  02  02         033  02  02         045  92 RTN
010  01  01         022  59 INT         034  22 INV
011  54  )          023  65  x          035  59 INT
<\pre>

which is obviously much to long to be incorporated into EPe3.