Created
December 12, 2019 15:08
-
-
Save rishi93/a93f288ef42ca785a0575c705165576d to your computer and use it in GitHub Desktop.
Daily Coding Problem - 14
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
""" | |
This problem was asked by Google. | |
The area of a circle is defined as πr^2. | |
Estimate π to 3 decimal places using a Monte Carlo method. | |
Hint: The basic equation of a circle is x2 + y2 = r2. | |
""" | |
import random | |
# Generate one million (x, y) points. They lie in the quadrant | |
# x and y both lie in [0, 1) | |
points_inside_circle = 0 | |
points_outside_circle = 0 | |
for i in range(1000000): | |
x = random.random() | |
y = random.random() | |
# Check if they lie inside the circle | |
if x**2 + y**2 < 1: | |
points_inside_circle += 1 | |
# Since we considered a quadrant: | |
# points_inside/total_points = pi/4 | |
print(points_inside_circle/1000000 * 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment