# Difference of the square of the sum

Find the difference between the square of the sums and sum of the squares.

This is the mathematical representation:

$\left(\sum n\right)^2-\sum n^2$

Your program/method should take two inputs, these are your lower and upper limits of the range, and are inclusive. Limits will be whole integers above 0.

You may use whichever base you would like to, but please state in your answer which base you have used.

Test case (Base 10)

5,9      970
91,123   12087152
1,10     2640


This is usual code-golf, so the shorter the answer the better.

• It took me a while to realize the input was the endpoints of a range. Jun 20, 2016 at 14:08
• @BradGilbertb2gills edited for clarity Jun 20, 2016 at 14:20
• This is simpler than it looks ?
– cat
Jun 22, 2016 at 20:17
• @cat what do you mean by that? Yes the maths is simple Alevel stuff. But it's all down to how you golf it Jun 22, 2016 at 20:18
• @george The question and many of the answers make it look like a lot of work, but it's not
– cat
Jun 22, 2016 at 21:17

## Perl 5.10, 41 bytes

map{$s+=$_,$r+=$_**2}(<>..<>);say$s**2-$r


Input is given on 2 lines, for instance:

61
127


Try it here!

# CoffeeScript, 49 Bytes

f=(n,m,s=0)->if n>m then 0else 2*n*s+f(n+1,m,n+s)

Translation of Neil's answer

Output:

f 91, 123 == 12087152 => true

# Python 2.7, 82 bytes 85 bytes

def a(l, u):
c=0
d=0
for e in range(l, u+1):
c+=e
d+=e**2
return c**2-d


Works in base 10.

• Currently your answer is invalid (use the test cases to verify it): 1. You forgot to square c 2. You have to subtract the sum of squares from the square of the sum, so c**2-d 3. I suggest to golf your code, and you can count newlines as one byte instead of two 4. I suggest adding a Try it online! link Jul 9, 2018 at 19:32

# Julia 0.6, 21 bytes

r->2triu(r*r',1)|>sum


Try it online!

Julia port of Luis Mendo's MATL answer. Comes out to the same bytecount as the other Julia answer by @gggg. Takes a Range similar to that answer, does a matrix multiply of it with its transpose to get all pairwise products in a matrix. Takes the upper triangular portion of that (the 1 argument is to denote the distance from the leading diagonal to start from), multiplies that by 2, and finally sums the values and returns that implicitly.

# Wolfram Language (Mathematica), 18 bytes

Tr@#^2-#.#&@*Range


Try it online!

Composition of Tr@#^2-#.#& (square of sum minus sum of squares, where sum of squares is implemented via dot product) and Range (list of all integers between the two inputs). If you like seeing the operations in order, Range/*Tr@#^2-#.#& is equivalent.

As a bonus, also solves this challenge with no modifications, since Range can either take one argument (giving the range from 1 to the input) or two. (But it's not the most efficient solution possible there.)

# Bash, 74 65 bytes

x=y=0;for i in seq $1$2;{ x=$[x+i];y=$[y+i*i]; };echo $[x*x-y]  Try it online! I'm no expert at bash golfing, but this works. Managed to cut 9 bytes just by reading through the bash golfing tips thread • for((i=$1;i<=$2;x+=i,y+=i*i++)){ :;};echo$[x*x-y] saves 15 bytes. Nov 23, 2018 at 1:37

# Tcl, 105 bytes

proc d a\ b {while {[incr a]<=$b} {set j$a
while \$j<=$b {incr p [expr 2*([incr j]-1)*($a-1)]}} expr$p}


Try it online!

# Tcl, 112 bytes

proc d a\ b {set i [expr $a-1] while $$\ij<$b {incr p [expr 2*[incr j]*$i]}} expr$p}


# Factor + math.unicode, 34 bytes

[ [a,b] dup Σ sq swap norm-sq - ]


Try it online!

• [a,b] Create a range from the input.
• dup Σ sq swap Square of sum.
• norm-sq Sum of squares.
• - Subtract.