Go forward to Programming Answer 10. Go backward to Programming Answer 8. Go up to Answers to Exercises.

Programming Tutorial Exercise 9
-------------------------------

The first step is to adjust `z' to be greater than 5.  A simple "for"
loop will do the job here.  If `z' is less than 5, we reduce the
problem using = psi(z+1) - 1/z}.  We go on to compute `psi(z+1)', and
remember to add back a factor of `-1/z' when we're done.  This step is
repeated until `z > 5'.

(Because this definition is long, it will be repeated in concise form
below.  You can use `M-# m' to load it from there.  While you are
entering a `Z ` Z '' body in a macro, Calc simply collects
keystrokes without executing them.  In the following diagrams we'll
pretend Calc actually executed the keystrokes as you typed them,
just for purposes of illustration.)

     1:  1.             1:  1.
         .                  .

      1.0 RET       C-x ( Z `  s 1  0 t 2

Here, variable 1 holds `z' and variable 2 holds the adjustment factor.
If `z < 5', we use a loop to increase it.

(By the way, we started with `1.0' instead of the integer 1 because
otherwise the calculation below will try to do exact fractional
arithmetic, and will never converge because fractions compare equal
only if they are exactly equal, not just equal to within the current
precision.)

     3:  1.      2:  1.       1:  6.
     2:  1.      1:  1            .
     1:  5           .
         .

       RET 5        a <    Z [  5 Z (  & s + 2  1 s + 1  1 Z ) r 1  Z ]

Now we compute the initial part of the sum: `ln(z) - 1/2z' minus the
adjustment factor.

     2:  1.79175946923      2:  1.7084261359      1:  -0.57490719743
     1:  0.0833333333333    1:  2.28333333333         .
         .                      .

         L  r 1 2 * &           -  r 2                -

Now we evaluate the series.  We'll use another "for" loop counting up
the value of `2 n'.  (Calc does have a summation command, `a +', but
we'll use loops just to get more practice with them.)

     3:  -0.5749       3:  -0.5749        4:  -0.5749      2:  -0.5749
     2:  2             2:  1:6            3:  1:6          1:  2.3148e-3
     1:  40            1:  2              2:  2                .
         .                 .              1:  36.
                                              .

        2 RET 40        Z ( RET k b TAB     RET r 1 TAB ^      * /

     3:  -0.5749       3:  -0.5772      2:  -0.5772     1:  -0.577215664892
     2:  -0.5749       2:  -0.5772      1:  0               .
     1:  2.3148e-3     1:  -0.5749          .
         .                 .

       TAB RET M-TAB       - RET M-TAB      a =     Z /    2  Z )  Z ' C-x )

This is the value of `- gamma', with a slight bit of roundoff error.
To get a full 12 digits, let's use a higher precision:

     2:  -0.577215664892      2:  -0.577215664892
     1:  1.                   1:  -0.577215664901532

         1. RET                   p 16 RET X

Here's the complete sequence of keystrokes:

     C-x ( Z `  s 1  0 t 2
                RET 5 a <  Z [  5 Z (  & s + 2  1 s + 1  1 Z ) r 1  Z ]
                L r 1 2 * & - r 2 -
                2 RET 40  Z (  RET k b TAB RET r 1 TAB ^ * /
                               TAB RET M-TAB - RET M-TAB a = Z /
                       2  Z )
           Z '
     C-x )