Last active
June 6, 2021 05:40
-
-
Save AdroitAnandAI/0ba9e3b0b5e505060941e540d2707374 to your computer and use it in GitHub Desktop.
To compute pi value
This file contains 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
# Compute value of PI using Monte Carlo Approach | |
# Source: http://spark.apache.org/examples.html | |
NUM_SAMPLES = 1000000 | |
def inside(p): | |
x, y = random.random(), random.random() | |
return x*x + y*y < 1 | |
# filter: http://spark.apache.org/docs/latest/api/python/pyspark.html | |
count = sc.parallelize(range(0, NUM_SAMPLES),10) \ | |
.filter(inside).count() | |
print("Pi is roughly %f" % (4.0 * count / NUM_SAMPLES)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment