Anonymous user
Cut a rectangle: Difference between revisions
m
→optimized: added/changed comments and whitespace.
m (→idiomatic: added/changed whitespace and comments.) |
m (→optimized: added/changed comments and whitespace.) |
||
Line 2,574:
numeric digits 20 /*be able to handle some big integers. */
parse arg N .; if N=='' | N=="," then N=10 /*N not specified? Then use default.*/
dir.= 0; dir.0.1= -1; dir.1.0= -1; dir.2.1= 1; dir.3.0= 1
do y=2 to N; yEven= y//2; say /*calculate rectangles up to size NxN.*/
do x=1 for y; if x//2 then if yEven then iterate
z= solve(y,x,1); _= comma(z); _= right(_, max(14, length(_))) /*align the output.
say right(y, 9) "x" right(x, 2) 'rectangle can be cut' _ "way"s(z).
end /*x*/
Line 2,587:
s: if arg(1)=1 then return arg(3); return word(arg(2) 's', 1) /*pluralizer.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
solve: procedure expose # dir. @. h len next. w; @.=
parse arg h,w,recur /*get values for some args. */
if h//2 then do; t= w; w= h; h= t; if h//2 then return 0
Line 2,598:
next.0= -1; next.1= -wp; next.2= 1; next.3= wp /*direction & distance.*/
if recur then #= 0
do x=cx+1 to w-1; t= x + cy*wp; @.t
_= len - t; @._= 1; call walk cy - 1, x
end /*x*/
#= # + 1
if h==w then #= # + # /*double the count of rectangle cuts. */
else if w//2==0 then if recur then call solve w, h, 0
Line 2,612:
if y==0 then do; #= #+2; return; end /* ◄──┤ " " " */
t= x + y*wp; @.t= @.t + 1; _= len - t /* │ordered by most likely ►──┐*/
@._= @._ +
do j=0 for 4; _= t + next.j /*try each of the four directions.*/
if @._==0 then do; yn= y + dir.j.0; xn= x + dir.j.1
if yn==h then do; #= #+2; iterate; end
if xn==0 then do; #= #+2; iterate; end
Line 2,622:
end
end /*j*/
@.t= @.t - 1; _= len - t; @._= @._ - 1; return</lang>▼
▲ _= len - t; @._= @._ - 1; return</lang>
{{out|output|text= is the same as the idiomatic version (above).}} <br><br>
|