Largest product in a grid: Difference between revisions

m
(→‎{{header|Perl}}: Added alternate version)
Line 198:
<pre>The maximum product of 4 adjacent horizontal or vertical in the matrix is: 51267216</pre>
Alternatively, to get the position of the maximum product:
<lang julia>letfunction maxprod(mat, len)
nrow, ncol = size(mat)
maxprod, maxrow, maxcol, arr = 0, 0:0, 0:0, [0]
for row in 1:nrow, col in 1:ncol
if row < nrow - len + 2
pro = prod(mat[row:row+3len-1, col])
if pro > maxprod
maxprod, maxrow, maxcol, arr = pro, row:row+3len-1, col:col, mat[row:row+3len-1, col]
end
end
if col < ncol - len + 2
pro = prod(mat[row, col:col+3len-1])
if pro > maxprod
maxprod, maxrow, maxcol, arr = pro, row:row, col:col+3len-1, mat[row, col:col+3len-1]
end
end
end
println("The maxiumummaximum product is $maxprod, product of $arr at row $maxrow, col $maxcol")
end
 
maxprod(mat, 4)
</lang>{{out}}
<pre>The maxiumummaximum product is 51267216, product of [66, 91, 88, 97] at row 7:10, col 16:16</pre>
 
=={{header|Perl}}==
4,104

edits