Last active
October 25, 2021 08:27
-
-
Save zeroflag/676591cfefb918d5c177384f8d899355 to your computer and use it in GitHub Desktop.
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
import sys | |
from random import random | |
from operator import add | |
from pyspark.sql import SparkSession | |
if __name__ == "__main__": | |
""" | |
Usage: pi [partitions] | |
""" | |
spark = SparkSession\ | |
.builder\ | |
.appName("PythonPi")\ | |
.getOrCreate() | |
partitions = int(sys.argv[1]) if len(sys.argv) > 1 else 2 | |
n = 100000 * partitions | |
def f(_): | |
x = random() * 2 - 1 | |
y = random() * 2 - 1 | |
return 1 if x ** 2 + y ** 2 <= 1 else 0 | |
count = spark.sparkContext.parallelize(range(1, n + 1), partitions).map(f).reduce(add) | |
print("Pi is roughly %f" % (4.0 * count / n)) | |
spark.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment