Minimum number of cells after, before, above and below NxN squares: Difference between revisions

Added Easylang
(Add COBOL)
(Added Easylang)
 
(31 intermediate revisions by 16 users not shown)
Line 4:
Find and show on this page the minimum number of cells after, before, above and below   '''N×N'''   squares,   where   '''N = 10'''.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F min_cells_matrix(siz)
R (0 .< siz).map(row -> (0 .< @siz).map(col -> min(@row, col, @@siz - @row - 1, @@siz - col - 1)))
 
F display_matrix(mat)
V siz = mat.len
V spaces = I siz < 20 {2} E I siz < 200 {3} E 4
print("\nMinimum number of cells after, before, above and below "siz‘ x ’siz‘ square:’)
L(row) 0 .< siz
print(mat[row].map(n -> String(n).rjust(@spaces)).join(‘’))
 
L(siz) [23, 10, 9, 2, 1]
display_matrix(min_cells_matrix(siz))</syntaxhighlight>
 
{{out}}
<pre>
 
Minimum number of cells after, before, above and below 23 x 23 square:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0
0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0
0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 9 9 9 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 10 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 10 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 9 9 9 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0
0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 
Minimum number of cells after, before, above and below 10 x 10 square:
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
 
Minimum number of cells after, before, above and below 9 x 9 square:
0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0
 
Minimum number of cells after, before, above and below 2 x 2 square:
0 0
0 0
 
Minimum number of cells after, before, above and below 1 x 1 square:
0
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Wren}}
As with the Algol W version, the cells are printed as they are calculated. Ensures the counts are shown in the same width.
<langsyntaxhighlight lang="algol68">BEGIN # show the minimum number of cells above, below, before and after each #
# cell in a suare matrix #
 
Line 42 ⟶ 117:
OD
 
END</langsyntaxhighlight>
{{out}}
<pre>
Line 102 ⟶ 177:
{{Trans|Wren}}
This version avoids generating an explicit list of elements for each row in the matrix and just prints the elements as they are calculated. The elements are all shown in the same field width.
<langsyntaxhighlight lang="algolw">begin % show the minimum number of cells above, below, before and after each %
% cell in a square matrix %
 
Line 130 ⟶ 205:
for n := 10, 9, 2, 1 do printMinCells( n )
 
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 188 ⟶ 263:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight lang="apl">n_by_n ← (⌽⌊⊖)∘(∘.⌊⍨¯1+⍳)
n_by_n¨ 2 3 9 10</langsyntaxhighlight>
{{out}}
<pre>┌───┬─────┬─────────────────┬───────────────────┐
Line 203 ⟶ 278:
│ │ │ │0 0 0 0 0 0 0 0 0 0│
└───┴─────┴─────────────────┴───────────────────┘</pre>
 
=={{header|AppleScript}}==
<syntaxhighlight lang="applescript">use framework "Foundation"
 
------------------- DISTANCES FROM EDGE ------------------
 
-- distancesFromEdge :: Int -> [[Int]]
on distancesFromEdge(n)
-- A matrix of minimum distances to the edge.
script f
on |λ|(row, col)
minimum({row - 1, col - 1, n - row, n - col})
end |λ|
end script
matrix(n, n, f)
end distancesFromEdge
 
 
--------------------------- TEST -------------------------
on run
script test
on |λ|(n)
showMatrix(my distancesFromEdge(n)) & ¬
linefeed
end |λ|
end script
unlines(map(test, {25, 10, 9, 2, 1}))
end run
 
 
------------------------- GENERIC ------------------------
 
-- matrix :: Int -> Int -> ((Int, Int) -> a) -> [[a]]
on matrix(nRows, nCols, f)
-- A matrix of a given number of columns and rows,
-- in which each value is a given function of its
-- (zero-based) column and row indices.
script go
property g : mReturn(f)'s |λ|
on |λ|(iRow)
set xs to {}
repeat with iCol from 1 to nCols
set end of xs to g(iRow, iCol)
end repeat
xs
end |λ|
end script
map(go, enumFromTo(1, nRows))
end matrix
 
 
-- minimum :: Ord a => [a] -> a
on minimum(xs)
set ca to current application
unwrap((ca's NSArray's arrayWithArray:xs)'s ¬
valueForKeyPath:"@min.self")
end minimum
 
 
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m ≤ n then
set xs to {}
repeat with i from m to n
set end of xs to i
end repeat
xs
else
{}
end if
end enumFromTo
 
 
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
 
 
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
 
 
-- unwrap :: NSValue -> a
on unwrap(nsValue)
if nsValue is missing value then
missing value
else
set ca to current application
item 1 of ((ca's NSArray's arrayWithObject:nsValue) as list)
end if
end unwrap
 
 
------------------------ FORMATTING ----------------------
 
