- 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.
Note that Scratch uses double-precision floating point numbers which limits the precision to about 10 decimal places.[citation needed] For more precise representations special handling is needed, see How to Use Large Numbers in Scratch.
Gregory-Leibniz Series Method
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.
Nilakantha Series Method
| 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.
Archimedes' Method
| 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.
Newtons' Method
| 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. As the examples shown above run forever, the custom block can't be used directly as it expects the script to finish. Instead, compute it in batches:
when green flag clicked set [Pi v] to (3) forever batch calculate :: custom end define batch calculate // run without screen refresh repeat (100) ... // script to calculate pi goes here end
Fractional Representations
Some fractions are approximately equal to pi. A well known example is . The fraction is sufficient for the most precise representation (as a double-precision floating point number).[1]
set [Pi v] to ((245850922) / (78256779))