All problems
Error Function Series
Problem
Evaluate the error function with its Maclaurin series to n terms:
erf(x) ≈ (2 / sqrt(pi)) * sum from k = 0 to n-1 of (-1)^k * x^(2k+1) / (k! * (2k+1))
Accumulate the terms iteratively. Large x or small n will be inaccurate because the series converges slowly.
Hint
Each term changes by a factor of -x^2 / (k+1); divide by 2k+1 before adding.
Test cases
case 1
in: [0,5]
expected: 0
case 2
in: [1,20]
expected: 0.8427007929497149
case 3
in: [-1,20]
expected: -0.8427007929497149
case 4
in: [0.5,15]
expected: 0.5204998778130466
Starter codePython
def erf_series(x, n):
# Your code here
pass4 test cases · run your code against them in the editor