-- showMatrix :: [[Maybe a]] -> String
on showMatrix(rows)
-- String representation of rows
-- as a matrix.
script showRow
on |λ|(a, row)
set {maxWidth, prevRows} to a
script showCell
on |λ|(acc, cell)
set {w, xs} to acc
if missing value is cell then
{w, xs & ""}
else
set s to cell as string
{max(w, length of s), xs & s}
end if
end |λ|
end script
set {rowMax, cells} to foldl(showCell, {0, {}}, row)
{max(maxWidth, rowMax), prevRows & {cells}}
end |λ|
end script
set {w, stringRows} to foldl(showRow, {0, {}}, rows)
script go
on |λ|(row)
unwords(map(justifyRight(w, space), row))
end |λ|
end script
unlines(map(go, stringRows))
end showMatrix
 
 
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
 
 
-- justifyRight :: Int -> Char -> String -> String
on justifyRight(n, cFiller)
script
on |λ|(txt)
if n > length of txt then
text -n thru -1 of ((replicate(n, cFiller) as text) & txt)
else
txt
end if
end |λ|
end script
end justifyRight
 
 
-- max :: Ord a => a -> a -> a
on max(x, y)
if x > y then
x
else
y
end if
end max
 
 
-- Egyptian multiplication - progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for binary
-- assembly of a target length
-- replicate :: Int -> String -> String
on replicate(n, s)
-- Egyptian multiplication - progressively doubling a list,
-- appending stages of doubling to an accumulator where needed
-- for binary assembly of a target length
script p
on |λ|({n})
n ≤ 1
end |λ|
end script
script f
on |λ|({n, dbl, out})
if (n mod 2) > 0 then
set d to out & dbl
else
set d to out
end if
{n div 2, dbl & dbl, d}
end |λ|
end script
set xs to |until|(p, f, {n, s, ""})
item 2 of xs & item 3 of xs
end replicate
 
 
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines
 
 
-- until :: (a -> Bool) -> (a -> a) -> a -> a
on |until|(p, f, x)
set v to x
set mp to mReturn(p)
set mf to mReturn(f)
repeat until mp's |λ|(v)
set v to mf's |λ|(v)
end repeat
v
end |until|
 
 
-- unwords :: [String] -> String
on unwords(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, space}
set s to xs as text
set my text item delimiters to dlm
return s
end unwords</syntaxhighlight>
{{Out}}
<pre> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0
0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0
0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 10 10 10 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 11 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 12 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 11 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 10 10 10 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0
0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
 
0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0
 
0 0
0 0
 
0
</pre>
 
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">printMinCells: function [n][
cells: array.of:n 0
loop 0..dec n 'r [
loop 0..dec n 'c ->
cells\[c]: min @[dec n-r, r, c, dec n-c]
print cells
]
]
 
loop [10 9 2 1] 'n [
print ["Minimum number of cells after, before, above and below" n "x" n "square:"]
printMinCells n
print ""
]</syntaxhighlight>
 
{{out}}
 
<pre>Minimum number of cells after, before, above and below 10 x 10 square:
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
 
Minimum number of cells after, before, above and below 9 x 9 square:
0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0
 
Minimum number of cells after, before, above and below 2 x 2 square:
0 0
0 0
 
Minimum number of cells after, before, above and below 1 x 1 square:
0</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">gridSize := 10
 
grid := []
loop % gridSize {
row := A_Index
loop % gridSize {
col := A_Index
grid[row, col] := Min(row, col, gridSize+1-row, gridSize+1-col) - 1
}
}
 
for row, obj in Grid {
for col, v in obj
result .= v " "
result .= "`n"
}
MsgBox % result</syntaxhighlight>
{{out}}
<pre>0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 </pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f MINIMUM_NUMBER_OF_CELLS_AFTER_BEFORE_ABOVE_AND_BELOW_NXN_SQUARES.AWK
BEGIN {
leng = split("3,4,9,10,23",arr,",")
for (k=1; k<=leng; k++) {
n = arr[k]
printf("\nDistance to nearest edge: %dx%d\n",n,n)
for (i=1; i<=n; i++) {
for (j=1; j<=n; j++) {
printf("%2d ",min(min(i-1,n-i),min(j-1,n-j)))
}
printf("\n")
}
}
exit(0)
}
function min(x,y) { return((x < y) ? x : y) }
</syntaxhighlight>
{{out}}
<pre>
Distance to nearest edge: 3x3
0 0 0
0 1 0
0 0 0
 
