From 429c66a831f50630e413e90695c8b1fcf71d5e82 Mon Sep 17 00:00:00 2001 From: antoniomolinabravo <85772354+antoniomolinabravo@users.noreply.github.com> Date: Tue, 28 Feb 2023 20:49:41 -0300 Subject: [PATCH] =?UTF-8?q?Actualizaci=C3=B3n=20de=20calculo=20Pi()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit solución con menos iteraciones, utilizando los promedios entre pares e impares se aproxima mas rápido al numero exacto --- Chapter1/calculating_pi.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Chapter1/calculating_pi.py b/Chapter1/calculating_pi.py index 61abfc6..d4c6daa 100644 --- a/Chapter1/calculating_pi.py +++ b/Chapter1/calculating_pi.py @@ -14,18 +14,37 @@ # See the License for the specific language governing permissions and # limitations under the License. +# solucion con menos iteraciones +# tengo otra version mas simplificada pero la estoy probando, que calcula a la iteracion 30 y otra a la 10 def calculate_pi(n_terms: int) -> float: numerator: float = 4.0 denominator: float = 1.0 operation: float = 1.0 pi: float = 0.0 + + pi_ant1: float = 4 # mem_pi_prev + pi_ant2: float = 0 # men_pi_prev_prev + pi_prom1: float = 0 # pi_mean_1 + pi_prom2: float = 0 # pi_mean_2 + for _ in range(n_terms): + pi_ant2 = pi_ant1 # backup1 + pi_ant1 = pi # backup2 pi += operation * (numerator / denominator) denominator += 2.0 operation *= -1.0 - return pi + pi_prom1 = (pi + pi_ant1) /2 # pi_mean_1 + pi_prom2 = (pi_ant1 + pi_ant2) /2 # pi_mean_2 + pi = (pi_prom1 + pi_prom2) /2 # pi_mean(pi_means) + + return pi if __name__ == "__main__": - print(calculate_pi(1000000)) + #print(calculate_pi(350)) # PAR:POR ABAJO IMPAR:POR ARIBA + pi1 = calculate_pi(99) # impar + pi2 = calculate_pi(100) # par + print(pi1) + print(pi2) + print( (pi1 + pi2) /2 ) # pi_mean(means)