turns printfs back on
[freebsd-src/fkvm-freebsd.git] / contrib / bc / Examples / pi.b
blob0d840cf795ee420e8fdf9c8f7e9f1be4b5fa3b0d
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.
5    The results are left in the global variable digits.
6    digits[0] is the number of 0's in PI.
8    This program requires the math library.
9 */
11 define pi () {
12   auto ix, pi, save_scale, work;
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;
21   print "\nCounting digits. . .";
22   for (ix = 0; ix < 10; ix++) digits[ix] = 0;
24   /* Extract the One's digit from pi. */
25   scale = 0;
26   one_digit = work / 1;
28   for (ix = save_scale; ix > 0; ix--) {
30     /* Remove the One's digit and multiply by 10. */
31     scale = ix;
32     work = (work - one_digit) / 1 * 10;
34     /* Extract the One's digit. */
35     scale = 0;
36     one_digit = work / 1;
38     digits[one_digit] += 1;
39   }
41   /* Restore the scale. */
42   scale = save_scale;
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   }
52   print "\n\n"