#!/usr/bin/perl ### #generate $ARGV[0] digits of pi. #the algorithm is here: #http://crd.lbl.gov/~dhbailey/ #### #written by Pete_I with help from BillN1VUX(from freenode) #### #edited to make more efficient use of memory use strict; use warnings; use Math::BigFloat; my $DIGS = $ARGV[0] || 50; Math::BigFloat->div_scale($DIGS+10); my $pi = Math::BigFloat->new(); for(0 .. $DIGS) { $pi->badd( get_digit($_) ); } print "\n", $pi->round($DIGS) . "\n"; sub get_digit { my $k = Math::BigFloat->new(shift); ( 1 / 16 ** $k ) * ( (4 / (8 * $k + 1))- (2 / (8 * $k + 4))- (1 / (8 * $k + 5))- (1 / (8 * $k + 6)) ) } __DATA__ digits of pi to compare accuracy 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282