<jupyter_start><jupyter_text>Daily Coading Problem 14 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. Monte Carlo methods rely on random sampling. In this case, if we take a cartesian plane and inscribe a circle with radius r inside a square with lengths 2r, then the area of the circle will be πr2 while the area of the square will be (2r)2 = 4r2. Then, the ratio of the areas of the circle to the square is π / 4.So, what we can do is the following:* Set r to be 1 (the unit circle)* Randomly generate points within the square with corners (-1, -1), (1, 1), (1, -1), (-1, 1)* Keep track of the points that fall inside and outside the circle       * You can check whether a point (x, y) is inside the circle if x2 + y2 < r2, which is another way of                representing a circle* Divide the number of points that fall inside the circle to the total number of points -- that should give us an   approximation of π / 4.<jupyter_code>from random import uniform
from math import pow

def generate():
    return (uniform(-1, 1), uniform(-1, 1))

def is_in_circle(coords):
    return coords[0] * coords[0] + coords[1] * coords[1] < 1

def estimate():
    iterations = 10000000
    in_circle = 0
    for _ in range(iterations):
        if is_in_circle(generate()):
            in_circle += 1
            
    pi_over_four = in_circle / iterations
    return pi_over_four * 4<jupyter_output><empty_output><jupyter_text>Note that this doesn't give a perfect approximation -- we need more iterations to get a closer estimate. We want the digits of pi up to 3 decimal places. This translates to an error of < 10^(-3). The error scales with the square root of the number of guesses, which means we need 10^6 iterations to get to our desired precision. If we want more precision, we'll have to crank up the iterations.This problem _is_ embarrassingly parallel. None of the estimations have any dependent computations, so we can parallelize this problem easily -- divide up the workload into P processes you have, and then add up all the points in the circle in the end. Extra credit: make this program multi-process.<jupyter_code>from random import random
radius = 2


def estimate_pi(num_random_tests):
    pi_counter = 0
    rsquared = radius ** 2
    for _ in range(num_random_tests):
        x_rand = random() * radius
        y_rand = random() * radius
        if (x_rand ** 2) + (y_rand ** 2) < rsquared:
            pi_counter += 1

    return 4 * pi_counter / num_random_tests

#assert round(estimate_pi(100000000), 3) == 3.141<jupyter_output><empty_output><jupyter_text>Method for below implimentationThe Monte Carlo method is based on statistical probability to calculate the number Pi. Accordingly:* Suppose we have a 1x1 square objective board. The center is the origin O(0,0). From the origin we draw a circle   inscribed in the square.* A gunner fires a dart at the target board (Assuming all hit the target but can be in or out of the circle.)* After n shots, we can count k shots inside the circle. The formula for calculating pi is pi = (k/n)/4.* The larger n is, the closer we get to pi.<jupyter_code>go on this link and check the image

https://res.cloudinary.com/deop9ytsv/image/upload/v1586329960/e9xI1.gif
from random import random

def estimate_pi():
    nThrows = 0
    nSuccess = 0
    x, y

    for i in range(0,1000000):

        x = Math.random()     #Throw a dart
        y = Math.random()
        nThrows+=1

        if ( x*x + y*y <= 1 ):
            nSuccess+=1
        return ("Pi/4 = " + nSuccess/nThrows )
        return ("Pi = " + 4* nSuccess/nThrows )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07491335643828312, 'fraction_numerical': 0.02879232204745401, 'mean_word_length': 3.2929061784897025, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '598614', 'license': 'MIT', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1079, 'n_words': 528, 'path': 'Daily Coading Problem 14.ipynb', 'repo_name': 'thecrood/Daily-Coading-Problem-14', 'size': '3751'}