Skip to content

Create maths/pi_generator.py #8666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 18, 2023
Merged
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d809eed
Create pi_generator.py
JulianStiebler Apr 17, 2023
8347256
Update pi_generator.py
JulianStiebler Apr 17, 2023
4440fd9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 17, 2023
f110e05
Update pi_generator.py
JulianStiebler Apr 17, 2023
5ad9894
Merge branch 'master' of https://github.com/JulianStiebler/Python
JulianStiebler Apr 17, 2023
5ac3220
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 17, 2023
1a2d82e
Update pi_generator.py
JulianStiebler Apr 17, 2023
fa2d5b0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 17, 2023
451e0be
Update pi_generator.py
JulianStiebler Apr 17, 2023
608296e
Merge branch 'master' of https://github.com/JulianStiebler/Python
JulianStiebler Apr 17, 2023
116d7e5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 17, 2023
f9bb5b6
Update pi_generator.py
JulianStiebler Apr 17, 2023
e27d251
Merge branch 'master' of https://github.com/JulianStiebler/Python
JulianStiebler Apr 17, 2023
5dcceb5
Update pi_generator.py
JulianStiebler Apr 17, 2023
70c490a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 17, 2023
43efcb0
Updated commentary on line 28, added math.pi comparison & math.isclos…
JulianStiebler Apr 18, 2023
63b60dc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Apr 18, 2023
d93f580
Removed # noqa: E501
JulianStiebler Apr 18, 2023
f9703f0
Merge branch 'master' of https://github.com/JulianStiebler/Python
JulianStiebler Apr 18, 2023
f3ff728
printf() added as recommended by cclaus
JulianStiebler Apr 18, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions maths/pi_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import doctest
def calculate_pi(limit) -> str:
"""
https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
Leibniz Formula for Pi

The Leibniz formula is the special case arctan 1 = 1/4 Pi .
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.
For simplicity' sake, let's just compare against known values.

>>> calculate_pi(15)
'3.141592653589793'

To further proof, since we cannot predict errors or interrupt an infinite generation
or interrupt any alternating series, here some more tests.

>>> calculate_pi(50)
'3.14159265358979323846264338327950288419716939937510'

>>> calculate_pi(80)
'3.14159265358979323846264338327950288419716939937510582097494459230781640628620899'
"""
q = 1
r = 0
t = 1
k = 1
n = 3
l = 3
decimal = limit
counter = 0

result = ""

"""
We will avoid using yield since we otherwise get a Generator-Object,
which we cant just compare against anything. We would have to make a list out of it
after the generation, so we will just stick to plain return logic:
"""
while counter != decimal + 1:
if 4 * q + r - t < n * t:
result += str(n)
if counter == 0:
result += "."

if decimal == counter:
break

counter += 1
nr = 10 * (r - n * t)
n = ((10 * (3 * q + r)) // t) - 10 * n
q *= 10
r = nr
else:
nr = (2 * q + r) * l
nn = (q * (7 * k) + 2 + (r * l)) // (t * l)
q *= k
t *= l
l += 2
k += 1
n = nn
r = nr
return result


def main() -> None:
pi_digits = calculate_pi(50)
print(pi_digits)
doctest.testmod()


if __name__ == "__main__":
main()