Matrix chain multiplication: Difference between revisions
m
→{{header|Perl}}: show cost, as per task spec
SqrtNegInf (talk | contribs) (→{{header|Perl 6}}: simplified code to directly address the task specifications) |
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: show cost, as per task spec) |
||
Line 543:
=={{header|Perl}}==
{{trans|Perl 6}}
<lang perl>
use feature 'say';
sub matrix_mult_chaining {
my(@dimensions) = @_;
my(@cp,@path);
Line 557 ⟶ 560:
for my $step ($start .. $end - 1) {
my $new_cost = $cp[$step][$start]
if ($new_cost < $cp[$end][$start]) {
$cp[$end][$start] = $new_cost; # cost
Line 567 ⟶ 570:
}
$cp[$n-1][0] . ' ' . find_path(0, $n-1, @cp);
}
Line 585 ⟶ 588:
}
{{out}}
<pre>38120 ((((((((A1A2)A3)A4)A5)A6)A7)(A8(A9A10)))(A11A12))
1773740 (A1((((((A2A3)A4)(((A5A6)A7)A8))A9)A10)A11))</pre>
=={{header|Perl 6}}==
|