For my Computational Physics class we have to compute the Madelung Constant for NaCl. My code to do this uses three nested for loops and therefore runs very slowly. I was wondering if there was a way to use arrays or some other method to increase the speed of computation. Thanks
from math import sqrt
l= int(input("The lattice size is :"))
M = 0.0
for i in range(-L,L+1):
for j in range(-L,L+1):
for k in range(-L,L+1):
if not (i==j==k==0):
M += ((-1)**(i+j+k+1))/sqrt(i*i +j*j +k*k)
print("The Madelung constant for NaCl with lattice size",l,"is",M)