[Question]
  [
The sum of the squares of the first ten natural numbers is,
\$1^2 + 2^2 + \dots + 10^2 = 385\$
The square of the sum of the first ten natural numbers is,
\$(1 + 2 + ... + 10)^2 = 55^2 = 3025\$
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 
\$3025 − 385 = 2640\$
For a given input n, find the difference between the sum of the squares of the first n natural numbers and the square of the sum.
Test cases
```
1       => 0
2       => 4
3       => 22
10      => 2640
24      => 85100
100     => 25164150
```
This challenge was first announced at [Project Euler #6](https://projecteuler.net/problem=6).
### Winning Criteria
* There are no rules about what should be the behavior with negative or zero input.
* The shortest answer wins.
  
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~5~~ 4 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
Ḋ²ḋṖ
```
**[Try it online!](https://tio.run/##y0rNyan8///hjq5Dmx7u6H64c9r///8NDQwA "Jelly – Try It Online")**
### How?
Implements \$\sum\_{i=2}^n{(i^2(i-1))}\$...
```
Ḋ²ḋṖ - Link: non-negative integer, n
Ḋ    - dequeue (implicit range)       [2,3,4,5,...,n]
 ²   - square (vectorises)            [4,9,16,25,...,n*n]
   Ṗ - pop (implicit range)           [1,2,3,4,...,n-1]
  ḋ  - dot product                    4*1+9*2+16*3+25*4+...+n*n*(n-1)
```
[Answer]
# [Python 3](https://docs.python.org/3/), ~~28~~ 27 bytes
-1 thanks to xnor
```
lambda n:(n**3-n)*(n/4+1/6)
```
**[Try it online!](https://tio.run/##K6gsycjPM/6fZhvzPycxNyklUSHPSiNPS8tYN09TSyNP30TbUN9M839afpFCSWpxiUJmnoKGoY6RjrGOoYGOkQmQNNC04lIAgoKizLwSDZAiHQV1XTt1HYU0ME9T8z8A "Python 3 – Try It Online")**
Implements \$n(n-1)(n+1)(3n+2)/12\$
---
Python 2, ~~29~~ 28 bytes: [`lambda n:(n**3-n)*(3*n+2)/12`](https://tio.run/##HYpBCoAgEADvvWJvqW2U2kmol3QxShJqDfPS682aw8DAXE/aA6nsxjkf9lxWC2QYCaFb4oJpQY3inSxDiJC2O4EnYBIVapQ9qqG456aCwhU9pX9CqNupRnDsK55f "Python 2 – Try It Online")
[Answer]
# [APL (Dyalog Unicode)](https://www.dyalog.com/), 10 bytes
```
1⊥⍳×⍳×1-⍨⍳
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///3/BR19JHvZsPTwcThrqPelcAWf/THrVNeNTb96hvqqf/o67mQ@uNH7VNBPKCg5yBZIiHZ/D/NAUjrjQFYyA2NAAA "APL (Dyalog Unicode) – Try It Online")
### How it works
```
1⊥⍳×⍳×1-⍨⍳
  ⍳×⍳×1-⍨⍳  Compute (x^3 - x^2) for 1..n
1⊥          Sum
```
Uses the fact that "square of sum" is equal to "sum of cubes".
[Answer]
# TI-Basic (TI-83 series), ~~12~~ 11 bytes
```
sum(Ans² nCr 2/{2,3Ans
```
Implements \$\binom{n^2}{2}(\frac12 + \frac1{3n})\$. 
Takes input in `Ans`: for example, run `10:prgmX` to compute the result for input `10`.
[Answer]
# [Brain-Flak](https://github.com/Flakheads/BrainHack), ~~74~~ ~~72~~ ~~68~~ 64 bytes
```
((([{}])){({}())}{})([{({}())({})}{}]{(({}())){({})({}())}{}}{})
```
[Try it online!](https://tio.run/##SypKzMzLSEzO/v9fQ0Mjuro2VlOzWqO6VkNTs7a6VhMoAuEASZBAbLUGhA9WpAlXCFL7//9/QwMA "Brain-Flak (BrainHack) – Try It Online")
Pretty simple way of doing it with a couple of tricky shifts. Hopefully someone will find some more tricks to make this even shorter.
[Answer]
# Japt `-x`, ~~9~~ ~~8~~ ~~5~~ 4 bytes
```
õ²í*
```
[Try it](https://ethproductions.github.io/japt/?v=1.4.6&code=9bLtKg==&input=MjQKLXg=)
---
## Explanation
```
õ        :Range [1,input]
 ²       :Square each
  í      :Interleave with 0-based indices
   *     :Reduce each pair by multiplication
         :Implicit output of the sum of the resulting array
```
[Answer]
# JavaScript, 20 bytes
```
f=n=>n&&n*n*--n+f(n)
```
[Try it online](https://tio.run/##y0osSyxOLsosKNHNy09J/f8/zTbP1i5PTS1PK09LVzdPO00jT/N/cn5ecX5Oql5OfrpGmoaRiabmfwA)
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~12~~ 10 bytes
```
ＩΣＥＮ×ιＸ⊕ι²
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM5sbhEI7g0V8M3sUDDMy@5KDU3Na8kNQXILigt8SvNTUot0tDU1FHwzcwrLdYIyC8H8jN1FIyBQnCOkSYYWP//b2jwX7csBwA "Charcoal – Try It Online") Link is to verbose version of code. Explanation: \$ ( \sum\_1^n x )^2 = \sum\_1^n x^3 \$ so \$ ( \sum\_1^n x )^2 - \sum\_1^n x^2 = \sum\_1^n (x^3 - x^2) = \sum\_1^n (x - 1)x^2 = \sum\_0^{n-1} x(x + 1)^2 \$.
```
   Ｎ        Input number
  Ｅ         Map over implicit range i.e. 0 .. n - 1
        ι   Current value
       ⊕    Incremented
         ²  Literal 2
      Ｘ     Power
     ι      Current value
    ×       Multiply
 Σ          Sum
Ｉ           Cast to string
            Implicitly print
```
[Answer]
# [Perl 6](https://github.com/nxadm/rakudo-pkg), 22 bytes
```
{sum (1..$_)>>²Z*^$_}
```
[Try it online!](https://tio.run/##K0gtyjH7n1upoJamYKvwv7q4NFdBw1BPTyVe087u0KYorTiV@Nr/xYmVCmkKhtZcEIYRjGEMYxgawOVMEGIG1v8B "Perl 6 – Try It Online")
Uses the construction \$ \sum\_{i=1}^n {(i^2(i-1))} \$
[Answer]
# [dc](https://www.gnu.org/software/bc/manual/dc-1.05/html_mono/dc.html), 16 bytes
```
?dd3^r-r3*2+*C/p
```
Implements \$(n^3-n)(3n+2)/12\$
[Try it online!](https://tio.run/##S0n@/98@JcU4rki3yFjLSFvLWb/g/39DAwA "dc – Try It Online")
[Answer]
# APL(Dyalog), 17 bytes
```
{+/(¯1↓⍵)×1↓×⍨⍵}⍳
```
(Much longer) Port of Jonathan Allan's Jelly answer.
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///v1pbX@PQesNHbZMf9W7VPDwdxDo8/VHvCiC39lHv5v9pj9omPOrte9Q31dP/UVfzofXGj9omAnnBQc5AMsTDM/h/moIRV5qCMRAbGgAA)
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 16 bytes
```
((×⍨+/)-(+/×⍨))⍳
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///X0Pj8PRHvSu09TV1NbT1wWxNzUe9m/@nPWqb8Ki371HfVE//R13Nh9YbP2qbCOQFBzkDyRAPz@D/aQpGXGkKxkBsaAAA "APL (Dyalog Unicode) – Try It Online")
```
 (×⍨+/)            The square (× self) of the sum (+ fold)
       -           minus
        (+/×⍨)     the sum of the square
(             )⍳   of [1, 2, … input].
```
[Answer]
# Mathematica, ~~21~~ 17 bytes
*-4 bytes thanks to [alephalpha](https://codegolf.stackexchange.com/users/9288).*
```
(3#+2)(#^3-#)/12&
```
Pure function. Takes an integer as input and returns an integer as output. Just implements the polynomial, since `Sum`s, `Range`s, `Tr`s, etc. take up a lot of bytes.
[Answer]
# [Java (JDK)](http://jdk.java.net/), 23 bytes
```
n->(3*n+2)*(n*n*n-n)/12
```
[Try it online!](https://tio.run/##NY5Na8MwDIbv@RWiELAzx2vSnlo62HGHsUPZaezgJXFxlsjGVgqh5LdnTsIQQl/PK6lVd5W39e9semc9QRtrOZDppB6wImNRZuek6lQI8K4MwiMBcMNPZyoIpCiGuzU19HHGruQN3r6@Qflb4CsK8Ib0icqPH67xiqwHfZkxf2GHDJ9KnjHMouXIn4tyPq8KgxR3UBMowAUehYBSwEFAsY/ZcYn7aSO19SzSK3vaFP9nAa5joKaXdiDp4l@k2S491idI6xR3YoEFaKmc68bXEL9kS4vzbfOULD7Nfw "Java (JDK) – Try It Online")
[Answer]
# [MathGolf](https://github.com/maxbergmark/mathgolf), 6 bytes
```
{î²ï*+
```
[Try it online!](https://tio.run/##y00syUjPz0n7/7/68LpDmw6v19L@/9@Qy4jLmMvQgMvIBEgaAAA "MathGolf – Try It Online")
Calculates \$\sum\_{k=1}^n (k^2(k-1))\$
### Explanation:
```
{       Loop (implicit) input times
 î²     1-index of loop squared
    *   Multiplied by
   ï    The 0-index of the loop
     +  And add to the running total
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 8 bytes
```
ÝDOnsnO-
```
Explanation:
```
ÝDOnsnO-     //Full program
Ý            //Push [0..a] where a is implicit input
 D           //Duplicate top of stack
  On         //Push sum, then square it
    s        //Swap top two elements of stack
     nO      //Square each element, then push sum
       -     //Difference (implicitly printed)
```
[Try it online!](https://tio.run/##yy9OTMpM/f//8FwX/7ziPH/d//8NDQA "05AB1E – Try It Online")
[Answer]
## C, C++, ~~46~~ ~~40~~ 37 bytes ( #define ), ~~50~~ ~~47~~ 46 bytes ( function )
-1 byte thanks to Zacharý
-11 bytes thanks to ceilingcat
Macro version :
```
#define F(n)n*n*~n*~n/4+n*~n*(n-~n)/6
```
Function version :
```
int f(int n){return~n*n*n*~n/4+n*~n*(n-~n)/6;}
```
Thoses lines are based on thoses 2 formulas :
Sum of numbers between 1 and n = `n*(n+1)/2`   
Sum of squares between 1 and n = `n*(n+1)*(2n+1)/6`
So the formula to get the answer is simply `(n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6`
And now to "optimize" the byte count, we break parenthesis and move stuff around, **while testing it always gives the same result**
`(n*(n+1)/2) * (n*(n+1)/2) - n*(n+1)*(2n+1)/6` =>
`n*(n+1)/2*n*(n+1)/2 - n*(n+1)*(2n+1)/6` =>
`n*(n+1)*n*(n+1)/4 - n*(n+1)*(2n+1)/6`
Notice the pattern `p = n*n+1 = n*n+n`, so in the function, we declare another variable `int p = n*n+n` and it gives :
`p*p/4 - p*(2n+1)/6`
For `p*(p/4-(2*n+1)/6)` and so `n*(n+1)*(n*(n+1)/4 - (2n+1)/6)`, it works half the time only, and I suspect integer division to be the cause ( `f(3)` giving 24 instead of 22, `f(24)` giving 85200 instead of 85100, so we can't factorize the macro's formula that way, even if mathematically it is the same.
Both the macro and function version are here because of macro substitution :
F(3) gives `3*3*(3+1)*(3+1)/4-3*(3+1)*(2*3+1)/6 = 22`   
F(5-2) gives `5-2*5-2*(5-2+1)*(5-2+1)/4-5-2*(5-2+1)*(2*5-2+1)/6 = -30`
and mess up with the operator precedence. the function version does not have this problem
[Answer]
# JavaScript (ES6), 22 bytes
```
n=>n*~-n*-~n*(n/4+1/6)
```
[Try it online!](https://tio.run/##y0osSyxOLsosKNHNy09J/Z9m@z/P1i5Pq043T0u3Lk9LI0/fRNtQ30zzf3J@XnF@TqpeTn66RpqGoaYmF6qIEYaIMYaIoQGmNhMsqoDK/gMA "JavaScript (Node.js) – Try It Online")
[Answer]
# Pyth, 7 bytes
```
sm**hdh
```
Try it online [here](https://pyth.herokuapp.com/?code=sm%2A%2Ahdh&input=10&debug=0).
Uses the formula in [Neil's answer](https://codegolf.stackexchange.com/a/175268/41020).
```
sm**hdhddQ   Implicit: Q=eval(input())
             Trailing ddQ inferred
 m       Q   Map [0-Q) as d, using:
    hd         Increment d
   *  hd       Multiply the above with another copy
  *     d      Multiply the above by d
s            Sum, implicit print 
```
[Answer]
# [SNOBOL4 (CSNOBOL4)](http://www.snobol4.org/csnobol4/), ~~70~~ 69 bytes
```
 N =INPUT
I X =X + N ^ 3 - N ^ 2
 N =GT(N) N - 1 :S(I)
 OUTPUT =X
END
```
[Try it online!](https://tio.run/##K87LT8rPMfn/X8FPwdbTLyA0hMtTIULBNkJBGygSp2CsoAumjbhACtxDNPw0gQxdBUMFq2ANT00uBf/QEKAmoAYuVz@X//8NDQA "SNOBOL4 (CSNOBOL4) – Try It Online")
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 21 bytes
```
n->(3*n+2)*(n^3-n)/12
```
[Try it online!](https://tio.run/##FcdLCoAwDEXRrTw6ajTFfpzqRkTBSUWQEIoTVx91dM/VvZ3hUKuYYBJmXzrpM3VethKEhpRtV70eLwgztJ1yf3T/OFQvRIwlMTKjMFL8NP6NK9kL "Pari/GP – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 6 bytes
```
LnDƶαO
```
[Try it online!](https://tio.run/##yy9OTMpM/f/fJ8/l2LZzG/3//zc0MAAA "05AB1E – Try It Online")
**Explanation**
```
L         # push range [1 ... input]
 n        # square each
  D       # duplicate
   ƶ      # lift, multiply each by its 1-based index
    α     # element-wise absolute difference
     O    # sum
```
Some other versions at the same byte count:
`L<ān*O`  
`Ln.āPO`  
`L¦nā*O` 
[Answer]
# [R](https://www.r-project.org/), 28 bytes
```
x=1:scan();sum(x)^2-sum(x^2)
```
[Try it online!](https://tio.run/##K/r/v8LW0Ko4OTFPQ9O6uDRXo0IzzkgXzIgz0vxvaGDw/z8A "R – Try It Online")
[Answer]
#  [Clojure](https://clojure.org/), 58 bytes
```
(fn[s](-(Math/pow(reduce + s)2)(reduce +(map #(* % %)s))))
```
[Try it online!](https://tio.run/##S87JzyotSv3/XyMtL7o4VkNXwzexJEO/IL9coyg1pTQ5VUFboVjTSBPO08hNLFBQ1tBSUFVQ1SzWBIL//wE "Clojure – Try It Online")
---
## Edit: I misunderstood the question
# [Clojure](https://clojure.org/), 55, 35 bytes
```
#(* %(+ 1 %)(- % 1)(+(* 3 %)2)1/12)
```
[Try it online!](https://tio.run/##jYwxDsIwEAR7XrESsnRHhJCdki9Q8AUTziLInCHYSLzeOKU7yt2Z3Smme1mk1i3tYGiAhWHaw8AyDa0bW3ZsD9ZxpasEBPyhMo44SW6yvIqPyDeB16TfRypvhKJTnpPCX9JHNvRcZs1RQaFdrdPzWiCQ5Q66Droejh0cuf4A "Clojure – Try It Online")
[Answer]
# [cQuents](https://github.com/stestoltz/cQuents/tree/cquents-0), ~~17~~ 15 bytes
```
b$)^2-c$
;$
;$$
```
[Try it online!](https://tio.run/##Sy4sTc0rKf7/P0lFM85IN1mFyxqEVP7/NzQAAA "cQuents – Try It Online")
## Explanation
```
 b$)^2-c$     First line
:             Implicit (output nth term in sequence)
 b$)          Each term in the sequence equals the second line at the current index
    ^2        squared
      -c$     minus the third line at the current index
;$            Second line - sum of integers up to n
;$$           Third line - sum of squares up to n
```
[Answer]
# MATLAB/Octave, 21 bytes
```
@(x)(x^3-x)*(x/4+1/6)
```
Anonymous function. We could save another 4 bytes if we assume `x` can be given as variable in workspace.  
Implements \$n(n−1)(n+1)(3n+2)/12\$
---
One of my approaches was `@(x)-sum([sum(1:x)*i,1:x].^2)`
which is few bytes longer but I found it interesting because It used imaginary unit to change the sign.  
I also found out I can generate sum of squares by multiplying normal and transposed vector with `(1:x)*(1:x)'`. Might be useful for future challenges!
[Answer]
# [Labyrinth](https://github.com/m-ender/labyrinth), 20 bytes
```
?(:):):_3*(***_12/!@
```
[Try it online!](https://tio.run/##y0lMqizKzCvJ@P/fXsNKEwjjjbU0tLS04g2N9BUd/v83NDAAAA "Labyrinth – Try It Online")
Straightforward closed-form formula of \$(x-1)x(x+1)(3x+2)/12\$.
```
?      Take input [x]
(:):)  Decrement, dup, increment, dup, increment [x-1 x x+1]
:_3*(  Dup, 3 times, decrement [x-1 x x+1 3x+2]
***    Multiply four values
_12/   Divide by 12
!@     Print it and halt
```
# [Labyrinth](https://github.com/m-ender/labyrinth), 21 bytes
```
?
::(";;;!@
{  :
+**}
```
[Try it online!](https://tio.run/##y0lMqizKzCvJ@P/fnsvKSkPJ2tpa0YGrWkHBiktbS6v2/39DAwMA "Labyrinth – Try It Online")
# [Labyrinth](https://github.com/m-ender/labyrinth), 22 bytes
```
   ?
:(::
}  "
**+{;!@
```
[Try it online!](https://tio.run/##y0lMqizKzCvJ@P9fQUHBnstKw8qKq1ZBQYlLS0u72lrR4f9/QwMDAA "Labyrinth – Try It Online")
Two versions using a loop to evaluate \$\sum\_{i=1}^{x}{i^2(i-1)}\$. Both versions test the loop exit with `i-1 == 0`, discard the top values `;` as necessary, print the sum and exit.
```
?     Take input [x]; enter loop [sum i] with initial values of sum=0, i=x
::(   Dup, dup, decrement [sum i i i-1]
:}    Dup and move i-1 to aux. store [sum i i i-1 | i-1]
**+   Multiply top three values and add to sum [sum' | i-1]
{     Move i-1 back to the main stack
      Loop exit test can be done anywhere in the last iteration
      (i==1 or i==0), since that iteration does not affect the sum.
```
[Answer]
# ARM (Thumb), 18 bytes
```
.section .text
.global func
.thumb
// int func(int dummy, int x)
// returns sum(i*i*(i-1) for i in 1..x)
// r1 = i (x->1), r2 = i*i*(i-1), r0 = sum
func:
    mov r0, #0       // 2000   int sum = 0;
    mov r2, #0       // 2200   int prod = 0;
.loop:               //        do {
    add r0, r0, r2   // 1880   sum += prod;
    mov r2, r1       // 1c0a   prod = x;
    mul r2, r1       // 434a   prod *= x;
    sub r1, #1       // 3901   x -= 1;
    mul r2, r1       // 434a   prod *= x;
    bne .loop        // d1f9   } while (prod != 0);
    bx lr            // 4770   return sum;
```
A function that takes the input integer via `r1` and returns the value via `r0`.
Uses the iterative formula \$\sum\_{i=1}^{x}{i^2(i-1)}\$, iterating backwards. The loop terminates when the value of \$i^2(i-1)\$ is 0.
For the purposes of writing test cases, the C-side function signature takes a dummy value for `r0`.
Test cases written in C:
```
#include <stdio.h>
int func(int dummy, int x);
int main(void) {
    int dummy;
    int inputs[] = {1, 2, 3, 10, 24, 100};
    for(int i = 0; i < 6; ++i) {
        int v = inputs[i];
        printf("input = %d -> ans = %d\n", v, func(dummy, v));
    }
    return 0;
}
```
Build, run, objdump commands:
```
arm-linux-gnueabihf-gcc main_c.c func_c.S -static -o main_c
qemu-arm -L /usr/arm-linux-gnueabihf ./main_c
arm-linux-gnueabihf-objdump -d main_c | awk -v RS= '/^[[:xdigit:]]+ <(func|.loop)>/'
```
(The objdump + awk trick comes from [this SO answer](https://stackoverflow.com/a/31138400).)
[Answer]
# [Vyxal](https://github.com/Vyxal/Vyxal), 8 bytes
```
ɾ∑²⁰ɾ²∑-
```
[Try it Online!](https://vyxal.pythonanywhere.com/#WyJQIiwiIiwiyb7iiJHCsuKBsMm+wrLiiJEtIiwiIiwiMyJd)
literal solution, squares the sum and subtracts the sum of the squares.
[Answer]
# [Factor](https://factorcode.org/) + `math.unicode`, 32 bytes
```
[ [1,b] 3 dupn Σ sq -rot v. - ]
```
[Try it online!](https://tio.run/##JcwxDoJAFIThnlPMAYQAWukBjI2NsSIU6/LEjfB22X2QqPE03scrrRiaKb5J/qvSYn08nw7H/Ra9klvmFbcUcCfP1C00stG2IbTE5FVnnkqM5bCcE/0TAYGGkVjTwnCeRB7OGxbskuSFAiXWKHKUm3lzvJNYoSpWl3rmZnSM7wdhQOqtYMqQoo69csjiDw "Factor – Try It Online")
```
word   | data stack                                     | comment
-------+------------------------------------------------+--------------
       | 10                                             | example input
[1,b]  | { 1 2 ... 10 }                                 |
3      | { 1 2 ... 10 } 3                               |
dupn   | { 1 2 ... 10 } { 1 2 ... 10 } { 1 2 ... 10 }   |
Σ      | { 1 2 ... 10 } { 1 2 ... 10 } 55               | sum
sq     | { 1 2 ... 10 } { 1 2 ... 10 } 3025             | square
-rot   | 3025 { 1 2 ... 10 } { 1 2 ... 10 }             |
v.     | 3025 385                                       | dot product
-      | 2640                                           | output
```
]