Pi Day 2016

×

Error message

Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in menu_set_active_trail() (line 2396 of /homepages/8/d424690692/htdocs/clickandbuilds/Drupal/Drupal7/includes/menu.inc).

Every year during the 14th of March we celebrate Pi day. This is due to the fact that Pi is close to 3.14, but what is it's true value and how can we compute it? In this article you'll learn two ways of approximating this number!

  • Pi is an irrational number used in math to represent the ratio of the circumference of a circle to its diameter. We can access an approximation of this constant in javascript with Math.PI.
  • The approximation we will compute is based on the probability to get a point inside a circle. As we know that the ratio of the areas is Pi, we will be simulating a certain number of points, and check whether the point lies in the circle or not. The code to do so can be found here:
         
        function distance(x, y) {
    	return getSquareRoot(x * x + y * y)
       }
    
       var aux = 0
       repeat (20011) {
    	goTo(Math.random() * 400 - 200, Math.random() * 400 - 200)
    	if (distance(getX(), getY()) <= 200) {
    		setColor("green")
    		aux++
    	} else {
    		setColor("red")
    	}
    	arc(1)
       }
       setSize(40)
       setColor("black")
       writeAt(4 * aux / 20011, -180, 0)
    
         
        

 

Can you find more ways to approximate Pi? Send us your ideas at info@eseecode.com, and happy Pi day!