pocketlang/test/examples/pi.pk

18 lines
276 B
Plaintext
Raw Normal View History

## PI approximation using Leibniz formula.
##
## PI/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 - ...
from math import abs
pi_by_4 = 0; sign = -1
for i in 1..10000
sign *= -1
pi_by_4 += sign * 1/(2*i - 1)
end
PI = 4 * pi_by_4
assert(abs(PI - 3.141593) < 0.0002)
print('PI =', PI)