Blame Examples/pi.b

Packit 70b277
/*
Packit 70b277
   This is a program to determine the distribution of digits in the
Packit 70b277
   fraction part of PI.   It will look at the first scale digits.
Packit 70b277
Packit 70b277
   The results are left in the global variable digits.
Packit 70b277
   digits[0] is the number of 0's in PI.
Packit 70b277
Packit 70b277
   This program requires the math library.
Packit 70b277
*/
Packit 70b277
Packit 70b277
define pi () {
Packit 70b277
  auto ix, pi, save_scale, work;
Packit 70b277
Packit 70b277
  save_scale = scale;
Packit 70b277
  scale += 5;
Packit 70b277
  print "\n\nCalculating PI to ",scale," digits.  Please wait . . .";
Packit 70b277
  pi = 4*a(1);
Packit 70b277
  scale -= 5;
Packit 70b277
  work = pi;
Packit 70b277
Packit 70b277
  print "\nCounting digits. . .";
Packit 70b277
  for (ix = 0; ix < 10; ix++) digits[ix] = 0;
Packit 70b277
Packit 70b277
  /* Extract the One's digit from pi. */
Packit 70b277
  scale = 0;
Packit 70b277
  one_digit = work / 1;
Packit 70b277
Packit 70b277
  for (ix = save_scale; ix > 0; ix--) {
Packit 70b277
Packit 70b277
    /* Remove the One's digit and multiply by 10. */
Packit 70b277
    scale = ix;
Packit 70b277
    work = (work - one_digit) / 1 * 10;
Packit 70b277
Packit 70b277
    /* Extract the One's digit. */
Packit 70b277
    scale = 0;
Packit 70b277
    one_digit = work / 1;
Packit 70b277
Packit 70b277
    digits[one_digit] += 1;
Packit 70b277
  }
Packit 70b277
Packit 70b277
  /* Restore the scale. */
Packit 70b277
  scale = save_scale;
Packit 70b277
Packit 70b277
  /* Report. */
Packit 70b277
  print "\n\n"
Packit 70b277
  print "PI to ", scale, " digits is:\n", pi/1, "\n\n"
Packit 70b277
  print "The frequency of the digits are:\n"
Packit 70b277
  for (ix = 0; ix < 10; ix++) {
Packit 70b277
    print "    ", ix, " - ", digits[ix], " times\n"
Packit 70b277
  }
Packit 70b277
Packit 70b277
  print "\n\n"
Packit 70b277
}