Largest product in a grid: Difference between revisions

Added Easylang
m (add ooRexx and REXX)
(Added Easylang)
 
(10 intermediate revisions by 10 users not shown)
Line 33:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F maxproduct(mat, length)
‘ find the largest product of len length horizontal or vertical length in matrix ’
V (nrow, ncol) = (mat.len, mat[0].len)
Line 76:
 
L(n) 2..5
maxproduct(MATRIX, n)</langsyntaxhighlight>
 
{{out}}
Line 87:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN # find the maximum product of 4 adjacent numbers in a row or column of a matrix #
[,]INT m = ( ( 08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12, 50, 77, 91, 08 )
, ( 49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00 )
Line 141:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
The maximum product of 4 elements: 51267216 is the column of 4 numbers starting at: 7, 16
</pre>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">grid: [
[08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08]
[49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00]
[81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65]
[52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91]
[22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80]
[24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50]
[32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70]
[67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21]
[24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72]
[21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95]
[78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92]
[16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57]
[86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58]
[19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40]
[04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66]
[88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69]
[04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36]
[20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16]
[20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54]
[01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48]
]
 
findLargestProduct: function [g][
dim: size g
maxProd: [[], 0]
loop 0..dec dim 'row [
loop 0..dim-4 'col [
items: @[g\[row]\[col], g\[row]\[col+1], g\[row]\[col+2], g\[row]\[col+3]]
prod: product items
if prod > last maxProd [
maxProd: @[items, prod]
]
]
]
loop 0..dec dim 'col [
loop 0..dim-4 'row [
items: @[g\[row]\[col], g\[row+1]\[col], g\[row+2]\[col], g\[row+3]\[col]]
prod: product items
if prod > last maxProd [
maxProd: @[items, prod]
]
]
]
return maxProd
]
 
print findLargestProduct grid</syntaxhighlight>
 
{{out}}
 
