#!/usr/bin/env python # Written by Jason Jung # Last modified: 2006.10.04 # This is a simple script that uses the formula # pi = 16 * arctan(5, y) - 4 * arctan(239, y) # to calculate the value of Pi to any number of digits # The formulas for Pi were supplied by works of # Carey Bloodworth and the users of Wikipedia # USAGE: ./machin.py # TEST RUN TIME # real 1388m51.658s # user 1223m32.380s # sys 120m20.907s # OS: Linux 2.6.15-26-386/i686 # CPU: mobile AMD Athlon(tm) 4 Processor (500.095 MHz) def arctan(x, y): sum = xpow = y // x z = 3 sign = -1 while 1: xpow = xpow // (x * x) term = xpow // z if not term: break sum += sign * term sign = -sign z += 2 return sum def pi(n): y = 10**(n + 10) pi = 16 * arctan(5, y) - 4 * arctan(239, y) return pi // 10**10 if __name__ == '__main__': from sys import argv print pi(int(argv[1]))