Have a mince pi:
import random
def mincepi( num_iters=42 ):
f = 0.0
for x in xrange( num_iters ):
if random.randrange( 2 ): f += 1.0 / ( 2*x + 1 )
else: f -= 1.0 / ( 2*x + 1 )
return 4*f
Yes, it's a randomised version of Leibniz' approximation of pi.
Brought to you by the Dept. of Wasted CPU Cycles.
Tags: