The optimal gravitational softening length for cosmological N-body simulations