Jump to content

Largest product in a grid: Difference between revisions

m
syntax highlighting fixup automation
(Added Sidef)
m (syntax highlighting fixup automation)
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>
Line 148:
 
=={{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:
}
}
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:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 268:
 
=={{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:
 
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>
Line 299:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: grouping kernel math.matrices math.order prettyprint
sequences ;
 
Line 331:
} 20 group
 
4 max-product .</langsyntaxhighlight>
{{out}}
<pre>
Line 338:
 
=={{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:
 
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:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 486:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 497:
=={{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:
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:
 
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:
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:
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:
while /(?=(\d\d)$gap(\d\d)$gap(\d\d)$gap(\d\d))/g;
}
print "max is $score\n";</langsyntaxhighlight>
{{out}}
<pre>
Line 699:
=== Generalized ===
Handles non-square input (both narrow and wide).
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 741:
}
 
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:
 
=={{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:
<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:
=={{header|Python}}==
{{trans|Julia}}
<langsyntaxhighlight lang="python">""" Rosetta code task: Largest_product_in_a_grid """
 
from math import prod
Line 863:
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 873:
=={{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:
@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:
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:
 
see nl + "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,059:
 
=={{header|Sidef}}==
<langsyntaxhighlight 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
Line 1,123:
var nums = products.max_by { .prod }
say "Largest product of #{N} adjacent elements: prod(#{nums}) = #{nums.prod}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,136:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "./fmt" for Fmt
 
var grid = [
Line 1,206:
for (c in maxC1..maxC2) Fmt.write("($d, $d) ", r+1, c+1)
}
System.print()</langsyntaxhighlight>
 
{{out}}
Line 1,216:
 
=={{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,255:
];
IntOut(0, Max);
]</langsyntaxhighlight>
 
{{out}}
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.