Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Github repo cloud-examples: https://github.com/sagemath/cloud-examples

Views: 8037
License: MIT

Different Approximations of π\pi

# Sage's value n(pi, digits = 100)
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
# using mpmath - http://code.google.com/p/mpmath/ import mpmath as mm mm.mp.dps = 100 print mm.pi print 2*mm.asinh(1j).imag print mm.gamma(0.5)**2 print mm.sqrt(6*mm.zeta(2)) print mm.quad(lambda x: 4*mm.sqrt(1-x**2), [0, 1]) print mm.quad(lambda x: mm.exp(-x**2), [-mm.inf, mm.inf]) ** 2 print mm.nsum(lambda n: 4*(-1)**n/(2*n+1), [0, mm.inf]) print mm.limit(lambda n: 2**(4*n+1)*mm.factorial(n)**4/(2*n+1)/mm.factorial(2*n)**2, mm.inf) print mm.findroot(mm.sin, 3.14)
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068
# dropping points import numpy as np N = 10000 x, y = 2 * np.random.rand(2, N) - 1 inside = np.sum(np.sqrt(x^2 + y^2) <= 1) list_plot(zip(x, y), color='gray', size = 1) + circle((0,0), 1, color= 'green') print("pi ~ %f" % (float(inside) / N * 4))
pi ~ 3.135200