Distance to nearest edge: 4x4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
 
Distance to nearest edge: 9x9
0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0
 
Distance to nearest edge: 10x10
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
 
Distance to nearest edge: 23x23
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0
0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0
0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 9 9 9 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 10 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 10 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 9 9 9 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0
0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
{{works with|Chipmunk Basic}}
<syntaxhighlight lang="qbasic">100 HOME : REM 100 CLS for Chipmunk Basic
110 n = 10
120 FOR i = 1 TO n
130 FOR j = 1 TO n
140 IF i-1 <= n-i THEN a = i-1 : GOTO 160
150 IF i-1 > n-i THEN a = n-i
160 IF j-1 <= n-j THEN b = j-1 : GOTO 180
170 IF j-1 > n-j THEN b = n-j
180 IF a <= b THEN r = a : GOTO 200
190 IF a > b THEN r = b
200 PRINT r " ";
210 NEXT j
220 PRINT
230 NEXT i
240 END</syntaxhighlight>
{{out}}
<pre>Same as Chipmunk Basic entry.</pre>
 
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">function min(a, b)
if a<=b then return a else return b
end function
 
subroutine minab(n)
for i = 1 to n
for j = 1 to n
print min(min(i-1, n-i), min(j-1, n-j)); " ";
next j
print
next i
end subroutine
 
call minab(10)
end</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|BASIC256}}
<syntaxhighlight lang="qbasic">10 cls
20 call minab(10)
30 end
40 sub min(a,b)
50 if a <= b then min = a else min = b
60 end sub
70 sub minab(n)
80 for i = 1 to n
90 for j = 1 to n
100 print using "##"; min(min(i-1,n-i),min(j-1,n-j));
110 next j
120 print
130 next i
140 end sub</syntaxhighlight>
{{out}}
<pre>Same as BASIC256 entry.</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">#define min(a, b) Iif(a<=b,a,b)
 
sub minab( n as uinteger )
for i as uinteger = 1 to n
for j as uinteger = 1 to n
print using "## ";min( min(i-1, n-i), min(j-1, n-j) );
next j
print
next i
end sub
 
minab(10)</syntaxhighlight>
{{out}}<pre>
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
</pre>
 
==={{header|GW-BASIC}}===
<syntaxhighlight lang="gwbasic">10 N = 10
20 FOR I = 0 TO N - 1
30 IF I < N - 1 - I THEN DI = I ELSE DI = N - 1 - I
40 FOR J = 0 TO N - 1
50 IF J < N - 1 - J THEN DJ = J ELSE DJ = N - 1 - J
60 IF DI < DJ THEN M = DI ELSE M = DJ
70 PRINT USING "## ";M;
80 NEXT J
90 PRINT
100 NEXT I</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{trans|GW-BASIC}}
{{works with|Commodore BASIC|3.5}}
{{works with|Nascom ROM BASIC|4.7}}
<syntaxhighlight lang="basic">
10 REM Minimum number of cells after, before, above and below NxN squares
20 LET N = 10
30 FOR I = 0 TO N-1
40 IF I < N-1-I THEN 70
50 LET D = N-1-I
60 GOTO 80
70 LET D = I
80 FOR J = 0 TO N-1
90 IF J < N-1-J THEN 120
100 LET E = N-1-J
110 GOTO 130
120 LET E = J
130 IF D < E THEN 160
140 LET M = E
150 GOTO 170
160 LET M = D
170 IF M >= 10 THEN 190
180 PRINT " ";
190 PRINT M;
200 NEXT J
210 PRINT
220 NEXT I
230 END
</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic}}
{{works with|QuickBasic}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="qbasic">DECLARE FUNCTION min! (a!, b!)
DECLARE SUB minab (n!)
 
CLS
minab (10)
END
 
FUNCTION min (a, b)
IF a <= b THEN min = a ELSE min = b
END FUNCTION
 
SUB minab (n)
FOR i = 1 TO n
FOR j = 1 TO n
PRINT USING "## "; min(min(i - 1, n - i), min(j - 1, n - j));
NEXT j
PRINT
NEXT i
END SUB</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
==={{header|RapidQ}}===
{{trans|FreeBASIC}}
Introduced extra variables <code>MinI</code> and <code>MinJ</code>, because nested <code>Min</code> functions do not work correctly (why do they not?).
<syntaxhighlight lang="xbasic">
' Minimum number of cells after, before, above and below NxN squares
DECLARE FUNCTION Min(A AS WORD, B AS WORD) AS WORD
DECLARE SUB MinAB(N AS WORD)
 
CLS
MinAB(10)
END
 
FUNCTION Min(A AS WORD, B AS WORD) AS WORD
IF A <= B THEN Min = A ELSE Min = B
END FUNCTION
 
SUB MinAB(N AS WORD)
FOR I = 1 TO N
MinI = Min(I - 1, N - I)
FOR J = 1 TO N
MinJ = Min(J - 1, N - J)
PRINT FORMAT$("%2d ", Min(MinI, MinJ));
NEXT J
PRINT
NEXT I
END SUB
</syntaxhighlight>
{{out}}
To samo, co we FreeBASIC.
<pre>
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
</pre>
 
==={{header|Tiny BASIC}}===
{{trans|Minimal BASIC}}
{{works with|TinyBasic}}
<syntaxhighlight lang="basic">
10 REM Minimum number of cells after, before, above and below NxN squares
20 LET N=10
30 LET I=0
40 IF I<N-1-I THEN GOTO 70
50 LET D=N-1-I
60 GOTO 80
70 LET D=I
80 LET J=0
90 IF J<N-1-J THEN GOTO 120
100 LET E=N-1-J
110 GOTO 130
120 LET E=J
130 IF D<E THEN GOTO 160
140 LET M=E
150 GOTO 170
160 LET M=D
170 IF M<10 THEN PRINT " ";
180 PRINT M;" ";
190 LET J=J+1
200 IF J=N THEN GOTO 220
210 GOTO 90
220 PRINT
230 LET I=I+1
240 IF I=N THEN GOTO 260
250 GOTO 40
260 END
</syntaxhighlight>
{{out}}
<pre>
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
</pre>
 
==={{header|True BASIC}}===
{{trans|QBasic}}
<syntaxhighlight lang="qbasic">FUNCTION min (a, b)
IF a <= b THEN LET min = a ELSE LET min = b
END FUNCTION
 
SUB minab (n)
FOR i = 1 TO n
FOR j = 1 TO n
PRINT USING "## ": min(min(i - 1, n - i), min(j - 1, n - j));
NEXT j
PRINT
NEXT i
END SUB
 
CALL minab (10)
END</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de QBasic.
</pre>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">minab(10)
end
 
sub minab(n)
for i = 1 to n
for j = 1 to n
print min(min(i-1, n-i), min(j-1, n-j)) using ("##");
next j
print
next i
end sub</syntaxhighlight>
{{out}}
<pre>
Igual que la entrada de FreeBASIC.
</pre>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let min(a,b) = a<b -> a, b
Line 216 ⟶ 1,043:
$)
let start() be minNbyN(10, 3)</langsyntaxhighlight>
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 230 ⟶ 1,057:
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn">NByN ← (⌽⌊⌽⎉1)∘(⌊⌜˜ ∘↕)⌽⊸⌊∘↕
NByN¨ 2‿3‿9‿10</langsyntaxhighlight>
{{out}}
<pre>┌─
Line 250 ⟶ 1,077:
=={{header|C}}==
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="c">#include<stdio.h>
#include<stdlib.h>
 
Line 269 ⟶ 1,096:
minab(10);
return 0;
}</langsyntaxhighlight>
{{out}}<pre>
0 0 0 0 0 0 0 0 0 0
Line 282 ⟶ 1,109:
0 0 0 0 0 0 0 0 0 0
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">min = proc [T: type] (a, b: T) returns (T)
where T has lt: proctype (T,T) returns (bool)
if a<b
then return(a)
else return(b)
end
end min
 
min_n_by_n = proc (n: int) returns (array[array[int]])
ai = array[int]
aai = array[ai]
t: aai := aai$[]
for y: int in int$from_to(0, n-1) do
aai$addh(t, ai$[])
for x: int in int$from_to(0, n-1) do
i: int := min[int](x, min[int](n-x-1, min[int](y, n-y-1)))
ai$addh(aai$top(t), i)
end
end
return(t)
end min_n_by_n
 
print_table = proc (s: stream, table: array[array[int]])
ai = array[int]
aai = array[ai]
for line: ai in aai$elements(table) do
for item: int in ai$elements(line) do
stream$puts(s, int$unparse(item) || " ")
end
stream$putl(s, "")
end
end print_table
 
start_up = proc ()
print_table(stream$primary_output(), min_n_by_n(10))
end start_up</syntaxhighlight>
{{out}}
<pre>0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0</pre>
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. MINIMUM-CELLS-N-BY-N.
Line 328 ⟶ 1,204:
CHECK-MINIMUM.
IF ITEM IS LESS THAN MIN, MOVE ITEM TO MIN.</langsyntaxhighlight>
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 340 ⟶ 1,216:
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0</pre>
 
