Skip to main content
Commonmark migration
Source Link

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

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.

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.

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.

Add explanation, taken from comments
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41

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.

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.

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.

added 1 character in body
Source Link
Sp3000
  • 61.9k
  • 13
  • 115
  • 287

Python 2, 45 bytes

lambda a,b:(a+~b)*(a-b)*(3*(a+b)**2+a-b-2)/12

Not actually sure if that'sClosed form solution - not the shortest, but I thought it'd be worth posting anyway.

Python 2, 45 bytes

lambda a,b:(a+~b)*(a-b)*(3*(a+b)**2+a-b-2)/12

Not actually sure if that's the shortest, but thought it'd be worth posting anyway.

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.

Source Link
Sp3000
  • 61.9k
  • 13
  • 115
  • 287
Loading