maths.pi_generator

Functions

calculate_pi(→ str)

https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80

main(→ None)

Module Contents

maths.pi_generator.calculate_pi(limit: int) str

https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 Leibniz Formula for Pi

The Leibniz formula is the special case arctan(1) = pi / 4. Leibniz’s formula converges extremely slowly: it exhibits sublinear convergence.

Convergence (https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80#Convergence)

We cannot try to prove against an interrupted, uncompleted generation. https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80#Unusual_behaviour The errors can in fact be predicted, but those calculations also approach infinity for accuracy.

Our output will be a string so that we can definitely store all digits.

>>> import math
>>> float(calculate_pi(15)) == math.pi
True

Since we cannot predict errors or interrupt any infinite alternating series generation since they approach infinity, or interrupt any alternating series, we’ll need math.isclose()

>>> math.isclose(float(calculate_pi(50)), math.pi)
True
>>> math.isclose(float(calculate_pi(100)), math.pi)
True

Since math.pi contains only 16 digits, here are some tests with known values:

>>> calculate_pi(50)
'3.14159265358979323846264338327950288419716939937510'
>>> calculate_pi(80)
'3.14159265358979323846264338327950288419716939937510582097494459230781640628620899'
maths.pi_generator.main() None