42
\$\begingroup\$

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.

Your program/method should return the answer.

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.

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

68 Answers 68

1 2
3
0
\$\begingroup\$

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!

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – wastl
    Commented Jul 9, 2018 at 19:32
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.)

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
1
  • 1
    \$\begingroup\$ for((i=$1;i<=$2;x+=i,y+=i*i++)){ :;};echo $[x*x-y] saves 15 bytes. \$\endgroup\$
    – Dennis
    Commented Nov 23, 2018 at 1:37
0
\$\begingroup\$

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 \$i<$b {set j [incr i]
while \$j<$b {incr p [expr 2*[incr j]*$i]}}
expr $p}

\$\endgroup\$
0
\$\begingroup\$

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.
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.