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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 |
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:
- Teaching Kids Programming – Area and Circumferences of Circle and Monte Carlo Simulation Algorithm of PI
- Using Parallel For in Java to Compute PI using Monte Carlo Algorithm
- Monte Carlo solution for Mathematics × Programming Competition #7
- R Programming Tutorial – How to Compute PI using Monte Carlo in R?
- C++ Coding Exercise – Parallel For – Monte Carlo PI Calculation
- Area of the Shadow? – The Monte Carlo Solution in VBScript
- VBScript Coding Exercise – Compute PI using Monte Carlo Random Method
- Computing Approximate Value of PI using Monte Carlo in Python
- How does the 8-bit BASIC perform on Famicom Clone – Subor SB2000 – FBasic – Compute PI approximation using Monte-Carlo method
- GoLang: Compute the Math Pi Value via Monte Carlo Simulation
- BASH Script to Compute the Math.PI constant via Monte Carlo Simulation
–EOF (The Ultimate Computing & Technology Blog) —
loading...
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)