[Question]
  [
[A000330 - OEIS](https://oeis.org/A000330)
# 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 >= 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.
  
[Answer]
# [Python 2](https://docs.python.org/2/), 22 bytes
```
lambda n:n*~n*~(n*2)/6
```
[Try it online!](https://tio.run/##K6gsycjPM/qfZhvzPycxNyklUSHPKk@rDog08rSMNPXN/qflFylkKmTmKRQl5qWnahgaaFpxKRQUZeaVKGTqKOnaKemkaWRq/gcA "Python 2 – 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](https://docs.python.org/2/), 27 bytes
```
f=lambda n:n and f(n-1)+n*n
```
[Try it online!](https://tio.run/##BcFBCoAgEAXQfacYWmkpZEuh7mLY1EB9Rdx0@um9@vW7YFXl7UnvkRMhghIysYEPdsYE5dJISEAt4TpNWGwcqDZBJ3Gj30fHRqz@ "Python 2 – Try It Online")
Saved 1 byte thanks to [Rod](https://codegolf.stackexchange.com/users/47120/rod) and 1 thanks to [G B](https://codegolf.stackexchange.com/users/18535/g-b).
[Answer]
# JavaScript (ES6), 16 bytes
```
n=>n*(n+++n)*n/6
```
### Demo
```
let f =
n=>n*(n+++n)*n/6
for(n = 0; n < 10; n++) {
  console.log('a(' + n + ') = ' + f(n))
}
```
### How?
The expression `n+++n` is parsed as `n++ + n` (1). Not that it really matters because `n + ++n` would also work in this case.
Therefore:
```
n*(n+++n)*n/6 =
n * (n + (n + 1)) * (n + 1) / 6 =
n * (2 * n + 1) * (n + 1) / 6
```
which [evaluates to **sum(k=0...n)(k²)**](https://oeis.org/A000330).
---
*(1) This can be verified by doing `n='2';console.log(n+++n)` which gives the integer `5`, whereas `n + ++n` would give the string `'23'`.*
[Answer]
# [MATL](https://github.com/lmendo/MATL), 3 bytes
```
:Us
```
... or them?
[**Try it online!**](https://tio.run/##y00syfn/3yq0@P9/EwA "MATL – Try It Online")
### Explanation
```
:Us
:    % Implicit input n. Push range [1 2 ... n]
 U   % Square, element-wise
  s  % Sum of array. Implicit display
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 3 bytes
```
ÝnO
```
[Try it online!](https://tio.run/##MzBNTDJM/f//8Nw8////TQA "05AB1E – Try It Online")
**Explanation**
```
  O    # sum of
 n     # squares of
Ý      # range [0 ... input]
```
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 36 bytes
```
({<(({}[()])())>{({})({}[()])}{}}{})
```
[Try it online!](https://tio.run/##SypKzMzTTctJzP7/X6PaRkOjujZaQzNWU0NT064ayNGECdRW1wKR5v//pgA "Brain-Flak – Try It Online")
```
# Main algorithm
(                                  )  # Push the sum of:
                {({})({}[()])}{}      #   The square of:
 {                              }     #     0 to i 
# Stuff for the loop
  <(({}[()])())>                      # Push i-1, i without counting it in the sum
                                 {}   # Pop the counter (0)
```
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 34 bytes
```
({(({}[()])()){({}[()])({})}{}}{})
```
[Try it online!](https://tio.run/##SypKzMzTTctJzP7/X6NaQ6O6NlpDM1ZTQ1OzGs6urtWsra4FIs3//00B "Brain-Flak – Try It Online")
# How does it work?
Initially I had the same idea as [Riley](https://codegolf.stackexchange.com/a/145553/56656)1 but it felt wrong to use a zeroer. I then realized that
```
{({}[()])({})}{}
```
Calculates n2 - n.
Why? Well we know 
```
{({})({}[()])}{}
```
Calculates n2 and loops n times. That means if we switch the order of the two pushes we go from increasing the sum by n + (n-1) each time to increasing the sum by (n-1) + (n-1) each time. This will decrease the result by one per loop, thus making our result n2 - n. At the top level this -n cancels with the n generated by the push that we were zeroing alleviating the need for a zeroer and saving us two bytes.
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 36 bytes
```
({({})(({}[()])){({})({}[()])}{}}{})
```
[Try it online!](https://tio.run/##SypKzMzTTctJzP7/X6Nao7pWUwNIRGtoxmpqQrhQXm11LRBp/v9vCgA "Brain-Flak – Try It Online")
Here is another solution, its not as golfy but it's pretty strange so I thought I would leave it as a challenge to figure out how it works.
If you are not into Brain-Flak but you still want the challenge here it is as a summation.
![Picture](https://a-ta.co/mathjax/!XGRpc3BsYXlzdHlsZVxzdW1fe2k9MX1ebiBcbGVmdCggKDJpIC0gMSkgKyBcc3VtX3tqPTF9XntpLTF9ICggMmotMSApIFxyaWdodCk=.svg)
---
1: I came up with my solution before I looked at the answers here. So no plagiarism here.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 3 bytes
```
R²S
```
[Try it online!](https://tio.run/##y0rNyan8/z/o0Kbg////mwAA "Jelly – Try It Online")
FGITW
# Explanation
```
R²S  Main Link
R    Generate Range
 ²   Square (each term)
  S  Sum
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 3 bytes
```
ṁ□ḣ
```
[Try it online!](https://tio.run/##yygtzv7//@HOxkfTFj7csfj///@mAA "Husk – Try It Online")
[Answer]
## [Hexagony](https://github.com/m-ender/hexagony), 23 bytes
```
?'+)=:!@/*"*'6/{=+'+}/{
```
[Try it online!](https://tio.run/##y0itSEzPz6v8/99eXVvT1krRQV9LSUvdTL/aVltdu1a/@v9/03/5BSWZ@XnF/wE "Hexagony – Try It Online")
### Explanation
Unfolded:
```
   ? ' + )
  = : ! @ /
 * " * ' 6 /
{ = + ' + } /
 { . . . . .
  . . . . .
   . . . .
```
This is really just a linear program with the `/` used for some redirection. The linear code is:
```
?'+){=+'+}*"*'6{=:!@
```
Which computes **n (n+1) (2n+1) / 6**. It uses the following memory edges:
[![enter image description here](https://i.stack.imgur.com/OSVZf.png)](https://i.stack.imgur.com/OSVZf.png)
Where the memory point (MP) starts on the edge labelled **n**, pointing north.
```
?   Read input into edge labelled 'n'.
'   Move MP backwards onto edge labelled 'n+1'.
+   Copy 'n' into 'n+1'.
)   Increment the value (so that it actually stores the value n+1).
{=  Move MP forwards onto edge labelled 'temp' and turn around to face
    edges 'n' and 'n+1'.
+   Add 'n' and 'n+1' into edge 'temp', so that it stores the value 2n+1.
'   Move MP backwards onto edge labelled '2n+1'.
+   Copy the value 2n+1 into this edge.
}   Move MP forwards onto 'temp' again.
*   Multiply 'n' and 'n+1' into edge 'temp', so that it stores the value
    n(n+1).
"   Move MP backwards onto edge labelled 'product'.
*   Multiply 'temp' and '2n+1' into edge 'product', so that it stores the
    value n(n+1)(2n+1).
'   Move MP backwards onto edge labelled '6'.
6   Store an actual 6 there.
{=  Move MP forwards onto edge labelled 'result' and turn around, so that
    the MP faces edges 'product' and '6'.
:   Divide 'product' by '6' into 'result', so that it stores the value
    n(n+1)(2n+1)/6, i.e. the actual result.
!   Print the result.
@   Terminate the program.
```
In theory it might be possible to fit this program into side-length 3, because the `/` aren't needed for the computation, the `:` can be reused to terminate the program, and some of the `'"=+*{` might be reusable as well, bringing the number of required commands below 19 (the maximum for side-length 3). I doubt it's possible to find such a solution by hand though, if one exists at all.
[Answer]
# [Ohm v2](https://github.com/MiningPotatoes/Ohm), 3 bytes
```
#²Σ
```
[Try it online!](https://tio.run/##y8/INfr/X/nQpnOL//83@Q8A "Ohm v2 – Try It Online")
**Explanation**
```
#    Range
 ²   Square
  Σ  Sum
```
[Answer]
# [Japt](https://github.com/ETHproductions/japt), 3 bytes
```
ô²x
```
[Try it here.](https://ethproductions.github.io/japt/?v=1.4.5&code=9LJ4&input=NQ==)
-1 thanks to [Shaggy](https://codegolf.stackexchange.com/users/58974/shaggy).
Explanation:
```
ò²x 
ô²  Map square on [0..input]
  x Sum
```
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 46 bytes
```
{(({})[()])}{}{({({})({}[()])}{}<>)<>}<>({{}})
```
[Try it online!](https://tio.run/##SypKzMzTTctJzP7/v1pDo7pWM1pDM1aztrq2WqMaxAVimIiNnaaNHZDUqK6urdX8/98UAA "Brain-Flak – Try It Online")
[Answer]
# [CJam](https://sourceforge.net/p/cjam), 10 bytes
```
ri),{_*+}*
```
[**Try it online!**](https://tio.run/##S85KzP3/vyhTU6c6Xku7Vuv/fxMA "CJam – Try It Online")
```
ri            e# Input integer n
  )           e# Add 1
   ,          e# Range [0 1 ... n]
    {   }*    e# Fold (reduce)
     _        e# Duplicate
      *       e# Multiply
       +      e# Add
```
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 14 bytes
```
Tr[Range@#^2]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n6Zg@z@kKDooMS891UE5zihW7X9AUWZeiYK@g0IaiKg20VEwrf3/3wQA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 5 bytes
```
⟦^₂ᵐ+
```
[Try it online!](https://tio.run/##SypKTM6ozMlPN/r//9H8ZXGPmpoebp2g/f@/6f8oAA "Brachylog – Try It Online")
### Explanation
```
⟦        Range
 ^₂ᵐ     Map square
    +    Sum
```
[Answer]
# [Actually](https://github.com/Mego/Seriously), 3 bytes
```
R;*
```
[Try it online!](https://tio.run/##S0wuKU3Myan8/z/IWuv/fxMA "Actually – Try It Online")
Takes `N` as input, and outputs the `N`th element in the sequence.
Explanation:
```
R;*
R    range(1, N+1) ([1, 2, ..., N])
 ;*  dot product with self
```
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), ~~7~~ 5 bytes
*2 bytes saved thanks to @Mego*
```
+.×⍨⍳
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R2wRtvcPTH/WueNS7GchXOARiGBoAAA)
**How?**
`⍳` - range
`+.×` - dot product
`⍨` - with itself
[Answer]
## R, 17 bytes
```
sum((0:scan())^2)
```
Pretty straightforward, it takes advantage from the fact that `^` (exponentiation) is vectorized in **R**.
[Answer]
## [CJam](https://sourceforge.net/p/cjam), 9 bytes
```
ri),_.*:+
```
[Try it online!](https://tio.run/##S85KzP3/vyhTUydeT8tK@/9/EwA "CJam – Try It Online")
### Explanation
```
ri        e# Read input and convert to integer N.
  ),      e# Get range [0 1 2 ... N].
    _     e# Duplicate.
     .*   e# Pairwise products, giving [0 1 4 ... N^2].
       :+ e# Sum.
```
Alternatively:
```
ri),2f#:+
```
This squares each element by mapping `2#` instead of using pairwise products. And just for fun another alternative which gets inaccurate for large inputs because it uses floating-point arithmetic:
```
ri),:mh2#
```
[Answer]
# [Julia](http://julialang.org/), ~~16~~ 14 bytes
*2 bytes saved thanks to @MartinEnder*
```
!n=(x=1:n)⋅x
```
[Try it online!](https://tio.run/##yyrNyUw0/f9fMc9Wo8LW0CpP81F3a8X/tPwihUwFWwVDK0MDLs6Cosy8kpw8DcVMTa7UvJT/AA)
**How?**
`(x=1:n)` creates a range of `1` to `n` and assign to `x`, `⋅` dot product with `x`.
[Answer]
## [Labyrinth](https://github.com/m-ender/labyrinth), 11 bytes
```
:!\
+ :
*:#
```
[Try it online!](https://tio.run/##y0lMqizKzCvJ@P/fSjGGS1vBikvLSvn/f1MA "Labyrinth – Try It Online")
Prints the sequence indefinitely.
### Explanation
The instruction pointer just keeps running around the square of code over and over:
```
:!\    Duplicate the last result (initially zero), print it and a linefeed.
:      Duplicate the result again, which increases the stack depth.
#      Push the stack depth (used as a counter variable).
:*     Square it.
+      Add it to the running total.
```
[Answer]
# [Cubix](https://github.com/ETHproductions/cubix), 15 bytes
```
Iu):^\+*p*6u@O,
```
[Try it online!](https://tio.run/##Sy5Nyqz4/9@zVNMqLkZbq0DLrNTBX@f/f1MA "Cubix – Try It Online")
My code is a bit sad `):`
Computes `n*(n+1)*(2n+1)/6`
```
    I u
    ) :
^ \ + * p * 6 u
@ O , . . . . .
    . .
    . .
```
```
^Iu : read in input, u-turn
    : stack  n
:)\ : dup, increment, go right..oh, hey, it cheered up!
    : stack: n, n+1
+   : sum
    : stack: n, n+1, 2*n+1
*   : multiply
    : stack: n, n+1, 2*n+1, (n+1)*(2*n+1)
p   : move bottom of stack to top
    : stack: n+1, 2*n+1, (n+1)*(2*n+1), n
*   : multiply
6   : push 6
u   : right u-turn
,   : divide
O   : output
@   : terminate
```
[Answer]
# [Befunge](https://github.com/catseye/Befunge-93), 16 bytes
```
&::2*1+*\1+*6/.@
```
Using the closed-form formula **n\*(n+1)\*(2n+1)/6**.
[Try it online!](https://tio.run/##S0pNK81LT/3/X83KykjLUFsrBojN9PUc/v83AQA "Befunge – Try It Online")
# [Befunge](https://github.com/catseye/Befunge-93), 38 bytes
```
v>::*\1-:v0+_$.@
&^ <     _\:^
>:#^_.@
```
Using a loop.
[Try it online!](https://tio.run/##S0pNK81LT/3/v8zOykorxlDXqsxAO15Fz4FLLU7BRgEE4mOs4rjsrJTj4vUc/v83BQA "Befunge – Try It Online")
[Answer]
## Haskell, 20 bytes
```
f 0=0
f n=n*n+f(n-1)
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P03BwNaAK00hzzZPK087TSNP11Dzf25iZp6CrUJBUWZeiYKKQpqC6X8A "Haskell – Try It Online")
[Answer]
# Excel, 19 bytes
```
=A1^3/3+A1^2/2+A1/6
```
[Answer]
# [><>](https://esolangs.org/wiki/Fish), ~~15~~ ~~13~~ 11 bytes
Saved 2 bytes thanks to *Not a tree*
```
0:n:l1-:*+!
```
[Try it online!](https://tio.run/##S8sszvj/38AqzyrHUNdKS1vx/38A "><> – Try It Online")
Outputs the sequence indefinitely.
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 7 5 bytes thanks to Steven H
```
s^R2h
```
Explanation:
```
s^R2h       Full program - inputs from stdin and outputs to stdout
s           output the sum of
    h       range(input), with
 ^R2         each element squared
```
---
My first solution
```
sm*ddUh
```
[Try it online!](https://tio.run/##K6gsyfj/vzhXKyUl@P9/cwA "Pyth – Try It Online")
Explanation:
```
sm*ddUh    Full program - inputs from stdin and outputs to stdout
s          sum of
 m   Uh    each d in range(input)
  *dd      squared
```
[Answer]
# [J](http://jsoftware.com/), ~~10~~ 9 bytes
```
4%~3!2*>:
```
[Try it online!](https://tio.run/##y/qvpKegnqZga6WgrqCjYKBgBcS6egrOQT5u/01U64wVjbTsrP5rcnGlJmfkK6QpmMAYpv8B "J – Try It Online")
*Saved 1 byte using an explicit formula, thanks to miles!*
---
## J, 10 bytes
```
1#.]*:@-i.
```
J has a range function, but this gives us number from `0` to `N-1`. To remedy this, we can just take the argument and subtract the range from it, giving us a range from `N` to `1`. This is done with `] -i.`. The rest of the code simply square this list argument (`*:@`) and then sums it (`1#.`).
## Other contenders
**11 bytes**: `1#.*:@i.@>:`
**13 bytes**: `1#.[:,@:*/~i.`
[Answer]
# [Thunno 2](https://github.com/Thunno/Thunno2) `S`, 2 [bytes](https://github.com/Thunno/Thunno2/blob/main/docs/codepage.md)
```
R²
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m728JKM0Ly_faGm0UrBS7IKlpSVpuhaLgw5tWlKclFwM5S9YaAJhAAA)
#### Explanation
```
R²  # Implicit input
R   # Range of input
 ²  # Square each
    # Sum the list
    # Implicit output
```
[Answer]
# [Nekomata](https://github.com/AlephAlpha/Nekomata), 3 bytes
```
R:∙
```
[Attempt This Online!](https://ato.pxeger.com/run?1=m70iLzU7PzexJHFZtJJurlLsgqWlJWm6FkuDrB51zFxSnJRcDBVZsNSAy4TLFMIBAA)
```
R:∙
R     Range
 :    Duplicate
  ∙   Dot product
```
Because there isn't a square built-in yet.
]