Largest product in a grid: Difference between revisions

Content added Content deleted
(Added Sidef)
m (syntax highlighting fixup automation)
Line 33: Line 33:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F maxproduct(mat, length)
<syntaxhighlight lang="11l">F maxproduct(mat, length)
‘ find the largest product of len length horizontal or vertical length in matrix ’
‘ find the largest product of len length horizontal or vertical length in matrix ’
V (nrow, ncol) = (mat.len, mat[0].len)
V (nrow, ncol) = (mat.len, mat[0].len)
Line 76: Line 76:


L(n) 2..5
L(n) 2..5
maxproduct(MATRIX, n)</lang>
maxproduct(MATRIX, n)</syntaxhighlight>


{{out}}
{{out}}
Line 87: Line 87:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # find the maximum product of 4 adjacent numbers in a row or column of a matrix #
<syntaxhighlight 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 )
[,]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 )
, ( 49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00 )
Line 141: Line 141:
)
)
)
)
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 148: Line 148:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>Grid =
<syntaxhighlight lang="autohotkey">Grid =
(
(
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
Line 198: Line 198:
}
}
}
}
MsgBox, 262144, ,% result := "Max Product = " maxProd . Steps[maxProd]</lang>
MsgBox, 262144, ,% result := "Max Product = " maxProd . Steps[maxProd]</syntaxhighlight>
{{out}}
{{out}}
<pre>Max Product = 51267216
<pre>Max Product = 51267216
66*91*88*97 @ Row 7 - Row 10, Col 16</pre>
66*91*88*97 @ Row 7 - Row 10, Col 16</pre>
=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f LARGEST_PRODUCT_IN_A_GRID.AWK
# syntax: GAWK -f LARGEST_PRODUCT_IN_A_GRID.AWK
BEGIN {
BEGIN {
Line 261: Line 261:
exit(0)
exit(0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 268: Line 268:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Largest product in a grid. Nigel Galloway: December 30th., 2021
// 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;
let N=[|8; 2;22;97;38;15; 0;40; 0;75; 4; 5; 7;78;52;12;50;77;91; 8;
Line 292: Line 292:


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)
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}}
{{out}}
<pre>
<pre>
Line 299: Line 299:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
{{works with|Factor|0.99 2021-06-02}}
<lang factor>USING: grouping kernel math.matrices math.order prettyprint
<syntaxhighlight lang="factor">USING: grouping kernel math.matrices math.order prettyprint
sequences ;
sequences ;


Line 331: Line 331:
} 20 group
} 20 group


4 max-product .</lang>
4 max-product .</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 338: Line 338:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>data 08,02,22,97,38,15,00,40,00,75,04,05,07,78,52,12,50,77,91,08
<syntaxhighlight 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 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
data 81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,03,49,13,36,65
Line 394: Line 394:


print "The largest product was ";champ;" at row ";cr;" and column ";cc;", reading ";
print "The largest product was ";champ;" at row ";cr;" and column ";cc;", reading ";
if across then print "across." else print "down."</lang>
if across then print "across." else print "down."</syntaxhighlight>
{{out}}<pre>The largest product was 51267216 at row 7 and column 16, reading down.</pre>
{{out}}<pre>The largest product was 51267216 at row 7 and column 16, reading down.</pre>


