Skip to main content
4 of 4
Commonmark migration

Python 2, 45 bytes

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 nth square pyramidal number, and t(n) be the nth triangular number. 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.

Sp3000
  • 61.9k
  • 13
  • 115
  • 287