-2

I made a code to try and compute the partial sum of a series, but it keeps producing a result that's about 0.01 off. It looks pretty straightforward to me and I haven't been able to spot what my mistake might've been, but there's definitely something off.

import math
def f(n):
    s = 0
    for x in range(1,n+1):
        y = x*(4**x)
        d = (math.factorial(x))*(4**x)
        n = -1**x
        s += n/d
    print(s)

f(7)
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141

1 Answers1

1

As written, your function computes a good approximation of - (√√e - 1) ~ -0.28402541668774148407342056806244.

By applying the fix suggested by jason, the constant is 1/√√e - 1 ~ -0.22119921692859513175482973302168.

The discrepancy is larger than 0.01. Given the abnormal assignment to y, we can suspect that you didn't post the right code.