From a0298d36b6f00df94cd49f5b143871ea9871c937 Mon Sep 17 00:00:00 2001 From: abhitrueprogrammer <67090539+abhitrueprogrammer@users.noreply.github.com> Date: Sun, 10 Apr 2022 18:10:26 +0530 Subject: [PATCH] Update pi.py Took the "factor" term outside the braket of infinite summation so that they all have to be added only once --- code/pi.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/code/pi.py b/code/pi.py index 971fdb5..bc9e7e1 100644 --- a/code/pi.py +++ b/code/pi.py @@ -36,13 +36,14 @@ def estimate_pi(): while True: num = factorial(4*k) * (1103 + 26390*k) den = factorial(k)**4 * 396**(4*k) - term = factor * num / den - total += term + + total += num / den + term = factor * num/den if abs(term) < 1e-15: break k += 1 - - return 1 / total + + return 1 / (factor * total) print(estimate_pi())