Skip to content

Instantly share code, notes, and snippets.

@mcrisc
Created December 20, 2011 15:51
Show Gist options
  • Select an option

  • Save mcrisc/1502035 to your computer and use it in GitHub Desktop.

Select an option

Save mcrisc/1502035 to your computer and use it in GitHub Desktop.
Estimates the value of PI
#coding: utf-8
# Estimates the value of PI
# Based on example described at http://code.google.com/edu/parallel/mapreduce-tutorial.html#Basics
# Author: Marcelo Criscuolo (Jaú), with
# invaluable contributions from Rafael Giusti (Argentino)
import math
from datetime import datetime
RADIUS = 50000 # the bigger the number, more precise is the estimation
SQ_RADIUS = RADIUS ** 2
t0 = datetime.now()
x = RADIUS
y = 0
area_section = 0
while x > 0:
sq_y = y ** 2
while (sq_y + (x ** 2)) > SQ_RADIUS:
x -= 1
area_section += x
y += 1
area_circle = 4 * area_section
area_square = (2 * RADIUS) ** 2
pi = 4.0 * area_circle / area_square
t1 = datetime.now()
print pi, '\t\testimated'
print math.pi, '\t\tfrom math.pi'
print abs(pi - math.pi)
print (t1 - t0)
@mcrisc

mcrisc commented Dec 21, 2011

Copy link
Copy Markdown
Author
RADIUS = 50000
3.1415924176            estimated
3.14159265359          from math.pi
2.3598979304e-07
0:00:00.110000         time (less than 1sec)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment