Skip to main content
2 of 4
added 268 characters in body
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

Python 3, 28 bytes

(Assuming code-golf is the winning criteria, but also efficient at O(1).)

lambda n:n*~-n*-~n*(n/4+1/6)

Try it online!


Python 2, 29 bytes: lambda n:n*~-n*-~n*(3*n+2)/12

Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282