Algorithms, Blockchain and Cloud

Teaching Kids Programming – Area and Circumferences of Circle and Monte Carlo Simulation Algorithm of PI


Teaching Kids Programming: Videos on Data Structures and Algorithms

The Math constant PI or is an irrational number which cannot be represented by a simple fraction where . The approximation of is 3.14159265358…. A Rational Number is the opposite – where the number can be represented as a simple fraction p/q.

The is defined as the ration of circumferences (perimeter) to diameter. A diameter is twice size of a circle’s radius.

and Thus we know

Visual Proof of Area of a Circle

We can cut the circle into as many thin rings as possible, then if we put them from the smallest to the largest it will be a triangle. The large side will be the circumference of the circle while the other side will the the radius – where we cut and unroll.

The area will thus be i.e.

Introduction to Monte Carlo Simulation

We ask the computer to randomly throw as many coins as possible on to the following 1×1 square, and then we can count how many of them fall onto the pizza (1/4 of circle). The more points we throw the better approximation of the 1/4 pi.

The randomness is provided by the random package and the random() method returns a fraction random value between 0 to 1 i.e. [0, 1)

from random import random

def pi(n):
    inside = 0
    for _ in range(n):
        x = random()
        y = random()
        if x * x + y * y <= 1:
            inside += 1
    return inside/n*4
    
print(pi(1000))    # 3.184
print(pi(10000))   # 3.1288
print(pi(100000))  # 3.1436

Monte Carlo Simulation Algorithms to compute the Pi based on Randomness:

–EOF (The Ultimate Computing & Technology Blog) —

1092 words
Last Post: Teaching Kids Programming - Remove a Node and Subtree using Depth First Search or Breadth First Search Algorithm
Next Post: Teaching Kids Programming - Divide and Conquer Algorithm Explained (Max Number, Ruler Marker)

The Permanent URL is: Teaching Kids Programming – Area and Circumferences of Circle and Monte Carlo Simulation Algorithm of PI (AMP Version)

Exit mobile version