Pascal's triangle: Difference between revisions

m
→‎{{header|REXX}}: included the new program and updated preamble.
(→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations, user a more idiomatic code for the output.)
m (→‎{{header|REXX}}: included the new program and updated preamble.)
Line 3,113:
 
=={{header|REXX}}==
There is no practical limit for this REXX version, triangles up to 46 rows have been
generated (without wrapping) in a screen window with a width of 620 characters.
 
If the number (of rows) specified is negative, the output is written to a (disk) file
instead.   Triangles with over a 1,000 rows have easily been created.  
The output file created (that is written to disk) is named     '''PASCALS.n'''
    where   '''n'''   is the absolute value of the number entered.
 
<lang rexx></lang>
 
Note: &nbsp; Pascal's triangle is also known as:
:::* &nbsp; Yang Hui's triangle
:::* &nbsp; Khayyam's triangle
:::* &nbsp; Khayyam─Pascal's triangle
:::* &nbsp; Tartaglia's triangle
<lang rexx>/*REXX program displays (or writes to a file) Pascal's triangle (centered/formatted).*/
numeric digits 3000 /*be able to handle gihugeic triangles.*/
parse arg nn . /*obtain the optional argument from CL.*/
if nn=='' | nn=="," then nn=10 /*Not specified? Then use the default.*/
N=abs(nn) /*N is the number of rows in triangle.*/
@.=1; $.=@. /*default value for rows and for lines.*/
w=length(!(N-1) / !(N%2) / !(N-1-N%2)) /*W is the width of the biggest number*/
/* [↓] build rows of Pascals' triangle*/
do r=1 for N; rm=r-1 /*Note: the first column is always 1.*/
do c=2 to rm; cm=c-1 /*build the rest of the columns in row.*/
@.r.c= @.rm.cm + @.rm.c /*assign value to a specific row & col.*/
$.r = $.r right(@.r.c, w) /*and construct a line for output (row)*/
end /*c*/ /* [↑] C is the column being built.*/
if r\==1 then $.r=$.r right(1, w) /*for most rows, append a trailing "1".*/
end /*r*/ /* [↑] R is the row being built.*/
/* [↑] WIDTH: for nicely looking line.*/
width=length($.N) /*width of the last (output) line (row)*/
/*if NN<0, output is written to a file.*/
do r=1 for N /*show│write lines (rows) of triangle. */
if nn>0 then say center($.r, width) /*SAY if NN positive,*/
else call lineout 'PASCALS.'n, center($.r, width) /*write──►file if not.*/
end /*r*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: procedure; parse arg x; !=1; do j=2 to x; !=!*j; end /*j*/; return !</lang>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 11 </tt>
<pre>