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

→‎{{header|ALGOL 68}}: Ensure the cell counts are all displayed in the same width
(→‎{{header|Pascal}}: updated speed by only change the new elements in the middle of the row before.)
(→‎{{header|ALGOL 68}}: Ensure the cell counts are all displayed in the same width)
Line 7:
=={{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.
<lang algol68>BEGIN # show the minimum number of cells above, below, before and after each #
# cell in a suare matrix #
Line 15:
PROC print min cells = ( INT n )VOID:
BEGIN
# deduce how many digits we need to show so the counts are all #
# the same width #
INT w = BEGIN
INT width := 1, v := ( ( n - ( ABS NOT ODD n ) ) OVER 2 );
WHILE v > 9 DO v OVERAB 10; width +:= 1 OD;
width
END;
print( ( "Minimum number of cells after, before, above and below "
, whole( n, 0 )
Line 24 ⟶ 31:
);
FOR r FROM 0 TO n - 1 DO
FOR c FROM 0 TO n - 1 DO print( ( whole( min( n-r-1, min( r, min( c, n-c-1 ) ) ), 0-w ), " " ) ) OD;
print( ( newline ) )
OD
END # print min cells # ;
[]INT tests = ( 10, 9, 2, 1, 21 );
FOR i FROM LWB tests TO UPB tests DO
print min cells( tests[ i ] );
Line 67 ⟶ 74:
Minimum number of cells after, before, above and below 1 x 1 square:
0
 
Minimum number of cells after, before, above and below 21 x 21 square:
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 0
0 1 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 2 1 0
0 1 2 3 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 4 3 2 1 0
0 1 2 3 4 5 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 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 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 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 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 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 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 5 4 3 2 1 0
0 1 2 3 4 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 3 2 1 0
0 1 2 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 1 0
0 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
</pre>
 
3,044

edits