-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
d809eed
Create pi_generator.py
JulianStiebler 8347256
Update pi_generator.py
JulianStiebler 4440fd9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f110e05
Update pi_generator.py
JulianStiebler 5ad9894
Merge branch 'master' of https://github.com/JulianStiebler/Python
JulianStiebler 5ac3220
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1a2d82e
Update pi_generator.py
JulianStiebler fa2d5b0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 451e0be
Update pi_generator.py
JulianStiebler 608296e
Merge branch 'master' of https://github.com/JulianStiebler/Python
JulianStiebler 116d7e5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f9bb5b6
Update pi_generator.py
JulianStiebler e27d251
Merge branch 'master' of https://github.com/JulianStiebler/Python
JulianStiebler 5dcceb5
Update pi_generator.py
JulianStiebler 70c490a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 43efcb0
Updated commentary on line 28, added math.pi comparison & math.isclos…
JulianStiebler 63b60dc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d93f580
Removed # noqa: E501
JulianStiebler f9703f0
Merge branch 'master' of https://github.com/JulianStiebler/Python
JulianStiebler f3ff728
printf() added as recommended by cclaus
JulianStiebler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
def 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 = 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' | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
JulianStiebler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 can't 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: | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
JulianStiebler marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
JulianStiebler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pi_digits = calculate_pi(50) | ||
print(pi_digits) | ||
JulianStiebler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import doctest | ||
doctest.testmod() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.