=={{header|EasyLang}}==
{{trans|Yabasic}}
<syntaxhighlight>
proc minab n . .
for i = 1 to n
for j = 1 to n
write lower lower (i - 1) (n - i) lower (j - 1) (n - j)
.
print ""
.
.
numfmt 0 2
minab 10
</syntaxhighlight>
{{out}}
<pre>
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
</pre>
 
=={{header|Excel}}==
Line 349 ⟶ 1,253:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">=LAMBDA(n,
LET(
lastIndex, n - 1,
Line 368 ⟶ 1,272:
)(SEQUENCE(n, n, 0, 1))
)
)</langsyntaxhighlight>
 
{{Out}}
Line 728 ⟶ 1,632:
|
|}
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
The problem is not well described. You have to look at other people's code to understand the problem, which turns out to be something quite different from the description seems to say.
 
<syntaxhighlight lang="Delphi">
 
function EdgeDistance(P: TPoint; Size: integer): integer;
{Find the distance to the nearest edge}
begin
Result:=Min(Min(P.X,(Size-1)-P.X),Min(P.Y,(Size-1)-P.Y));
end;
 
 
procedure MapMatrix(Memo: TMemo; Size: integer);
{Map each cell in Size X Size matrix}
{with the distance to nearest edge}
var X,Y,E: integer;
var S: string;
begin
Memo.Lines.Add(Format('Map for %d X %d Matrix',[Size,Size]));
S:='';
for Y:=0 to Size-1 do
begin
for X:=0 to Size-1 do
begin
E:=EdgeDistance(Point(X,Y),Size);
S:=S+Format('%3d',[E]);
end;
S:=S+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
 
 
procedure ShowEdgeMaps(Memo: TMemo);
{Show a series of maps for matrices of different sizes}
var I: integer;
begin
for I:=3 to 12 do MapMatrix(Memo,I);
end;
 
</syntaxhighlight>
{{out}}
<pre>
Map for 3 X 3 Matrix
0 0 0
0 1 0
0 0 0
 
Map for 4 X 4 Matrix
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
 
Map for 5 X 5 Matrix
0 0 0 0 0
0 1 1 1 0
0 1 2 1 0
0 1 1 1 0
0 0 0 0 0
 
Map for 6 X 6 Matrix
0 0 0 0 0 0
0 1 1 1 1 0
0 1 2 2 1 0
0 1 2 2 1 0
0 1 1 1 1 0
0 0 0 0 0 0
 
Map for 7 X 7 Matrix
0 0 0 0 0 0 0
0 1 1 1 1 1 0
0 1 2 2 2 1 0
0 1 2 3 2 1 0
0 1 2 2 2 1 0
0 1 1 1 1 1 0
0 0 0 0 0 0 0
 
Map for 8 X 8 Matrix
0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 0
0 1 2 2 2 2 1 0
0 1 2 3 3 2 1 0
0 1 2 3 3 2 1 0
0 1 2 2 2 2 1 0
0 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0
 
Map for 9 X 9 Matrix
0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0
 
Map for 10 X 10 Matrix
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
 
Map for 11 X 11 Matrix
0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 3 2 1 0
0 1 2 3 4 4 4 3 2 1 0
0 1 2 3 4 5 4 3 2 1 0
0 1 2 3 4 4 4 3 2 1 0
0 1 2 3 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0
 
Map for 12 X 12 Matrix
0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 3 3 2 1 0
0 1 2 3 4 4 4 4 3 2 1 0
0 1 2 3 4 5 5 4 3 2 1 0
0 1 2 3 4 5 5 4 3 2 1 0
0 1 2 3 4 4 4 4 3 2 1 0
0 1 2 3 3 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0
</pre>
 
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Minimum number of cells after, before, above and below NxN squares. Nigel Galloway: August 1st., 2021
printfn "%A" (Array2D.init 10 10 (fun n g->List.min [n;g;9-n;9-g]))
printfn "\n%A" (Array2D.init 9 9 (fun n g->List.min [n;g;8-n;8-g]))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 761 ⟶ 1,807:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<langsyntaxhighlight lang="factor">USING: io kernel math math.matrices math.vectors prettyprint
sequences ;
 
Line 768 ⟶ 1,814:
'[ dup sum _ > [ _ v-n vabs ] when infimum ] matrix-map ;
 
