(* :Author: Christopher Moretti *) (* :Summary: This package contains the command PiByRelativelyPrime,which estimates pi by estimating the probability that 2 integers have no common factor. *) (* :History: Version 1.0 by Christopher Moretti, 1997 *) PiByRelativelyPrime[n_,ran_]:= Block[ {i}, i=0; Do[ If[ GCD[Random[Integer,{-ran,ran}],Random[Integer,{-ran,ran}]]==1, i=i+1];,{ j,1,n}]; Print["Out of ",n," random pairs of integers, ",i, " were relatively prime."]; Print["The percentage of relatively prime pairs is approximately ", N[100 i/n]]; Print["The approximation to Pi is ",N[Sqrt[6n/i],20]] ];