<pre>[66 91 88 97] 51267216</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Grid =
(
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
Line 198 ⟶ 253:
}
}
MsgBox, 262144, ,% result := "Max Product = " maxProd . Steps[maxProd]</langsyntaxhighlight>
{{out}}
<pre>Max Product = 51267216
66*91*88*97 @ Row 7 - Row 10, Col 16</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f LARGEST_PRODUCT_IN_A_GRID.AWK
BEGIN {
Line 261 ⟶ 317:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
51267216 = 66*91*88*97 in column 16 rows 7-10
</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
type T2DGrid = array [0..19,0..19] of integer;
 
 
const PGrid: T2DGrid =(
(08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08),
(49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00),
(81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,03,49,13,36,65),
(52,70,95,23,04,60,11,42,69,24,68,56,01,32,56,71,37,02,36,91),
(22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80),
(24,47,32,60,99,03,45,02,44,75,33,53,78,36,84,20,35,17,12,50),
(32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70),
(67,26,20,68,02,62,12,20,95,63,94,39,63,08,40,91,66,49,94,21),
(24,55,58,05,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72),
(21,36,23,09,75,00,76,44,20,45,35,14,00,61,33,97,34,31,33,95),
(78,17,53,28,22,75,31,67,15,94,03,80,04,62,16,14,09,53,56,92),
(16,39,05,42,96,35,31,47,55,58,88,24,00,17,54,24,36,29,85,57),
(86,56,00,48,35,71,89,07,05,44,44,37,44,60,21,58,51,54,17,58),
(19,80,81,68,05,94,47,69,28,73,92,13,86,52,17,77,04,89,55,40),
(04,52,08,83,97,35,99,16,07,97,57,32,16,26,26,79,33,27,98,66),
(88,36,68,87,57,62,20,72,03,46,33,67,46,55,12,32,63,93,53,69),
(04,42,16,73,38,25,39,11,24,94,72,18,08,46,29,32,40,62,76,36),
(20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,04,36,16),
(20,73,35,29,78,31,90,01,74,31,49,71,48,86,81,16,23,57,05,54),
(01,70,54,71,83,51,54,69,16,92,33,48,61,43,52,01,89,19,67,48));
 
 
 
function GetAdjacentProduct(Grid: T2DGrid; P: TPoint): integer;
{Get adjacent products from Point P}
var X,Y,Best1,Best2: integer;
var P2: TPoint;
begin
{Get the value at the target point}
Best1:=Grid[P.X,P.Y];
Best2:=Grid[P.X,P.Y];
{Multiply by next 3 elements to the right}
for X:=1 to 3 do
if (P.X+X)<High(Grid) then
Best1:=Best1 * Grid[P.X+X,P.Y];
{Multiply by next 3 elements down}
for Y:=1 to 3 do
if (P.Y+Y)<High(Grid) then
Best2:=Best2 * Grid[P.X,P.Y+Y];
{Return the best one}
if Best1>Best2 then Result:=Best1
else Result:=Best2;
end;
 
 
function GetBestGridProduct(Grid: T2DGrid): integer;
{Look at all positions in the grid and find largest product}
var P: integer;
var X,Y: integer;
begin
Result:=0;
for Y:=0 to High(Grid) do
for X:=0 to High(Grid[0]) do
begin
P:=GetAdjacentProduct(Grid,Point(X,Y));
if P>Result then Result:=P;
end;
end;
 
procedure TestGridProduct(Memo: TMemo);
{Run test problem}
var Best: integer;
begin
Best:=GetBestGridProduct(PGrid);
Memo.Lines.Add(IntToStr(Best));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
51267216
</pre>
 
 
=={{header|EasyLang}}==
<syntaxhighlight>
repeat
s$ = input
until s$ = ""
m[][] &= number strsplit s$ " "
.
for i to 20
for j to 16
p = 1
p2 = 1
for k to 4
p *= m[i][j + k]
p2 *= m[j + k][i]
.
max = higher max p
max = higher max p2
.
.
print max
#
input_data
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
</syntaxhighlight>
{{out}}
<pre>
51267216
</pre>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Largest product in a grid. Nigel Galloway: December 30th., 2021
let N=[|8; 2;22;97;38;15; 0;40; 0;75; 4; 5; 7;78;52;12;50;77;91; 8;
Line 292 ⟶ 482:
 
printfn "%d" (seq{for n in 0..19 do for g in 0..16 do let n=n*20 in yield N.[n+g]*N.[n+g+1]*N.[n+g+2]*N.[n+g+3]; for n in 0..19 do for g in 0..16 do let g=g*20 in yield N.[n+g]*N.[n+g+20]*N.[n+g+40]*N.[n+g+60]}|>Seq.max)
</syntaxhighlight>
</lang>
{{out}}
<pre>
51267216
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: grouping kernel math.matrices math.order prettyprint
sequences ;
 
Line 331 ⟶ 522:
} 20 group
 
4 max-product .</langsyntaxhighlight>
{{out}}
<pre>
Line 338 ⟶ 529:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">data 08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08
data 49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00
data 81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,03,49,13,36,65
Line 394 ⟶ 585:
 
print "The largest product was ";champ;" at row ";cr;" and column ";cc;", reading ";
if across then print "across." else print "down."</langsyntaxhighlight>
{{out}}<pre>The largest product was 51267216 at row 7 and column 16, reading down.</pre>
 
Line 401 ⟶ 592:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 486 ⟶ 677:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 493 ⟶ 684:
66 x 91 x 88 x 97 = 51,267,216
at indices (one based): (7, 16) (8, 16) (9, 16) (10, 16)
</pre>
 
=={{header|Haskell}}==
<syntaxhighlight lang=Haskell>import Data.List.Split ( divvy )
import Data.List ( transpose )
 
grid :: [String]
grid =["08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08" ,
"49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00",
"81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65",
"52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 9",
"22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80",
"24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50",
"32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70",
"67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21",
"24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72",
"21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95",
"78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92",
"16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57",
"86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58",
"19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40",
"04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66",
"88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69",
"04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36",
"20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16",
"20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54",
"01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"]
 
toMatrix :: [String] -> [[Int]]
toMatrix = map ( map read . words )
 
maxHorizontal :: [[Int]] -> Int
maxHorizontal = maximum . map product . divvy 4 1 . concat
 
maxTotal :: [[Int]] -> Int
maxTotal matrix = max ( maxHorizontal matrix ) ( maxHorizontal $ transpose
matrix )
 
main :: IO ( )
main = do
print $ maxTotal $ toMatrix grid
</syntaxhighlight>
 
{{out}}
<pre>
51267216
</pre>
 
=={{header|J}}==
 
First, the "hard part" -- represent the grid itself:
 
<syntaxhighlight lang=J>grid=: ".>cutLF{{)n
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
}}</syntaxhighlight>
 
