There are many ways to calculate pi (π) which is defined by Wikipedia below:
“ | The number π (/paɪ/) is a mathematical constant. It is defined as the ratio of a circle's circumference to its diameter, and it also has various equivalent definitions. It appears in many formulas in all areas of mathematics and physics. It is approximately equal to 3.14159. It has been represented by the Greek letter "π" since the mid-18th century, and is spelled out as "pi". It is also referred to as Archimedes' constant. | ” |
Here are just of few of the methods that have been proposed to calculate this value.
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 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
![]() | 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
![]() | 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(π∕2) = 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