Skip to content

Instantly share code, notes, and snippets.

@zeroflag
Last active October 25, 2021 08:27
Show Gist options
  • Save zeroflag/676591cfefb918d5c177384f8d899355 to your computer and use it in GitHub Desktop.
Save zeroflag/676591cfefb918d5c177384f8d899355 to your computer and use it in GitHub Desktop.
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