With that out of the way, we find all products of four numbers and then find the largest of those:
 
<syntaxhighlight lang=J> >./,4 */\ (,.|:)grid
51267216</syntaxhighlight>
 
If we wanted to find which four numbers formed this product, we could do a little more work:
 
<syntaxhighlight lang=J> ($ #: I.@,) (= >./@,)4 */\ (,.|:)grid
6 15</syntaxhighlight>
 
This tells us that the four number sequence started at row 6 (row 0 is the first row) and column 15. This also means that the numbers extend downwards from there. (If the column index had been 20 or higher, the number sequence would have come from the transposed copy of the array, so they would have been arranged left to right in the original 'grid'.)
 
In other words:
 
<syntaxhighlight lang=J> (6 7 8 9 ,&.> 15) { grid
66 91 88 97
*/66 91 88 97
51267216</syntaxhighlight>
 
=={{header|jq}}==
'''Works with both jq and gojq, the C and Go implementations of jq'''
 
In Part 1 below, a simple solution to the basic problem, namely finding the
maximum value, is presented.
 
In Part 2, the focus shifts to reporting the location and direction of the
elements with the maximal product. Arrays are indexed from 0.
 
In both cases, it is assumed that `grid` has been defined as a
function returning the grid as an array of arrays as presented, for
example, in the [[#Wren|Wren]] entry.
'''Generic Utilities'''
<syntaxhighlight lang=jq>
def prod(s): reduce s as $x (1; . * $x);
def prod: prod(.[]);
 
# Input: an array
# Output: a stream of arrays
def windows($size): range(0; 1+length-$size) as $i | .[$i:$i+$size];
</syntaxhighlight>
===Part 1===
<syntaxhighlight lang=jq>
# Input: a matrix
def largest_product($size):
([.[] | (windows($size) | prod)] | max) as $rowmax
| ([transpose[] | (windows($size) | prod)] | max) as $colmax
| [$rowmax, $colmax]|max,
if ($rowmax > $colmax) then "The rows have it." else "The columns
have it." end ;
 
grid | largest_product(4)
</syntaxhighlight>
{{output}}
<pre>
51267216
The columns have it.
</pre>
===Part 2===
<syntaxhighlight lang=jq>
# Input: a row
# Output: [$i, $maxproduct]
def largest_product_of_row($size):
[range(0; 1 + length - $size) as $i
| [$i, (.[$i:$i+$size] | prod)] ] | max_by(.[1]);
 
# Input: a matrix
def largest_product_of_rows($size):
[range(0; length) as $row
| [$row, (.[$row] | largest_product_of_row($size)) ]] | max_by(.[1][1])
| [ .[0], .[1][]] ;
 
# Input: a matrix
def largest_product_with_details($size):
largest_product_of_rows($size) as [$row, $rowcol, $rmax]
| (transpose | largest_product_of_rows($size)) as [$col, $colrow, $cmax]
| if $rmax == $cmax
then "row-wise at \([$row, $rowcol]) equals col-wise at \([$col, $colrow]): \($cmax)"
elif $rmax > $cmax then "The rows have it at \([$row, $rowcol]): \($rmax)"
else "The columns have it at \([$colrow, $col]): \($cmax)"
end ;
 
grid | largest_product_with_details(4)
</syntaxhighlight>
{{output}}
<pre>
The columns have it at [6,15]: 51267216
</pre>
 
=={{header|Julia}}==
First, a quick method, which does not reveal the product locations:
<langsyntaxhighlight lang="julia">mat = [
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
Line 523 ⟶ 875:
maximum([prod(mat[i:i+3, j]) for i in 1:17, j in 1:20]))
println("The maximum product of 4 adjacent horizontal or vertical in the matrix is: $x")
</langsyntaxhighlight>{{out}}
<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:
<langsyntaxhighlight lang="julia">function maxprod(mat, len)
nrow, ncol = size(mat)
maxprod, maxrow, maxcol, arr = 0, 0:0, 0:0, [0]
Line 547 ⟶ 899:
 
maxprod(mat, 4)
</langsyntaxhighlight>{{out}}
<pre>The maximum product is 51267216, product of [66, 91, 88, 97] at row 7:10, col 16:16</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">array = {
{08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12,
50, 77, 91, 08},
Line 594 ⟶ 947:
maxProduct[x_List, n_] := Max[Times @@@ Partition[x, n, 1]]
Max@Join[maxProduct[#, 4] & /@ array,
maxProduct[#, 4] & /@ Transpose[array]]</langsyntaxhighlight>
 
{{out}}<pre>51267216</pre>
 
=={{header|ooRexx}}==
<langsyntaxhighlight lang="oorexx">/* REXX */
a.1=.array~of(08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08)
a.2=.array~of(49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00)
Line 651 ⟶ 1,004:
Parse Var rc row col
Say 'Maximum in column' col 'rows' row '...' (row+3)
Say l</langsyntaxhighlight>
{{out}}<pre>Maximum in row 9 columns 11 ... 14 : 78*78*96*83=48477312
Maximum in column 16 rows 7 ... 10 : 66*91*88*97=51267216</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Largest_product_in_a_grid
Line 691 ⟶ 1,044:
while /(?=(\d\d)$gap(\d\d)$gap(\d\d)$gap(\d\d))/g;
}
print "max is $score\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 699 ⟶ 1,052:
=== Generalized ===
Handles non-square input (both narrow and wide).
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 741 ⟶ 1,094:
}
 
say "Largest product of $_ adjacent elements: " . max max_products($_,@m), max_products($_,@mt) for 1..6;</langsyntaxhighlight>
{{out}}
<pre>Largest product of 1 adjacent elements: 99
Line 751 ⟶ 1,104:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">splint</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
Line 802 ⟶ 1,155:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The largest product of length %d is %,d in %s starting at %d,%d\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">&</span><span style="color: #000000;">gridmax</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 815 ⟶ 1,168:
=={{header|Python}}==
{{trans|Julia}}
<langsyntaxhighlight lang="python">""" Rosetta code task: Largest_product_in_a_grid """
 
from math import prod
Line 863 ⟶ 1,216:
for n in range(2, 6):
maxproduct(MATRIX, n)
</langsyntaxhighlight>{{out}}
<pre>
The max 2-product is 9215, product of [95, 97] at row [7, 9], col 8.
Line 870 ⟶ 1,223:
The max 5-product is 2326829868, product of [62, 99, 69, 82, 67] at row 17, col [9, 14].
</pre>
 
=={{header|Quackery}}==
 
<code>transpose</code> is defined at [[Matrix transposition#Quackery]].
 
<syntaxhighlight lang="Quackery"> [ 1 swap witheach * ] is product ( [ --> n )
 
[ 4 split
over product
unrot witheach
[ join behead drop
tuck product
max swap ]
drop ] is 4*max ( [ --> n )
 
' [ [ 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 ]
[ 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 ]
[ 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 ]
[ 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 ]
[ 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 ]
[ 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 ]
[ 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 ]
[ 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 ]
[ 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 ]
[ 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 ]
[ 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 ]
[ 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 ]
[ 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 ]
[ 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 ]
[ 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 ]
[ 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 ]
[ 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 ]
[ 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 ]
[ 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 ]
[ 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 ] ]
 
0 over
witheach [ 4*max max ]
swap transpose
witheach [ 4*max max ]
echo</syntaxhighlight>
 
{{out}}
 
<pre>51267216</pre>
 
=={{header|Raku}}==
General solution. No hard coded values. Works with any size matrix, configurable number of terms.
<syntaxhighlight lang="raku" perl6line>my @matrix = q:to/END/.lines».words;
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
Line 901 ⟶ 1,299:
@matrix.rotor($terms => -$terms+1).flat»[$_].batch($terms)».reduce(&[*]), # vertical
@matrix[$_].rotor($terms => -$terms+1)».reduce(&[*]); # horizontal
}</langsyntaxhighlight>
{{out}}
<pre>Largest product of 4 adjacent elements: 51267216</pre>
 
=={{header|REXX}}==
<langsyntaxhighlight lang="rexx">/* REXX */
Call mk_a 1,08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08
Call mk_a 2,49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00
Line 960 ⟶ 1,358:
a.row.col=arg(col+1)
End
Return</langsyntaxhighlight>
{{out}}<pre> Maximum in row 9 columns 11 ... 14 : 78*78*96*83=48477312
Maximum in column 16 rows 7 ... 10 : 66*91*88*97=51267216</pre>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Largest product is:" + nl
Line 1,048 ⟶ 1,446:
 
see nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,056 ⟶ 1,454:
Indices = (7,16)(8,16)(9,16)(10,16)
done...
</pre>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">gridstr =
"08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"
 
grid = gridstr.lines.map{|line| line.split.map(&:to_i) }
hor_ver = grid.each + grid.transpose.each
puts hor_ver.map{|line| line.each_cons(4).map{|slice| slice.inject(&:*) }.max}.max
</syntaxhighlight>
{{out}}
<pre>51267216
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var text = <<'EOT'
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
EOT
 
func horizontal(N, i, j, matrix) {
N.of {|k| matrix[i][j+k] }
}
 
func diagonal(N, i, j, matrix) {
N.of {|k| matrix[i+k][j+k] }
}
 
var matrix = Matrix(text.lines.map{ .nums }...)
 
var reversed_matrix = matrix.horizontal_flip
var transposed_matrix = matrix.transpose
 
define (
CHECK_DIAGONALS = false # true to also check diagonals
)
 
const e = matrix.end
 
for N in (1..6) {
 
var products = gather {
for i in (0..e), j in (0..e) {
 
(j+N < e) || next
 
# Horizontal and vertical
take(horizontal(N, i, j, matrix))
take(horizontal(N, i, j, transposed_matrix))
 
CHECK_DIAGONALS || next
(i+N < e) || next
 
# Left-to-right and right-to-left diagonals
take(diagonal(N, i, j, matrix))
take(diagonal(N, i, j, reversed_matrix))
}
}
 
var nums = products.max_by { .prod }
say "Largest product of #{N} adjacent elements: prod(#{nums}) = #{nums.prod}"
}</syntaxhighlight>
{{out}}
<pre>
Largest product of 1 adjacent elements: prod([99]) = 99
Largest product of 2 adjacent elements: prod([95, 97]) = 9215
Largest product of 3 adjacent elements: prod([91, 88, 97]) = 776776
Largest product of 4 adjacent elements: prod([66, 91, 88, 97]) = 51267216
Largest product of 5 adjacent elements: prod([62, 99, 69, 82, 67]) = 2326829868
Largest product of 6 adjacent elements: prod([99, 69, 82, 67, 59, 85]) = 188210512710
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var grid = [
Line 1,130 ⟶ 1,635:
for (c in maxC1..maxC2) Fmt.write("($d, $d) ", r+1, c+1)
}
System.print()</langsyntaxhighlight>
 
{{out}}
Line 1,140 ⟶ 1,645:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int Grid, Max, Prod, I, J, K;
[Grid:=[[08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08],
[49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00],
Line 1,179 ⟶ 1,684:
];
IntOut(0, Max);
]</langsyntaxhighlight>
 
{{out}}
1,983

edits