Skip to content

Commit

Permalink
Actualización de calculo Pi()
Browse files Browse the repository at this point in the history
solución con menos iteraciones, utilizando los promedios entre pares e impares se aproxima mas rápido al numero exacto
  • Loading branch information
antoniomolinabravo authored Feb 28, 2023
1 parent 8a164b6 commit 429c66a
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Chapter1/calculating_pi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

1 comment on commit 429c66a

@antoniomolinabravo
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incluso se puede hacer:

pi1 = calculate_pi(34)  # impar
pi2 = calculate_pi(35) # par
print(pi1)
print(pi2)
print( (pi1 + pi2) /2 ) # pi_mean(means)

davecom#3.1415787467182046 (34)
davecom#3.141605369457352 (35)
davecom#3.1415920580877783 mean()

Please sign in to comment.