{ 10 9 2 1 } [ square simple-table. nl ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 799 ⟶ 1,845:
 
=={{header|Fermat}}==
<langsyntaxhighlight lang="fermat">
Func Min(a, b) = if a<=b then a else b fi.;
n:=10;
Line 805 ⟶ 1,851:
[x]:= [<i=1,n> <j=1,n> Min(Min(i-1,n-i),Min(j-1,n-j))];
[x];
</syntaxhighlight>
</lang>
{{out}}<pre>
[[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, `
Line 817 ⟶ 1,863:
0, 1, 1, 1, 1, 1, 1, 1, 1, 0, `
0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]]
</pre>
 
=={{header|FreeBASIC}}==
<lang freebasic>#define min(a, b) Iif(a<=b,a,b)
 
sub minab( n as uinteger )
for i as uinteger = 1 to n
for j as uinteger = 1 to n
print using "## ";min( min(i-1, n-i), min(j-1, n-j) );
next j
print
next i
end sub
 
minab(10)</lang>
{{out}}<pre>
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
</pre>
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 878 ⟶ 1,898:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 937 ⟶ 1,957:
[0]
</pre>
 
=={{header|GW-BASIC}}==
<lang gwbasic>10 N = 10
20 FOR I = 0 TO N - 1
30 IF I < N - 1 - I THEN DI = I ELSE DI = N - 1 - I
40 FOR J = 0 TO N - 1
50 IF J < N - 1 - J THEN DJ = J ELSE DJ = N - 1 - J
60 IF DI < DJ THEN M = DI ELSE M = DJ
70 PRINT USING "## ";M;
80 NEXT J
90 PRINT
100 NEXT I</lang>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List.Split (chunksOf)
 
----------- SHORTEST DISTANCES TO EDGE OF MATRIX ---------
Line 976 ⟶ 1,984:
let w = (succ . maximum) $ fmap (length . show) =<< m
rjust n c = (drop . length) <*> (replicate n c <>)
in unlines (unwords . fmap (rjust w ' ' . show) <$> m)</langsyntaxhighlight>
{{Out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 1,003 ⟶ 2,011:
 
0</pre>
 
or in terms of Data.Matrix:
<syntaxhighlight lang="haskell">import Data.Matrix ( matrix, Matrix )
 
----------- SHORTEST DISTANCES TO EDGE OF MATRIX ---------
 
distancesToEdge :: Int -> Matrix Int
distancesToEdge n = matrix n n
(\(i, j) -> minimum $ ($) <$> [pred, (n -)] <*> [i, j])
 
 
--------------------------- TEST -------------------------
main :: IO ()
main = mapM_ print $ distancesToEdge <$> [10, 9, 2, 1]</syntaxhighlight>
{{Out}}
<pre>┌ ┐
│ 0 0 0 0 0 0 0 0 0 0 │
│ 0 1 1 1 1 1 1 1 1 0 │
│ 0 1 2 2 2 2 2 2 1 0 │
│ 0 1 2 3 3 3 3 2 1 0 │
│ 0 1 2 3 4 4 3 2 1 0 │
│ 0 1 2 3 4 4 3 2 1 0 │
│ 0 1 2 3 3 3 3 2 1 0 │
│ 0 1 2 2 2 2 2 2 1 0 │
│ 0 1 1 1 1 1 1 1 1 0 │
│ 0 0 0 0 0 0 0 0 0 0 │
└ ┘
┌ ┐
│ 0 0 0 0 0 0 0 0 0 │
│ 0 1 1 1 1 1 1 1 0 │
│ 0 1 2 2 2 2 2 1 0 │
│ 0 1 2 3 3 3 2 1 0 │
│ 0 1 2 3 4 3 2 1 0 │
│ 0 1 2 3 3 3 2 1 0 │
│ 0 1 2 2 2 2 2 1 0 │
│ 0 1 1 1 1 1 1 1 0 │
│ 0 0 0 0 0 0 0 0 0 │
└ ┘
┌ ┐
│ 0 0 │
│ 0 0 │
└ ┘
┌ ┐
│ 0 │
└ ┘</pre>
 
or bypassing 'minimum', to reduce the count of comparisons (same output as above):
 
<syntaxhighlight lang="haskell">import Data.Bifunctor (bimap)
import Data.Matrix (Matrix, matrix)
 
----------- SHORTEST DISTANCES TO EDGE OF MATRIX ---------
 
distancesToEdge :: Int -> Matrix Int
distancesToEdge n = matrix n n (uncurry min . bimap f f)
where
m = quot n 2
f i
| i <= m = pred i
| otherwise = n - i
 
--------------------------- TEST -------------------------
main :: IO ()
main = mapM_ print $ distancesToEdge <$> [10, 9, 2, 1]</syntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight lang="j">nByN=: (|."1<.|.)@(<./~@i.)
nByN each 2 3 9 10</langsyntaxhighlight>
{{out}}
<pre>┌───┬─────┬─────────────────┬───────────────────┐
Line 1,020 ⟶ 2,092:
│ │ │ │0 0 0 0 0 0 0 0 0 0│
└───┴─────┴─────────────────┴───────────────────┘</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq'''
 
'''Also works with fq, a Go implementation of a large subset of jq'''
<syntaxhighlight lang=jq>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def printMinCells:
"Minimum number of cells after, before, above and below each cell in a \(.) x \(.) matrix:",
( (. / 2 | ceil | tostring | length) as $p
| range(0; .) as $r
| [ range(0; .) as $c
| [. - $r - 1, $r, $c, . - $c - 1] | min | lpad($p)] | join(" ") );
 
23, 10, 9, 2, 1
| printMinCells, ""
</syntaxhighlight>
{{output}}
As expected.
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">function printNbyN(sizes)
for N in sizes
mat = zeros(Int, N, N)
Line 1,035 ⟶ 2,128:
printNbyN([23, 10, 9, 2, 1])
 
</langsyntaxhighlight>{{out}}
<pre>
Minimum number of cells after, before, above and below 23 x 23 square:
Line 1,099 ⟶ 2,192:
 
=={{header|MiniZinc}}==
<syntaxhighlight lang="minizinc">
<lang MiniZinc>
%Minimum number of cells after, before, above and below NxN squares. Nigel Galloway, August 3rd., 2021
int: Size=10; int: S=Size-1; set of int: N=0..S;
array[N,N] of var int: G = array2d(N,N,[min([n,g,S-n,S-g])|n,g in N]);
output([show2d(G)])
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,122 ⟶ 2,215:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE MinNByN;
FROM InOut IMPORT WriteCard, WriteLn;
 
Line 1,145 ⟶ 2,238:
BEGIN
minNbyN(10, 3);
END MinNByN.</langsyntaxhighlight>
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
Line 1,160 ⟶ 2,253:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import strutils
 
proc printMinCells(n: Positive) =
Line 1,173 ⟶ 2,266:
for n in [10, 9, 2, 1]:
printMinCells(n)
echo()</langsyntaxhighlight>
 
{{out}}
Line 1,207 ⟶ 2,300:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">
n=10
matrix(n,n,i,j,min(min(i-1,n-i),min(j-1,n-j)))
</syntaxhighlight>
</lang>
{{out}}<pre>
[0 0 0 0 0 0 0 0 0 0]
Line 1,235 ⟶ 2,328:
=={{header|Pascal}}==
Using symmetry within row and col.Fill only the middle and let the values before in place.
<langsyntaxhighlight lang="pascal">program mindistance;
{$IFDEF FPC} //used fpc 3.2.1
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON}
Line 1,320 ⟶ 2,413:
Test(1);
end.
</syntaxhighlight>
</lang>
{{out}}
<pre style="width: 800px; height: 480px>
Line 1,377 ⟶ 2,470:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use List::Util qw( max min );
Line 1,389 ⟶ 2,482:
}
print "\n";
}</langsyntaxhighlight>
{{out}}
<pre>0 x 0 distance to nearest edge:
Line 1,445 ⟶ 2,538:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">distance_to_edge</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
Line 1,457 ⟶ 2,550:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({</span><span style="color: #000000;">23</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span><span style="color: #000000;">distance_to_edge</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,512 ⟶ 2,605:
</pre>
Although I rather like it the way it is, you could argue there should be more spacing on the 23x23, if you insist do this before the loops and use fmt on the innermost line:
<!--<langsyntaxhighlight Phixlang="phix">-->
<span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%%%dd"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprint</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)))+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
or maybe just (good for n<=200 whereas the above goes on and on to "%4d", etc.)
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">20</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"%3d"</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PILOT}}==
<syntaxhighlight lang="pilot">C :size=10
:y=0
*line
C :$l=
:x=0
*item
C :sy=(size-y)-1
:sx=(size-x)-1
:i=x
C (y<i):i=y
C (sy<i):i=sy
C (sx<i):i=sx
:$l=$l #i
:x=x+1
J (x<size):*item
T :$l
C :y=y+1
J (y<size):*line
E :</syntaxhighlight>
{{out}}
<pre> 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0</pre>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def min_cells_matrix(siz):
return [[min(row, col, siz - row - 1, siz - col - 1) for col in range(siz)] for row in range(siz)]
 
Line 1,537 ⟶ 2,662:
if __name__ == "__main__":
test_min_mat()
</langsyntaxhighlight>{{out}}
<pre>
Minimum number of cells after, before, above and below 23 x 23 square:
Line 1,597 ⟶ 2,722:
Or, disentangling computation from IO (separating model from display), and composing from generics:
 
<langsyntaxhighlight lang="python">'''Distance to edge of matrix'''
 
from itertools import chain, product
Line 1,662 ⟶ 2,787:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
 
0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0
 
0 0
0 0
 
0</pre>
 
and in terms of a generalized matrix function:
<syntaxhighlight lang="python">'''Minimum distances to edge of matrix'''
 
from itertools import chain
 
 
# distanceFromEdge :: Int -> [[Int]]
def distanceFromEdge(n):
'''A matrix of minimum distances to the
edge of the matrix.
'''
return matrix(n)(n)(
lambda row, col: min([
row - 1, col - 1,
n - row, n - col
])
)
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''Test'''
for n in [10, 9, 2, 1]:
print(
showMatrix(
distanceFromEdge(n)
) + "\n"
)
 
 
# ----------------------- GENERIC ------------------------
 
# matrix :: Int -> Int -> ((Int, Int) -> a) -> [[a]]
def matrix(nRows):
'''A matrix of a given number of columns and rows,
in which each value is a given function over the
tuple of its (one-based) row and column indices.
'''
def go(nCols):
def g(f):
return [
[f(y, x) for x in range(1, 1 + nCols)]
for y in range(1, 1 + nRows)
]
return g
return go
 
 
# showMatrix :: [[Int]] -> String
def showMatrix(xs):
'''String representation of xs
as a matrix.
'''
def go():
rows = [[str(x) for x in row] for row in xs]
w = max(map(len, chain.from_iterable(rows)))
return "\n".join(
" ".join(k.rjust(w, ' ') for k in row)
for row in rows
)
return go() if xs else ''
 
 
# MAIN ---
if __name__ == '__main__':
main()</syntaxhighlight>
{{Out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 1,691 ⟶ 2,910:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub distance-to-edge (\N) {
my $c = ceiling N / 2;
my $f = floor N / 2;
Line 1,706 ⟶ 2,925:
say "\n$_ x $_ distance to nearest edge:";
.fmt("%{$max}d").say for @dte;
}</langsyntaxhighlight>
{{out}}
<pre>0 x 0 distance to nearest edge:
Line 1,763 ⟶ 2,982:
=={{header|REXX}}==
This REXX version automatically adjusts the width of each (cell) number displayed so that all displayed numbers are aligned.
<langsyntaxhighlight lang="rexx">/*REXX pgm finds the minimum# of cells after, before, above, & below a NxN square matrix*/
parse arg $ /*obtain optional arguments from the CL*/
if $='' | $="," then $= 21 10 9 2 1 /*Not specified? Then use the default.*/
Line 1,781 ⟶ 3,000:
 
say; say /*display 2 blank lines between outputs*/
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,848 ⟶ 3,067:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Minimum number of cells after, before, above and below NxN squares:" + nl
Line 1,876 ⟶ 3,095:
 
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,895 ⟶ 3,114:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def dist2edge(n)
width = (n/2).to_s.size+1
m = n-1
Line 1,903 ⟶ 3,122:
end
puts dist2edge(10)</langsyntaxhighlight>
{{out}}
<pre>0 0 0 0 0 0 0 0 0 0
Line 1,916 ⟶ 3,135:
0 0 0 0 0 0 0 0 0 0
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn print_min_cells(n int) {
println("Minimum number of cells after, before, above and below $n x $n square:")
for r in 0..n {
mut cells := []int{len: n}
for c in 0..n {
nums := [n - r - 1, r, c, n - c - 1]
mut min := n
for num in nums {
if num < min {
min = num
}
}
cells[c] = min
}
println(cells)
}
}
fn main() {
for n in [23, 10, 9, 2, 1] {
print_min_cells(n)
println('')
}
}</syntaxhighlight>
 
{{out}}
<pre>
Same as Go entry
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-math}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Nums
import "./fmt" for Fmt
 
var printMinCells = Fn.new { |n|
Line 1,935 ⟶ 3,187:
printMinCells.call(n)
System.print()
}</langsyntaxhighlight>
 
{{out}}
Line 1,993 ⟶ 3,245:
Minimum number of cells after, before, above and below 1 x 1 square:
0
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func Min(A, B);
int A, B;
return if A<B then A else B;
 
def N=10, M=N-1, C=M/2;
int X, Y, VX, VY;
[for Y:= 0 to M do
[for X:= 0 to M do
[VX:= if X <= C then X else M-X;
VY:= if Y <= C then Y else M-Y;
IntOut(0, Min(VX, VY)); ChOut(0, ^ );
];
CrLf(0);
];
]</syntaxhighlight>
 
{{out}}
<pre>
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
</pre>
1,983

edits