-
-
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 2 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,98 @@ | ||
def calculate_Pi(limit) -> str: | ||
""" | ||
https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 | ||
Leibniz Formula for Pi | ||
|
||
Leibniz formula for Pi, named after Gottfried Leibniz, states that | ||
1 - 1/3 + 1/5 - 1/7 + 1/9 - ... = Pi/4 | ||
an alternating series. | ||
|
||
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) | ||
However, the Leibniz formula can be used to calculate Pi to high precision using various convergence acceleration techniques. | ||
For example, the Shanks transformation, Euler transform or Van Wijngaarden transformation, which are general methods for alternating series, | ||
can be applied effectively to the partial sums of the Leibniz series. | ||
|
||
Further, combining terms pairwise gives the non-alternating series | ||
Pi/4 = ∑n=0^infinity * ( 1 / 4n+1 - 1/4n+3) = ∑n=0^infinity * 2/((4n+1)*(4n+3) | ||
which can be evaluated to high precision from a small number of terms using Richardson extrapolation or the Euler-Maclaurin formula. | ||
This series can also be transformed into an integral by means of the Abel-Plana formula and evaluated using techniques for numerical integration. | ||
|
||
math.pi gives us a constant with 16 digits, excluding the known leading 3, | ||
since our algorithm always gives the leading 3. ofc, we need to check for 15 numbers. | ||
|
||
We cannot try to prove against an interrupted, uncompleted generation. | ||
https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80#Unusual_behaviour | ||
If the series is truncated at the right time, the decimal expansion of the approximation will agree | ||
with that of Pi for many more digits, except for isolated digits or digit groups. For example, | ||
taking five million terms yields 3.141592(4)5358979323846(4)643383279502(7)841971693993(873)058 ... | ||
Where as each (number) is incorrect. | ||
|
||
The errors can in fact be predicted; but those calculations also approach infinity for accuracy. | ||
For simplicity' sake, let's just compare it against known standing 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(100) | ||
'3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679' | ||
|
||
""" | ||
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 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: | ||
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) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() | ||
#import doctest | ||
#doctest.testmod() |
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.