- 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 places). 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
This method is more complicated, but arrives on pi quicker. |
Below is another method for calculating pi:
when green flag 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
This method is simpler and does the calculation instantly. |
The higher that the variable "n" is, the closer to pi the output will be.
when green flag clicked 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.
If "n" is too big, it will show that pi is equal 0.
Method Four
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 green flag 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