Pascal's triangle: Difference between revisions
Content added Content deleted
m (added whitespace before the TOC (Table Of Contents), added a Task (bold) header. split the task's requirement sentences into paragraphs, used a bigger font for the formula to make it easier to read the superscript, replace <= with ≤.) |
m (→{{header|REXX}}: added/changed comments and whitespace, optimized the code (for speed), made the code simplier.) |
||
Line 3,145: | Line 3,145: | ||
if nn=='' | nn=="," then nn=10 /*Not specified? Then use the default.*/ |
if nn=='' | nn=="," then nn=10 /*Not specified? Then use the default.*/ |
||
N=abs(nn) /*N is the number of rows in triangle.*/ |
N=abs(nn) /*N is the number of rows in triangle.*/ |
||
w=length( !(N-1) / !(N%2) / !(N-1-N%2) ) /*W: the width of the biggest integer.*/ |
|||
@.=1; $.=@.; unity=right(1, w) /*defaults rows & lines; aligned unity.*/ |
|||
/* [↓] build rows of Pascals' triangle*/ |
/* [↓] build rows of Pascals' triangle*/ |
||
do r=1 for N; rm=r-1 /*Note: the first column is always 1.*/ |
do r=1 for N; rm=r-1 /*Note: the first column is always 1.*/ |
||
Line 3,153: | Line 3,153: | ||
$.r = $.r right(@.r.c, w) /*and construct a line for output (row)*/ |
$.r = $.r right(@.r.c, w) /*and construct a line for output (row)*/ |
||
end /*c*/ /* [↑] C is the column being built.*/ |
end /*c*/ /* [↑] C is the column being built.*/ |
||
if r\==1 then $.r=$.r |
if r\==1 then $.r=$.r unity /*for rows≥2, append a trailing "1".*/ |
||
end /*r*/ /* [↑] R is the row being built.*/ |
end /*r*/ /* [↑] R is the row being built.*/ |
||
/* [↑] WIDTH: for nicely looking line.*/ |
/* [↑] WIDTH: for nicely looking line.*/ |
||
width=length($.N) /*width of the last (output) line (row)*/ |
width=length($.N) /*width of the last (output) line (row)*/ |
||
/*if NN<0, output is written to a file.*/ |
/*if NN<0, output is written to a file.*/ |
||
do r=1 for N |
do r=1 for N; $$=center($.r, width) /*center this particular Pascals' row. */ |
||
if nn>0 then say |
if nn>0 then say $$ /*SAY if NN is positive, else */ |
||
else call lineout |
else call lineout 'PASCALS.'n, $$ /*write this Pascal's row ───► a file.*/ |
||
end /*r*/ |
end /*r*/ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
!: |
!: procedure; !=1; do j=2 to arg(1); !=!*j; end /*j*/; return ! /*compute factorial*/</lang> |
||
'''output''' when using the input of: <tt> 11 </tt> |
'''output''' when using the input of: <tt> 11 </tt> |
||
<pre> |
<pre> |