Page 1 of 1

python question import sympy def estimate_pi_chudnovsky(n): # YOUR CODE HERE # Test that the function returns a floa

Posted: Wed Apr 27, 2022 3:11 pm
by answerhappygod
python question
Python Question Import Sympy Def Estimate Pi Chudnovsky N Your Code Here Test That The Function Returns A Floa 1
Python Question Import Sympy Def Estimate Pi Chudnovsky N Your Code Here Test That The Function Returns A Floa 1 (113.68 KiB) Viewed 225 times
Python Question Import Sympy Def Estimate Pi Chudnovsky N Your Code Here Test That The Function Returns A Floa 2
Python Question Import Sympy Def Estimate Pi Chudnovsky N Your Code Here Test That The Function Returns A Floa 2 (67.68 KiB) Viewed 225 times
import sympy
def estimate_pi_chudnovsky(n):
# YOUR CODE HERE
# Test that the function returns a float
import sympy
from nose.tools import assert_equal
assert_equal(type(estimate_pi_chudnovsky(10)),sympy.core.numbers.Float)
# Test that the return value has at least 1000 digits
import sympy
from nose.tools import assert_equal
assert_equal(len(str(estimate_pi_chudnovsky(10)))>1000,True)
# Test that two terms give 41 correct digits
from nose.tools import assert_almost_equal
import sympy
assert_almost_equal(estimate_pi_chudnovsky(2),sympy.pi.evalf(100),delta=1e-41)
# Check that ValueError is raised for incorrect input.
from nose.tools import assert_raises
assert_raises(ValueError,estimate_pi_chudnovsky,-1)
from nose.tools import assert_raises

# Test that two terms give 41 correct digits
from nose.tools import assert_almost_equal
import sympy
It is possible to use different methods to compute approximations to a. One way is to use a so-called Monte Carlo method which uses random numbers to estimate n. Consider the figure below, which shows the unit disc inscribed in the square (-1, 1] x [-1, 1]. Monte Carlo Approximation to n 1.0 10 0.5 0.5 0.0 0.0 -0.5 -0.5 -1.0 -1.0 -0.5 0.0 0.5 10 х -1.0 Pae -1.0 -0.5 0.0 0.5 10 Denote the areas of the square and disc A, and Ad, respectively. Then of course As = 4 and Ad = i So T Ad A 4 In the right-hand figure, a set of N total random points (uniformly distributed) have been plotted on the unit square. The points inside and outside the disc are coloured green and red, respectively. If the number of points inside the disc are denoted N disc , then the ratio of Ndisc to N total is an approximation of the ratio of areas above, namely N disc TT N disc >= 4 Ntotal 4 N total
(d) [30 marks] There are many many ways to approximate n. One of the most efficient algorithms is using the following partial sums: 00 (-1){(6k)!(13591409 + 545140134k) Sn = 12 Σ (3k)!(k!)3640320 3k+3/2 k=0 It is know that Sn as n → and the convergence is very rapid. This series was discovered by the Chudnovsky brothers in 1988 and uses an approach first discovered by Ramanujan. Versions of this algorithm are used for all record-breaking calculations for digits of t. Write a function estimate_pi_chudnovsky which uses this formula to approximate n. The function should use SymPy , take a non-negative integer n as input and return an estimate of a given as a NumPy Float with at least 1000 digits precision. Your function should also raise a ValueError if the input is not a non- negative integer.