## Python 2, 45 bytes <!-- language: lang-py --> lambda a,b:(a+~b)*(a-b)*(3*(a+b)**2+a-b-2)/12 Closed form solution - not the shortest, but I thought it'd be worth posting anyway. ## Explanation Let `p(n)` be the *n*th [square pyramidal number][1], and `t(n)` be the *n*th [triangular number][2]. Then, for *n* over the range *a*, ..., *b*: * ∑n = `t(b)-t(a-1)`, and * ∑n² = `p(b) - p(a-1)` * So (∑n)²-∑n² = `(t(b)-t(a-1))² - (p(b) - p(a-1))`. This expression reduces to that in the code. [1]: https://en.wikipedia.org/wiki/Square_pyramidal_number [2]: https://en.wikipedia.org/wiki/Triangular_number