Help with speeding up this code

3 posts were split to a new topic: Question about sum(x → …, 1:n) notation