Dennis
Dennis

Reputation: 175

index 100 is out of bounds for axis 0 with size 100

I was trying to write a programme using python to compute the infinite series of

1/1^2 + 1/2^2 + 1/3^2 +1/4^2 +.....

my code is as follows:

n = 100
x = np.zeros([n])
x[0] = 0
for i in range(n):
   x[i+1] = x[i] + 1/float((i+1)**2)
print x[99]

when I tried to execute the code, it returned something as:

IndexError: index 100 is out of bounds for axis 0 with size 100

I would like to know what is wrong with the code. Thanks!:)

Upvotes: 2

Views: 16572

Answers (1)

tlysoo
tlysoo

Reputation: 171

Index i goes up to 99 so you are trying to get x[i + 1] == x[100] at the last iteration, so it can not work because x goes up to x[99]

In your for loop just do range(n - 1)

Upvotes: 9

Related Questions