mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-07 03:07:46 +08:00
[pre-commit.ci] pre-commit autoupdate (#9543)
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.0.291 → v0.0.292](https://github.com/astral-sh/ruff-pre-commit/compare/v0.0.291...v0.0.292) - [github.com/codespell-project/codespell: v2.2.5 → v2.2.6](https://github.com/codespell-project/codespell/compare/v2.2.5...v2.2.6) - [github.com/tox-dev/pyproject-fmt: 1.1.0 → 1.2.0](https://github.com/tox-dev/pyproject-fmt/compare/1.1.0...1.2.0) * updating DIRECTORY.md * Fix typos in test_min_spanning_tree_prim.py * Fix typos * codespell --ignore-words-list=manuel --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
![66853113+pre-commit-ci[bot]@users.noreply.github.com](/assets/img/avatar_default.png)
committed by
GitHub

parent
60291738d2
commit
895dffb412
@ -3,60 +3,53 @@ 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 .
|
||||
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.
|
||||
The errors can in fact be predicted, but those calculations also approach infinity
|
||||
for accuracy.
|
||||
|
||||
Our output will always be a string since we can defintely store all digits in there.
|
||||
For simplicity' sake, let's just compare against known values and since our outpit
|
||||
is a string, we need to convert to float.
|
||||
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 are going to need math.isclose()
|
||||
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-constant contains only 16 digits, here some test with preknown values:
|
||||
Since math.pi contains only 16 digits, here are some tests with known values:
|
||||
|
||||
>>> calculate_pi(50)
|
||||
'3.14159265358979323846264338327950288419716939937510'
|
||||
>>> calculate_pi(80)
|
||||
'3.14159265358979323846264338327950288419716939937510582097494459230781640628620899'
|
||||
|
||||
To apply the Leibniz formula for calculating pi,
|
||||
the variables q, r, t, k, n, and l are used for the iteration process.
|
||||
"""
|
||||
# Variables used for the iteration process
|
||||
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:
|
||||
"""
|
||||
# We can't compare against anything if we make a generator,
|
||||
# so we'll stick with plain return logic
|
||||
while counter != decimal + 1:
|
||||
if 4 * q + r - t < n * t:
|
||||
result += str(n)
|
||||
|
Reference in New Issue
Block a user