use bignum qw/p -20/; my ($pi,$n) = ('3+1/(X)',1500); $pi =~ s{X}{ '6'. ( $_!=$n ?'+'.((2*$_+1)**2).'/(X)' :'' ) }e for(1..$n); print eval $pi;