Largest product in a grid: Difference between revisions
m
syntax highlighting fixup automation
(Added Sidef) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 33:
{{trans|Python}}
<
‘ 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)</
{{out}}
Line 87:
=={{header|ALGOL 68}}==
<
[,]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</
{{out}}
<pre>
Line 148:
=={{header|AutoHotkey}}==
<
(
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
Line 198:
}
}
MsgBox, 262144, ,% result := "Max Product = " maxProd . Steps[maxProd]</
{{out}}
<pre>Max Product = 51267216
66*91*88*97 @ Row 7 - Row 10, Col 16</pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f LARGEST_PRODUCT_IN_A_GRID.AWK
BEGIN {
Line 261:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 268:
=={{header|F_Sharp|F#}}==
<
// 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:
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>
{{out}}
<pre>
Line 299:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
sequences ;
Line 331:
} 20 group
4 max-product .</
{{out}}
<pre>
Line 338:
=={{header|FreeBASIC}}==
<
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:
print "The largest product was ";champ;" at row ";cr;" and column ";cc;", reading ";
if across then print "across." else print "down."</
{{out}}<pre>The largest product was 51267216 at row 7 and column 16, reading down.</pre>
Line 401:
{{trans|Wren}}
{{libheader|Go-rcu}}
<
import (
Line 486:
}
fmt.Println()
}</
{{out}}
Line 497:
=={{header|Julia}}==
First, a quick method, which does not reveal the product locations:
<
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:
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")
</
<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:
<
nrow, ncol = size(mat)
maxprod, maxrow, maxcol, arr = 0, 0:0, 0:0, [0]
Line 547:
maxprod(mat, 4)
</
<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}}==
<
{08, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 52, 12,
50, 77, 91, 08},
Line 594:
maxProduct[x_List, n_] := Max[Times @@@ Partition[x, n, 1]]
Max@Join[maxProduct[#, 4] & /@ array,
maxProduct[#, 4] & /@ Transpose[array]]</
{{out}}<pre>51267216</pre>
=={{header|ooRexx}}==
<
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:
Parse Var rc row col
Say 'Maximum in column' col 'rows' row '...' (row+3)
Say l</
{{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}}==
<
use strict; # https://rosettacode.org/wiki/Largest_product_in_a_grid
Line 691:
while /(?=(\d\d)$gap(\d\d)$gap(\d\d)$gap(\d\d))/g;
}
print "max is $score\n";</
{{out}}
<pre>
Line 699:
=== Generalized ===
Handles non-square input (both narrow and wide).
<
use warnings;
use feature 'say';
Line 741:
}
say "Largest product of $_ adjacent elements: " . max max_products($_,@m), max_products($_,@mt) for 1..6;</
{{out}}
<pre>Largest product of 1 adjacent elements: 99
Line 751:
=={{header|Phix}}==
<!--<
<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:
<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>
<!--</
{{out}}
<pre>
Line 815:
=={{header|Python}}==
{{trans|Julia}}
<
from math import prod
Line 863:
for n in range(2, 6):
maxproduct(MATRIX, n)
</
<pre>
The max 2-product is 9215, product of [95, 97] at row [7, 9], col 8.
Line 873:
=={{header|Raku}}==
General solution. No hard coded values. Works with any size matrix, configurable number of terms.
<syntaxhighlight lang="raku"
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:
@matrix.rotor($terms => -$terms+1).flat»[$_].batch($terms)».reduce(&[*]), # vertical
@matrix[$_].rotor($terms => -$terms+1)».reduce(&[*]); # horizontal
}</
{{out}}
<pre>Largest product of 4 adjacent elements: 51267216</pre>
=={{header|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:
a.row.col=arg(col+1)
End
Return</
{{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}}==
<
see "working..." + nl
see "Largest product is:" + nl
Line 1,048:
see nl + "done..." + nl
</syntaxhighlight>
{{out}}
<pre>
Line 1,059:
=={{header|Sidef}}==
<
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,123:
var nums = products.max_by { .prod }
say "Largest product of #{N} adjacent elements: prod(#{nums}) = #{nums.prod}"
}</
{{out}}
<pre>
Line 1,136:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<
var grid = [
Line 1,206:
for (c in maxC1..maxC2) Fmt.write("($d, $d) ", r+1, c+1)
}
System.print()</
{{out}}
Line 1,216:
=={{header|XPL0}}==
<
[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,255:
];
IntOut(0, Max);
]</
{{out}}
|