Paraffins: Difference between revisions

m
→‎{{header|REXX}}: simplified code, changed comments, split some compound statements, added whitespace.
(Added alternative solution for Pascal.)
m (→‎{{header|REXX}}: simplified code, changed comments, split some compound statements, added whitespace.)
Line 2,576:
 
Programming note:   the biggest concern was calculating the number of numeric digits   (so as to avoid integer overflow).
<lang rexx>/*REXX programpgm enumerates (without repetition) the #number of paraffins with N carbon atoms. of carbon*/
parse arg nodes . /*obtain optional argument from the CL.*/
if nodes=='' | nodes=="," then nodes= 100 /*Not specified? Then use the default.*/
Line 2,585:
say right(0, w) unrooted.0 /*show enumerations of 0 carbon atoms*/
/* [↓] process all nodes (up to NODES)*/
do C=1 for nodes; h= C % 2 /*C: is the number of carbon atoms. */
call tree 0, C, C, 1, 1 /* [↓] if # of carbon atoms is even···*/
if \(C//2==0) then unrooted.C= unrooted.C + rooted.h * (rooted.h + 1) % 2
say right(C, w) unrooted.C /*display an aligned formatted number. */
end /*C*/
Line 2,593:
/*──────────────────────────────────────────────────────────────────────────────────────*/
tree: procedure expose rooted. unrooted. nodes #. /*this function is recursive.*/
parse arg br,n,L,sum,cnt; nm= n - 1; LL nm= Ln +- L1; brpLL= brL + 1L
brp= br + 1
do b=brp to 4; sum= sum + n; if sum>nodes then leave
if b==4 then do b=brp to 4; sum= if LL>=sum + then leaven
if b==brp then #.br= rooted.n * cntif sum>nodes then leave
if b==4 else #.br= #.br * (rooted.nthen +if bLL>=sum - brp) % (b -then br)leave
if LL<sumb==brp then unrooted#.sumbr= unrootedrooted.sumn +* #.brcnt
if b else #.br==4 #.br * (rooted.n + b - brp) % (b then- leavebr)
rooted. if LL<sum then unrooted.sum= rootedunrooted.sum + #.br
do m=nm by -1 for nm; call treeif b, m, L,==4 sum, #.br; end then /*m*/leave
end /*b*/ rooted.sum = rooted.sum + /* ↑↑↑↑↑↑↑↑↑ recursive invocation of TREE#. */br
do m=nm by -1 for nm; call tree b, m, L, sum, #.br
end /*m*/
end /*b*/ /* ↑↑↑↑↑↑↑↑↑ recursive. */
return</lang>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 600 </tt>}}