For more information, see Approximations of π on Wikipedia. Pi (π) is the ratio between the circumference of a circle and the diameter of the circle, which is approximately 3.14159 (rounded to 5 decimal points). This article will show different methods to approximate pi.
Method One
The script below is one method to calculate pi:
when green flag clicked set [pi v] to (4) set [Refreshes v] to (1) forever change [Refreshes v] by (2) set [pi v] to ((pi) - ((4)/(Refreshes))) change [Refreshes v] by (2) set [pi v] to ((pi) + ((4)/(Refreshes))) end
This script will constantly make the variable pi closer and closer to the actual number π.
This is called the Gregory-Leibniz series.
Method Two
Note: | This method is more complicated, but arrives on pi quicker. |
Below is another method for calculating pi:
when gf clicked set [pi v] to (3) set [Refreshes v] to (2) forever set [pi v] to ((pi) + ((4)/((Refreshes) * (((Refreshes) + (1)) * ((Refreshes) + (2)))))) change [Refreshes v] by (2) set [pi v] to ((pi) - ((4)/((Refreshes) * (((Refreshes) + (1)) * ((Refreshes) + (2)))))) change [Refreshes v] by (2) end
This is referred to as the Nilakantha series.
Method Three
Note: | This method is simpler and does the calculation instantly. |
The higher that the variable "n" is, the closer to pi the output will be.
ask [Please enter n:] and wait set [n v] to (answer) set [pi v] to ((n) * ([sin v] of ((180) / (n)):: operators ))
This method was used by Archimedes to calculate the original pi. This method can never actually get the exact value of pi, but like all the other methods it can give enough precision to be used in a project.
Method Four
Note: | This method doesn't need to run forever. |
The script below is another method to calculate pi:
define calculate pi set [x v] to [1] repeat (4) cosine (x) radians change [x v] by ((cos) / ([sqrt v] of ((1) - ((cos) * (cos))))) end set [pi v] to ((x) * (2)) define cosine (x) radians set [n v] to [0] set [term v] to [1] set [cos v] to [1] repeat (10) change [n v] by (2) set [term v] to (((-1) * (term)) * (((x) * (x)) / (((n) - (1)) * (n)))) change [cos v] by (term) end
Running calculate pi::custom
will make (pi)
equal π.
This method, called Newton's method, looks for a value for x that makes the cosine function equal zero, and it finds cos(π∕₂) = 0 .
Speeding up the Program
One way to make the program much faster is to use a run without screen refresh custom block, however this may cause issues on some mobile devices. To do this, see below:
For Method One
when green flag clicked set [pi v] to (4) set [Refreshes v] to (1) forever Update::custom end define Update // Run without screen refresh repeat (100) change [Refreshes v] by (2) set [pi v] to ((pi) - ((4)/(Refreshes))) change [Refreshes v] by (2) set [pi v] to ((pi) + ((4)/(Refreshes))) end
For Method Two
when gf clicked set [pi v] to (3) set [Refreshes v] to (2) forever Update method 2 :: custom end define Update method 2 //Run without screen refresh repeat (100) set [pi v] to ((pi) + ((4)/((Refreshes) * (((Refreshes) + (1)) * ((Refreshes) + (2)))))) change [Refreshes v] by (2) set [pi v] to ((pi) - ((4)/((Refreshes) * (((Refreshes) + (1)) * ((Refreshes) + (2)))))) change [Refreshes v] by (2) end