펄 6 , 36 32 31 바이트
{([+] $_=@_[0]..@_[1])²-[+] $_»²}
{([+] $_=$^a..$^b)²-[+] $_»²}
{[+]($_=$^a..$^b)²-[+] $_»²}
그것을 테스트
설명:
{ # 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 )
$_»²
}
테스트:
#! /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