Pi Day 2016

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!

Similar Posts

  • Taller Jornades de Pensament Computacional a l’educació matemàtica

    Adjunt teniu els arxius que treballarem al taller. En aquest taller, utilitzarem els llenguatges de programació eSeeCode i Pyturtle per a treballar en reptes de matemàtiques concrets. Aquests casos reals que hem treballat amb alumnes ens permetran abordar els conceptes bàsics del pensament computacional i explorarem com, amb només unes poques instruccions, podem realitzar grans…

  • Greatest Common Divisor

    The greatest common divisor between two numbers is defined as the largest value that can divide both numbers. To compute the greatest common divisor there are many algorithms. One of the fastest is over 2 millennium old. It’s called Euclid’s algorithm. Here you will be able to learn more about this algorithm and how to…

  • Explorium

    Benvinguts al taller de robòtica! Fent un quadrat Clicant en aquest enllaç podreu accedir a la plataforma de programació eSeeCode . =0){divElement.className=divElement.className.replace(‘glyphicon-minus’,’glyphicon-plus’);}else{divElement.className=divElement.className.replace(‘glyphicon-plus’,’glyphicon-minus’);}”> Utilitzant bucles Podem simplificar el codi anterior utilitzant un bucle. Els bucles poden ser amb comptadors (per repetir un nombre de cops) o amb condicions (mentre es compleixi una condició). Els primers són…

  • GCD2

    The greatest common divisor between two numbers is defined as the largest value that can divide both numbers. To compute the greatest common divisor there are many algorithms. One of the fastest is over 2 millennium old. It’s called Euclid’s algorithm. Here you will be able to learn more about this algorithm and how to…

  • Recursive problems

      Fibonacci spiral ‌ Recursive function A recursive function is a function that calls itself. We already saw an example of recursive program when we did the Greatest common divisor algorithm. Another typical example is the factorial of a number. The factorial of a number n is the product of 1*2..(n-1)*n. And it is written…