# Sum square difference

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.

### Winning Criteria

• There are no rules about what should be the behavior with negative or zero input.

• This challenge needs a winning criterion (e.g. code golf) – dylnan Nov 4 '18 at 19:25
• This is a subset of this question – caird coinheringaahing Nov 4 '18 at 19:45
• Can the sequence be 0 indexed? i.e. the natural numbers up to n? – Jo King Nov 4 '18 at 23:49
• – user202729 Nov 5 '18 at 5:53
• @Enigma I really don't think that this is a duplicate of the target since many answers here don't port easily to be answers of that, so this adds something. – Jonathan Allan Nov 5 '18 at 8:33

# PowerShell, 73 39 bytes

1.."$args"|%{$r+=$_;$s+=$_*$_}
$r*$r-$s  Try it online! -34 bytes thanks to @mazzy and his genius PowerShell-foo • nice try. iex is a power tool in the Powershell. but it is often longer than the plus operator :) Try it online! – mazzy May 13 at 16:39 • Damn @mazzy ... Thanks for the pro-tip! – KGlasier May 13 at 16:42 • formula as other solutions in this topic – mazzy May 14 at 7:48 ## C#, 89 Bytes int x=0,y=0;for(int i=1;i<=Int32.Parse(s[0]);i++){x+=i*i;y+=i;}Console.Write($"{y*y-x}");


ungolfed:

int x=0,y=0;
for(int i=1;i<=Int32.Parse(s[0]);i++){
x+=i*i;
y+=i;
}
Console.Write($"{y*y-x}");  Try it online! • You can save 31 bytes if you use a function to read in and return ints, as well as make some changes with the for loop call. Try it online! – Meerkat Nov 5 '18 at 19:27 • @Meerkat Is one allowed to put the Output code in the footer? Then I get to 57 bytes – user51497 Nov 5 '18 at 21:01 • Output is supposed to be in the body. With the function call, this is what the return essentially does, which is why it's included in the body. – Meerkat Nov 5 '18 at 21:28 # Axiom, 39 bytes f(n)==reduce(+,[x^3-x^2 for x in 1..n])  test: -> [[x,f x]for x in [1,2,3,10]] [[1,0],[2,4],[3,22],[10,2640]]  # Clojure, 91 bytes (fn s[n](let[u #(apply + %)q #(Math/pow % 2)m(range 1(inc n))](-(q(u m))(u(map #(q %)m)))))  The naive, literal approach. See below: (defn sum-sq-diff [n] (let [; Shortcut functions to save bytes sum #(apply + %) square #(Math/pow % 2) nums (range 1 (inc n)) ss1 (sum (map #(square %) nums)) ss2 (square (sum nums))] (- ss2 ss1))) (mapv sum-sq-diff [1 2 3 10 24 100]) => [0.0 4.0 22.0 2640.0 85100.0 2.516415E7]  Try it online! # Ruby, 24 bytes ->n{(n+n+3*n*=n)*~-n/12}  Try it online! # Python 3, 72 bytes x=[i+1for i in range(int(input()))] print(sum(x)**2-sum(i**2for i in x))  Try it online! # F# (Mono), 57 41 bytes let f x=Seq.sumBy(fun y->y*y*(y-1))[1..x]  Try it online! # PHP, 37 bytes while($i<$argn)$s+=$i++*$i*$i;echo$s;


Try it online!

Standalone program input number via STDIN. Example:

$echo 10|php -nF ssd.php 2640$ echo 100|php -nF ssd.php
25164150


# Perl 6, 22 bytes

{($_³-$_)*(\$_/4+⅙)}


Try it online!