Docsity

X27663 Slow pi approximation, Ejercicios de Informática

Toggle sidebar
bg1
Write a function slow_pi_approx(n) that given a non negative integer n
computes 4∑k = 0n (−1)k/2k + 1 The returned value has to be rounded to the
ten thousandth by using the python function round(_,4).#
def slow_pi_approx(n):
'''
>>> slow_pi_approx(10)
3.2323
>>> slow_pi_approx(100)
3.1515
>>> slow_pi_approx(1000)
3.1426
'''
total = 0
for x in range(n+1):
total += ((-1)**x)/(2*x+1)
return round(4*total,4)
if __name__ == '__main__':
import doctest
print(doctest.testmod())

Vista previa parcial del texto

¡Descarga X27663 Slow pi approximation y más Ejercicios en PDF de Informática solo en Docsity!

Write a function slow_pi_approx(n) that given a non negative integer n

computes 4∑ k = 0 n (−1) k /2 k + 1 The returned value has to be rounded to the

ten thousandth by using the python function round(_,4).

def slow_pi_approx(n): _'''

slow_pi_approx(10)

slow_pi_approx(100)

slow_pi_approx(1000)

'''_ total = 0 for x in range(n+1): total += ((-1)x)/(2x+1) return round(4total, 4 ) if name == 'main': import doctest print(doctest.testmod())