Created
December 20, 2011 15:51
-
-
Save mcrisc/1502035 to your computer and use it in GitHub Desktop.
Estimates the value of PI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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
commented
Dec 21, 2011
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment