Floyd's triangle: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace.
m (→‎{{header|REXX}}: added/changed comments and whitespace.)
Line 2,646:
<lang rexx>/*REXX program constructs & displays Floyd's triangle for any number of specified rows.*/
parse arg rows .; if rows=='' then rows=5 /*Not specified? Then use the default.*/
mx=rows * (rows+1) % 2 - rows /*calculate maximum value of any value.*/
say 'displaying a ' rows " row Floyd's triangle:" /*show header for the triangle.*/
say
#=1; do r=1 for rows; i=0; _= /*construct Floyd's triangle row by row*/
do #=# for r; i=i+1 /*start to construct a row of triangle.*/
_=_ right(#, length( mx-rows+i) ) /*build a row of the Floyd's triangle. */
end /*#*/
say substr(_, 2) /*remove 1st leading blank in the line,.*/
end /*r*/ /*stick [↑]a fork in introducedit, by firstwe're abutmentall done. */</lang>
/*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the default input:
<pre>
Line 2,694 ⟶ 2,693:
<lang rexx>/*REXX program constructs & displays Floyd's triangle for any number of rows in base 16.*/
parse arg rows .; if rows=='' then rows=6 /*Not specified? Then use the default.*/
mx=rows * (rows+1) % 2 - rows /*calculate maximum value of any value.*/
say 'displaying a ' rows " row Floyd's triangle in base 16:"; say /*show triangle hdr*/
#=1
do r=1 for rows; i=0; _= /*construct Floyd's triangle row by row*/
do #=# for r; i=i+1 /*start to construct a row of triangle.*/
_=_ right( d2x(#), length( d2x(mx-rows+i) ) ) /*build a row of the Floyd's triangle. */
end /*#*/
say substr(_, 2) /*remove 1st leading blank in the line,.*/
end /*r*/ /*stick [↑]a fork in introducedit, by firstwe're abutmentall done. */</lang>
/*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the default input:
<pre>
Line 2,750 ⟶ 2,748:
if rows=='' | rows=="," then rows= 5 /*Not specified? Then use the default.*/
if radx=='' | radx=="," then radx=10 /* " " " " " " */
mx=rows * (rows+1) % 2 - rows /*calculate maximum value of any value.*/
say 'displaying a ' rows " row Floyd's triangle in base" radx':'; say /*display hdr*/
#=1
do r=1 for rows; i=0; _= /*construct Floyd's triangle row by row*/
do #=# for r; i=i+1 /*start to construct a row of triangle.*/
_=_ right(base(#, radx), length( base(mx-rows+i, radx) ) ) /*build triangle row.*/
end /*#*/
say substr(_, 2) /*remove 1st leading blank in the line,*/
end /*r*/ /* [↑] introduced by first abutment. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
base: procedure; parse arg x 1 ox,toB,inB /*obtain number, toBase, inBase. */
@abc= 'abcdefghijklmnopqrstuvwxyz' /*lowercase Latin alphabet. */
@abcU=@abc; upper @abcU /*go whole hog and extend 'em. */
@@@= '0123456789'@abc || @abcU /*prefix 'em with numeric digits.*/
@@@=@@@'<>[]{}()?~!@#$%^&*_=|\/;:¢¬≈' /*add some special chars as well.*/
/*handles up to base 90, all chars must be viewable.*/
numeric digits 10003000 /*what the hey, support gihugeics*/
mxB=length(@@@) /*max base (radix) supported here*/
if toB=='' | toB=="," then toB=10 /*if skipped, assume default (10)*/
Line 2,774 ⟶ 2,772:
if toB<2 | tob>mxB then call erb 'toBase',toB /* " " " toBase. */
if x=='' then call erm /* " " " number. */
sigX=left(x, 1) sigX=left(x,1) /*obtain a possible leading sign.*/
if pos(sigX, '-+')\==0 then x=substr(x, 2) /*X number has a leading sign? */
else sigX= /* ··· no leading sign.*/
#=0; do j=1 for length(x); _=substr(x, j, 1) /*convert X, base inB ──► base 10*/
_=substr(x, j, 1) /*pick off a numeral from X. */
v=pos(_, @@@) /*get the value of this "digit". */
if v==0 | v>inB then call erd x,j,inB /*is this an illegal "numeral" ? */
#=# * inB + v -1 1 /*construct new num, dig by dig. */
end /*j*/
y=
do while # >= toB /*convert #, base 10 ──► base toB*/
y=substr(@@@, (# // toB) + 1, 1)y /*construct the number for output*/
#=# %toB toB /* ··· and whittle # down also.*/
end /*while*/
 
Line 2,792 ⟶ 2,789:
return y /*return the number in base toB.*/
/*───────────────────────────────────────────────────────────────────────────────────────*/
erb: call ser 'illegal' arg(2) "base: " arg(1) "must be in range: 2──► " mxB
erd: call ser 'illegal "digit" in' x":" _
erm: call ser 'no argument specified.'
ser: say; say '*** error! ***'; say arg(1); say; exit 13</lang>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 6 &nbsp; 2 </tt>
<pre>