#!/usr/bin/perl -w # Time-stamp: <18 Feb 2000 18:03 charpov@cs.unh.edu> # without these two lines, the pi number of this program is 2.8784916685157 # computes the pi approximation of a file # uses the fact that the probability for two integers to be coprime # is 6/pi^2 use strict; # assume the first line is one (the first test will be positive, and $p will # be nonzero). my ($n, $p, $l) = (0, 0, 1); # for all nonempty and nonnull lines while (<>) { # conversion: returns (in hexa) the bits in the line seen as a number # chop the \n at the end of lines (if not, lines are never coprime) # dc wants hexa numbers to be uppercased chomp; $_ = uc unpack "H*", $_; if (not /^0*$/) { # test the current line with the previous one; increase by one the # number of tests; increase the number of positive tests if numbers # are coprime # package Math::BigInt is so slow that it's more efficient to fork and # ask dc to compute the gcd $n++; $p++ if (`echo "16i$l $_ sa[sblalbdsa%d0<.]ds.xlap" | /usr/bin/dc` eq "1\n"); $l = "$_"; # print the current approximation of pi # print "($p/$n)\n"; # for debug print (sqrt (6.0*$n/$p)."\n"); } }