Line 401: Line 401:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 486: Line 486:
}
}
fmt.Println()
fmt.Println()
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 497: Line 497:
=={{header|Julia}}==
=={{header|Julia}}==
First, a quick method, which does not reveal the product locations:
First, a quick method, which does not reveal the product locations:
<lang julia>mat = [
<syntaxhighlight lang="julia">mat = [
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
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
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
Line 523: Line 523:
maximum([prod(mat[i:i+3, j]) for i in 1:17, j in 1:20]))
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")
println("The maximum product of 4 adjacent horizontal or vertical in the matrix is: $x")
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>The maximum product of 4 adjacent horizontal or vertical in the matrix is: 51267216</pre>
<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:
Alternatively, to get the position of the maximum product:
<lang julia>function maxprod(mat, len)
<syntaxhighlight lang="julia">function maxprod(mat, len)
nrow, ncol = size(mat)
nrow, ncol = size(mat)
maxprod, maxrow, maxcol, arr = 0, 0:0, 0:0, [0]
maxprod, maxrow, maxcol, arr = 0, 0:0, 0:0, [0]
Line 547: Line 547:


maxprod(mat, 4)
maxprod(mat, 4)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>The maximum product is 51267216, product of [66, 91, 88, 97] at row 7:10, col 16:16</pre>
<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}}==
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<lang Mathematica>array = {
<syntaxhighlight lang="mathematica">array = {
{08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12,
{08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12,
50, 77, 91, 08},
50, 77, 91, 08},
Line 594: Line 594:
maxProduct[x_List, n_] := Max[Times @@@ Partition[x, n, 1]]
maxProduct[x_List, n_] := Max[Times @@@ Partition[x, n, 1]]
Max@Join[maxProduct[#, 4] & /@ array,
Max@Join[maxProduct[#, 4] & /@ array,
maxProduct[#, 4] & /@ Transpose[array]]</lang>
maxProduct[#, 4] & /@ Transpose[array]]</syntaxhighlight>


{{out}}<pre>51267216</pre>
{{out}}<pre>51267216</pre>


=={{header|ooRexx}}==
=={{header|ooRexx}}==
<lang oorexx>/* REXX */
<syntaxhighlight 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.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)
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: Line 651:
Parse Var rc row col
Parse Var rc row col
Say 'Maximum in column' col 'rows' row '...' (row+3)
Say 'Maximum in column' col 'rows' row '...' (row+3)
Say l</lang>
Say l</syntaxhighlight>
{{out}}<pre>Maximum in row 9 columns 11 ... 14 : 78*78*96*83=48477312
{{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>
Maximum in column 16 rows 7 ... 10 : 66*91*88*97=51267216</pre>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Largest_product_in_a_grid
use strict; # https://rosettacode.org/wiki/Largest_product_in_a_grid
Line 691: Line 691:
while /(?=(\d\d)$gap(\d\d)$gap(\d\d)$gap(\d\d))/g;
while /(?=(\d\d)$gap(\d\d)$gap(\d\d)$gap(\d\d))/g;
}
}
print "max is $score\n";</lang>
print "max is $score\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 699: Line 699:
=== Generalized ===
=== Generalized ===
Handles non-square input (both narrow and wide).
Handles non-square input (both narrow and wide).
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 741: Line 741:
}
}


say "Largest product of $_ adjacent elements: " . max max_products($_,@m), max_products($_,@mt) for 1..6;</lang>
say "Largest product of $_ adjacent elements: " . max max_products($_,@m), max_products($_,@mt) for 1..6;</syntaxhighlight>
{{out}}
{{out}}
<pre>Largest product of 1 adjacent elements: 99
<pre>Largest product of 1 adjacent elements: 99
Line 751: Line 751:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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: Line 802:
<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: #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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 815: Line 815:
=={{header|Python}}==
=={{header|Python}}==
{{trans|Julia}}
{{trans|Julia}}
<lang python>""" Rosetta code task: Largest_product_in_a_grid """
<syntaxhighlight lang="python">""" Rosetta code task: Largest_product_in_a_grid """


from math import prod
from math import prod
Line 863: Line 863:
for n in range(2, 6):
for n in range(2, 6):
maxproduct(MATRIX, n)
maxproduct(MATRIX, n)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
The max 2-product is 9215, product of [95, 97] at row [7, 9], col 8.
The max 2-product is 9215, product of [95, 97] at row [7, 9], col 8.
Line 873: Line 873:
=={{header|Raku}}==
=={{header|Raku}}==
General solution. No hard coded values. Works with any size matrix, configurable number of terms.
General solution. No hard coded values. Works with any size matrix, configurable number of terms.
<lang perl6>my @matrix = q:to/END/.lines».words;
<syntaxhighlight lang="raku" line>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
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
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
Line 901: Line 901:
@matrix.rotor($terms => -$terms+1).flat»[$_].batch($terms)».reduce(&[*]), # vertical
@matrix.rotor($terms => -$terms+1).flat»[$_].batch($terms)».reduce(&[*]), # vertical
@matrix[$_].rotor($terms => -$terms+1)».reduce(&[*]); # horizontal
@matrix[$_].rotor($terms => -$terms+1)».reduce(&[*]); # horizontal
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Largest product of 4 adjacent elements: 51267216</pre>
<pre>Largest product of 4 adjacent elements: 51267216</pre>


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/* REXX */
<syntaxhighlight 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 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
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: Line 960:
a.row.col=arg(col+1)
a.row.col=arg(col+1)
End
End
Return</lang>
Return</syntaxhighlight>
{{out}}<pre> Maximum in row 9 columns 11 ... 14 : 78*78*96*83=48477312
{{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>
Maximum in column 16 rows 7 ... 10 : 66*91*88*97=51267216</pre>


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
see "working..." + nl
see "working..." + nl
see "Largest product is:" + nl
see "Largest product is:" + nl
Line 1,048: Line 1,048:


see nl + "done..." + nl
see nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,059: Line 1,059:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var text = <<'EOT'
<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
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
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
Line 1,123: Line 1,123:
var nums = products.max_by { .prod }
var nums = products.max_by { .prod }
say "Largest product of #{N} adjacent elements: prod(#{nums}) = #{nums.prod}"
say "Largest product of #{N} adjacent elements: prod(#{nums}) = #{nums.prod}"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,136: Line 1,136:
=={{header|Wren}}==
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "./fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "./fmt" for Fmt


var grid = [
var grid = [
Line 1,206: Line 1,206:
for (c in maxC1..maxC2) Fmt.write("($d, $d) ", r+1, c+1)
for (c in maxC1..maxC2) Fmt.write("($d, $d) ", r+1, c+1)
}
}
System.print()</lang>
System.print()</syntaxhighlight>


{{out}}
{{out}}
Line 1,216: Line 1,216:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>int Grid, Max, Prod, I, J, K;
<syntaxhighlight lang="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],
[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],
[49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,04,56,62,00],
Line 1,255: Line 1,255:
];
];
IntOut(0, Max);
IntOut(0, Max);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}