From d809eed0144926bf87a0730b6eaa437a7a626b0b Mon Sep 17 00:00:00 2001 From: JulianStiebler <68881884+JulianStiebler@users.noreply.github.com> Date: Mon, 17 Apr 2023 20:50:45 +0200 Subject: [PATCH 01/16] Create pi_generator.py --- maths/pi_generator.py | 98 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 maths/pi_generator.py diff --git a/maths/pi_generator.py b/maths/pi_generator.py new file mode 100644 index 000000000000..1599011f2294 --- /dev/null +++ b/maths/pi_generator.py @@ -0,0 +1,98 @@ +def calculate_Pi(limit): + """ + 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' + + """ + 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(): + pi_digits = calculate_Pi(50) + print(pi_digits) + + +if __name__ == "__main__": + main() + #import doctest + #doctest.testmod() From 8347256787c82a9345d7b345970776eebb0c6d1b Mon Sep 17 00:00:00 2001 From: JulianStiebler <68881884+JulianStiebler@users.noreply.github.com> Date: Mon, 17 Apr 2023 20:53:33 +0200 Subject: [PATCH 02/16] Update pi_generator.py --- maths/pi_generator.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index 1599011f2294..edde62fcc1ea 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -1,4 +1,4 @@ -def calculate_Pi(limit): +def calculate_Pi(limit) -> str: """ https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 Leibniz Formula for Pi @@ -87,7 +87,7 @@ def calculate_Pi(limit): return result -def main(): +def main() -> None: pi_digits = calculate_Pi(50) print(pi_digits) From 4440fd9558add5d3510fa26370a3d1db219380a5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Apr 2023 18:56:26 +0000 Subject: [PATCH 03/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/pi_generator.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index edde62fcc1ea..52873d5d74d9 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -4,32 +4,32 @@ def calculate_Pi(limit) -> str: 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. + 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. + 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. - + 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. + 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, + 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, + 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. @@ -44,7 +44,7 @@ def calculate_Pi(limit) -> str: >>> calculate_Pi(100) '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679' - + """ q = 1 r = 0 @@ -94,5 +94,5 @@ def main() -> None: if __name__ == "__main__": main() - #import doctest - #doctest.testmod() + # import doctest + # doctest.testmod() From f110e05b8912fd7c246d6dd1bd4e5080acdb3003 Mon Sep 17 00:00:00 2001 From: JulianStiebler <68881884+JulianStiebler@users.noreply.github.com> Date: Mon, 17 Apr 2023 21:04:27 +0200 Subject: [PATCH 04/16] Update pi_generator.py --- maths/pi_generator.py | 52 ++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 35 deletions(-) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index edde62fcc1ea..b186c649d617 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -1,50 +1,30 @@ -def calculate_Pi(limit) -> str: +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. + 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) + 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. + 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) + >>> calculate_pi(50) '3.14159265358979323846264338327950288419716939937510' - >>> calculate_Pi(100) - '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679' - + >>> calculate_pi(80) + '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899' """ q = 1 r = 0 @@ -58,8 +38,9 @@ def calculate_Pi(limit) -> str: 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: + 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: @@ -88,7 +69,8 @@ def calculate_Pi(limit) -> str: def main() -> None: - pi_digits = calculate_Pi(50) + pi_digits = calculate_pi(50) + print(pi_digits) From 5ac3220f87b158a8abf98b99f3645292256ca15f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Apr 2023 19:06:58 +0000 Subject: [PATCH 05/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/pi_generator.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index bcd629cdfdf3..9e3388485a4e 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -39,7 +39,7 @@ def calculate_pi(limit) -> str: """ 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 + 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: From 1a2d82eefccf11a009bd45158c8811b61db98f6e Mon Sep 17 00:00:00 2001 From: JulianStiebler <68881884+JulianStiebler@users.noreply.github.com> Date: Mon, 17 Apr 2023 21:08:47 +0200 Subject: [PATCH 06/16] Update pi_generator.py --- maths/pi_generator.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index 9e3388485a4e..4820b06c3d7d 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -1,3 +1,4 @@ +import doctest def calculate_pi(limit) -> str: """ https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 @@ -70,11 +71,9 @@ def calculate_pi(limit) -> str: def main() -> None: pi_digits = calculate_pi(50) - print(pi_digits) + doctest.testmod() if __name__ == "__main__": main() - # import doctest - # doctest.testmod() From fa2d5b018a4b3ea91fb60dfe69091649e323f430 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Apr 2023 19:09:19 +0000 Subject: [PATCH 07/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/pi_generator.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index 4820b06c3d7d..88c039e770be 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -1,4 +1,6 @@ import doctest + + def calculate_pi(limit) -> str: """ https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 From 451e0beb1b098f201cccff5c097032b15c452c4e Mon Sep 17 00:00:00 2001 From: JulianStiebler <68881884+JulianStiebler@users.noreply.github.com> Date: Mon, 17 Apr 2023 21:10:16 +0200 Subject: [PATCH 08/16] Update pi_generator.py --- maths/pi_generator.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index 4820b06c3d7d..4e90bbcdc515 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -1,4 +1,3 @@ -import doctest def calculate_pi(limit) -> str: """ https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 @@ -72,6 +71,8 @@ def calculate_pi(limit) -> str: def main() -> None: pi_digits = calculate_pi(50) print(pi_digits) + + import doctest doctest.testmod() From 116d7e5e81a2d7e87c07bbe9797ad2933e15960c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Apr 2023 19:11:24 +0000 Subject: [PATCH 09/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/pi_generator.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index 4e90bbcdc515..c2b2b0fb6701 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -73,6 +73,7 @@ def main() -> None: print(pi_digits) import doctest + doctest.testmod() From f9bb5b663388da365d85df255dd109998c2be286 Mon Sep 17 00:00:00 2001 From: JulianStiebler <68881884+JulianStiebler@users.noreply.github.com> Date: Mon, 17 Apr 2023 21:21:50 +0200 Subject: [PATCH 10/16] Update pi_generator.py --- maths/pi_generator.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index 4e90bbcdc515..251504865369 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -1,4 +1,4 @@ -def calculate_pi(limit) -> str: +def calculate_pi(limit: int) -> str: """ https://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80 Leibniz Formula for Pi @@ -39,7 +39,7 @@ def calculate_pi(limit) -> str: """ 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 + 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: @@ -71,7 +71,6 @@ def calculate_pi(limit) -> str: def main() -> None: pi_digits = calculate_pi(50) print(pi_digits) - import doctest doctest.testmod() From 5dcceb5dcef2deeb80a82fb4d08398d029f109d2 Mon Sep 17 00:00:00 2001 From: JulianStiebler <68881884+JulianStiebler@users.noreply.github.com> Date: Mon, 17 Apr 2023 21:23:43 +0200 Subject: [PATCH 11/16] Update pi_generator.py --- maths/pi_generator.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index 14164008a976..5617906779ac 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -67,12 +67,10 @@ def calculate_pi(limit: int) -> str: r = nr return result - def main() -> None: pi_digits = calculate_pi(50) print(pi_digits) import doctest - doctest.testmod() From 70c490ae28f92691a4195f58d14ca5221338208e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 17 Apr 2023 19:24:14 +0000 Subject: [PATCH 12/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/pi_generator.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index 5617906779ac..14164008a976 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -67,10 +67,12 @@ def calculate_pi(limit: int) -> str: r = nr return result + def main() -> None: pi_digits = calculate_pi(50) print(pi_digits) import doctest + doctest.testmod() From 43efcb098b87893cc0e27ecbd2c5733393e6375d Mon Sep 17 00:00:00 2001 From: JulianStiebler <68881884+JulianStiebler@users.noreply.github.com> Date: Tue, 18 Apr 2023 11:37:54 +0200 Subject: [PATCH 13/16] Updated commentary on line 28, added math.pi comparison & math.isclose() test --- maths/pi_generator.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index 14164008a976..d5ff2ac8d542 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -12,20 +12,33 @@ def calculate_pi(limit: int) -> str: 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' + 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. - To further proof, since we cannot predict errors or interrupt an infinite generation - or interrupt any alternating series, here some more tests. + >>> import math + >>> float(calculate_pi(15)) == math.pi + True + + To further proof, 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() + + >>> math.isclose(float(calculate_pi(50)), math.pi) + True + + >>> math.isclose(float(calculate_pi(100)), math.pi) + True + + Since the math.pi-constant is only 16 digits long, here some test with preknown values: >>> calculate_pi(50) '3.14159265358979323846264338327950288419716939937510' - >>> calculate_pi(80) '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899' - """ + + In the Leibniz formula for calculating pi, the variables q, r, t, k, n, and l are used for the iteration process. + """ # noqa: E501 q = 1 r = 0 t = 1 @@ -72,7 +85,6 @@ def main() -> None: pi_digits = calculate_pi(50) print(pi_digits) import doctest - doctest.testmod() From 63b60dc2365545631bf7d173af9cc9b043c227bb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 18 Apr 2023 09:40:28 +0000 Subject: [PATCH 14/16] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/pi_generator.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index d5ff2ac8d542..3b7afe39e48e 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -85,6 +85,7 @@ def main() -> None: pi_digits = calculate_pi(50) print(pi_digits) import doctest + doctest.testmod() From d93f5806186b3b5c46802bb234053369e99aeb0e Mon Sep 17 00:00:00 2001 From: JulianStiebler <68881884+JulianStiebler@users.noreply.github.com> Date: Tue, 18 Apr 2023 11:47:17 +0200 Subject: [PATCH 15/16] Removed # noqa: E501 --- maths/pi_generator.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index d5ff2ac8d542..4557d74327f3 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -21,7 +21,8 @@ def calculate_pi(limit: int) -> str: >>> float(calculate_pi(15)) == math.pi True - To further proof, since we cannot predict errors or interrupt any infinite alternating series generation since they approach infinity, + 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() >>> math.isclose(float(calculate_pi(50)), math.pi) @@ -30,15 +31,16 @@ def calculate_pi(limit: int) -> str: >>> math.isclose(float(calculate_pi(100)), math.pi) True - Since the math.pi-constant is only 16 digits long, here some test with preknown values: + Since math.pi-constant contains only 16 digits, here some test with preknown values: >>> calculate_pi(50) '3.14159265358979323846264338327950288419716939937510' >>> calculate_pi(80) '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899' - In the Leibniz formula for calculating pi, the variables q, r, t, k, n, and l are used for the iteration process. - """ # noqa: E501 + To apply the Leibniz formula for calculating pi, + the variables q, r, t, k, n, and l are used for the iteration process. + """ q = 1 r = 0 t = 1 From f3ff72842d0df5b0726cc02fdb07f0c699724ea4 Mon Sep 17 00:00:00 2001 From: JulianStiebler <68881884+JulianStiebler@users.noreply.github.com> Date: Tue, 18 Apr 2023 11:50:18 +0200 Subject: [PATCH 16/16] printf() added as recommended by cclaus --- maths/pi_generator.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maths/pi_generator.py b/maths/pi_generator.py index 0494f08c44c4..dcd218aae309 100644 --- a/maths/pi_generator.py +++ b/maths/pi_generator.py @@ -84,8 +84,7 @@ def calculate_pi(limit: int) -> str: def main() -> None: - pi_digits = calculate_pi(50) - print(pi_digits) + print(f"{calculate_pi(50) = }") import doctest doctest.testmod()