Perl 6, 36 32 31 bytes
{([+] $_=@_[0]..@_[1])²-[+] $_»²}
{([+] $_=$^a..$^b)²-[+] $_»²}
{[+]($_=$^a..$^b)²-[+] $_»²}
Test it
Explanation:
{ # bare block with placeholder parameters $a and $b
[+](# reduce with &infix:<+>
# create a range, and store it in $_
$_ = $^a .. $^b
)²
-
[+] # reduce with &infix:<+>
# square each element of $_ ( possibly in parallel )
$_»²
}
Test:
#! /usr/bin/env perl6
use v6.c;
use Test;
my @tests = (
(5,9) => 970,
(91,123) => 12087152,
(1,10) => 2640,
);
plan +@tests;
my &diff-sq-of-sum = {[+]($_=$^a..$^b)²-[+] $_»²}
for @tests -> $_ ( :key(@input), :value($expected) ) {
is diff-sq-of-sum(|@input), $expected, .gist
}
1..3
ok 1 - (5 9) => 970
ok 2 - (91 123) => 12087152
ok 3 - (1 10) => 2640