Department numbers: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments, changed header information.
m (→‎{{header|REXX}}: added the REXX language.)
m (→‎{{header|REXX}}: added/changed comments, changed header information.)
Line 53:
 
=={{header|REXX}}==
A little extra code was added to allow the specification for the high department number as well as the sum.
<lang rexx>/*REXX program finds/displays all possible variants of (3) department numbering puzzle.*/
parse arg high sum .
if high=='' | high=="," then high= 7
if sum=='' | sum=="," then sum=12
#=0; @pd='police'; @fd="fire"; @sd='sanitation'
Lpd=length(@pd); Lfd=length(@fd); Lsd=length(@sd)
 
Also, extra code was added to nicely format a title (header) for the output, as well as displaying the number of solutions found.
do PD=2 by 2 to high
<lang rexx>/*REXX program finds/displays all possible variants of (3) department numbering puzzle.*/
do FD=1 for high
parse arg high sum . /*obtain optional arguments from the CL*/
if FD==PD then iterate
if high=='' | high=="," then high= 7 /*Not specified? Then use the default.*/
if FD+PD>sum-1 then iterate PD
if sum=='' | sum=="," then sum=12 /* " " " " " " */
$2=PD+FD
#=0; @pd= ' police '; @fd= " fire "; ; @sd= ' sanitation ' /*define names of departments.*/
do SD=1 for high
@dept= ' department '; L=length(@dept) /*literal; and also its length*/
if SD==PD then iterate
#=0 /*initialize the number of solutions. */
if SD==FD then iterate
do PD=2 by 2 to high /*try numbers for the police department*/
$3=$2+SD
do FD=1 for high /* " " " " fire " */
if $3> sum then iterate FD
if $3\FD==sumPD then iterate /*Same FD# & PD#? They must be unique.*/
if FD+PD>sum-1 then iterate PD /*Is sum too large? Try another PD#. */
#=# + 1
if #= do SD=1 for then say @pd high @fd /*try numbers for the sanitation dept. @sd*/
if SD==PD | SD==FD then iterate /*Is SD# ¬unique? They must be unique,*/
if #==1 then say center('',Lpd,"═") center('',Lfd,"═") center('',Lsd,"═")
$=PD+FD+SD say center(PD,Lpd) center(FD,Lfd) center(SD,Lsd)/*compute sum of department numbers. */
if $> sum then iterate FD /*Is the sum too high? Try another FD#*/
end /*SD*/
if $\==sum then iterate /*Is the sum ¬correct? " " SD#*/
end /*FD*/
#=# + 1 /*bump the number of solutions (so far)*/
end /*PD*/
if #==1 then do /*Is this the 1st solution? Show hdr.*/
say
say center(@pd, L) center(@fd, L) center(@sd, L)
if #==0 then #='no'
say copies(center( @dept, L)' ', 3)
say # "solutions found."</lang>
say copies(center('number', L)' ', 3)
if #==1 then say center('',Lpd L, "═") center('',Lfd L, "═") center('',Lsd L, "═")
2 4 6 end
say center(PD, L) center(FD, L) center(SD, L) /*display a solution.*/
do SD=1 end for high/*SD*/
end /*SDFD*/
end /*FDPD*/
say /*display a blank line before the #sols*/
if #==0 then #= 'no' /*use a better word for bupkis. */
say # "solutions found." /*stick a fork in it, we're all done. */</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
police fire sanitation
department department department
══════ ════ ══════════
2 number 3 number 7 number
════════════ ════════════ ════════════
2 4 6
2 6 43 7
2 7 34 6
4 2 1 7 6 4
4 2 7 2 63
4 3 51 7
4 5 32 6
4 6 23 5
4 7 15 3
4 6 1 52
6 4 2 4 7 1
6 4 21 5
6 5 12 4
6 4 2
6 5 1
 
14 solutions found.