in reply to adding n-tuples
Note that you can factor out a1 and b1 from the formula, you'll get
In the second factor, you can use the same technique to factor out a2 + b2, and so on. In the end, you'll get(a1 + b1) * (a2 * a3 * a4 + a2 * a3 * b4 + ...)
(a1 + b1) * (a2 + b2) * (a3 + b3) * (a4 + b4)
So, the solution is
$sum = 1; for my $i (0 .. 3) { $sum *= $A[$i] + $B[$i]; } say $sum;
And it gives the same result as
my $sum = 0; for my $pattern (map unpack('b4', pack 's', $_), 0 .. 15) { my $prod = 1; for my $i (0 .. 3) { $prod *= (\@A, \@B)[substr $pattern, $i, 1][$i]; } $sum += $prod; } say $sum;
|
|---|