Matrix chain multiplication: Difference between revisions
m
→{{header|Fortran}}
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: show cost, as per task spec) |
|||
Line 200:
This is a translation of the Python iterative solution.
<lang fortran>module
implicit none
contains
Line 206:
implicit none
integer :: a(:), n, i, j, k
integer(8) :: c▼
integer, allocatable :: u(:, :)
▲ integer(8) :: c
integer(8), allocatable :: v(:, :)
n = ubound(a, 1) - 1
allocate (u(n, n), v(n, n))
v =
u(:, 1) = -1
v(:, 1) = 0
Line 219:
do k = 1, j - 1
c = v(i, k) + v(i + k, j - k) + int(a(i), 8) * int(a(i + k), 8) * int(a(i + j), 8)
if (
u(i, j) = k
v(i, j) = c
Line 233:
recursive subroutine aux(i, j)
integer :: i, j, k
k = u(i, j)
if (k < 0) then
Line 248:
end module
program
use
implicit none
call optim([5, 6, 3, 1])
call optim([1, 5, 25, 30, 100, 70, 2, 1, 100, 250, 1, 1000, 2])
call optim([1000, 1, 500, 12, 1, 700, 2500, 3, 2, 5, 14, 10])
Line 259 ⟶ 260:
<pre>
48 (1*(2*3))
38120 ((((((((1*2)*3)*4)*5)*6)*7)*(8*(9*10)))*(11*12))
1773740 (1*((((((2*3)*4)*(((5*6)*7)*8))*9)*10)*11))
|