Pi Estimation

[ ]:
import pycompss.interactive as ipycompss
[ ]:
ipycompss.start(graph=True)
[ ]:
from dds import DDS
[ ]:
def inside(_):
    """Check if inside.

    :return: If inside.
    """
    import random
    rand_x = random.random()
    rand_y = random.random()
    return (rand_x * rand_x) + (rand_y * rand_y) < 1

[ ]:
import time

def pi_estimation():
    """Pi estimation.

    Example is taken from: https://spark.apache.org/examples.html

    :return: If the pi value calculated is between 3.1 and 3.2.
    """
    start = time.time()

    print("--- PI ESTIMATION ---")

    print("- Estimating Pi by 'throwing darts' algorithm.")
    tries = 100000
    print(f"- Number of tries: {tries}")

    count = DDS().load(range(0, tries), 10).filter(inside).count()
    rough_pi = 4.0 * count / tries

    print(f"- Pi is roughly {rough_pi}")
    print("- Elapsed Time: ", time.time() - start)
    print("---------------------")

    return 3.1 < rough_pi < 3.2
[ ]:
pi_estimation()
[ ]:
ipycompss.stop()