1/*
2   This is a program to determine the distribution of digits in the
3   fraction part of PI.   It will look at the first scale digits.
4
5   The results are left in the global variable digits.
6   digits[0] is the number of 0's in PI.
7
8   This program requires the math library.
9*/
10
11define pi () {
12  auto ix, pi, save_scale, work;
13
14  save_scale = scale;
15  scale += 5;
16  print "\n\nCalculating PI to ",scale," digits.  Please wait . . .";
17  pi = 4*a(1);
18  scale -= 5;
19  work = pi;
20
21  print "\nCounting digits. . .";
22  for (ix = 0; ix < 10; ix++) digits[ix] = 0;
23
24  /* Extract the One's digit from pi. */
25  scale = 0;
26  one_digit = work / 1;
27
28  for (ix = save_scale; ix > 0; ix--) {
29
30    /* Remove the One's digit and multiply by 10. */
31    scale = ix;
32    work = (work - one_digit) / 1 * 10;
33
34    /* Extract the One's digit. */
35    scale = 0;
36    one_digit = work / 1;
37
38    digits[one_digit] += 1;
39  }
40
41  /* Restore the scale. */
42  scale = save_scale;
43
44  /* Report. */
45  print "\n\n"
46  print "PI to ", scale, " digits is:\n", pi/1, "\n\n"
47  print "The frequency of the digits are:\n"
48  for (ix = 0; ix < 10; ix++) {
49    print "    ", ix, " - ", digits[ix], " times\n"
50  }
51
52  print "\n\n"
53}
54