Square pyramidal numbers A000330 - OEIS

Task

Your task is simple, generate a sequence that, given index i, the value on that position is the sum of squares from 0 upto i where i &gt;= 0.

Example:

Input: 0
Output: 0           (0^2)

Input: 4
Output: 30          (0^2 + 1^2 + 2^2 + 3^2 + 4^2)

Input: 5
Output: 55          (0^2 + 1^2 + 2^2 + 3^2 + 4^2 + 5^2)


Specification:


You may take no input and output the sequence indefinitely;
You may take input N and output the Nth element of the sequence;
You may take input N and output the first N elements of the sequence.
Python 2, 22 bytes



lambda n:n*~n*~(n*2)/6


Try it online!

This uses the closed-form formula n * (n+1) * (2*n+1) / 6. The code performs the following operations:


Multiplies n by (n*):


The bitwise complement of n (~n), which essentially means -1-n. 
And by the bitwise complement of 2n (*~(n*2)), which means -1-2n.

Divides by 6 (/6).


Python 2, 27 bytes



f=lambda n:n and f(n-1)+n*n


Try it online!

Saved 1 byte thanks to Rod and 1 thanks to G B.