Floyd's triangle: Difference between revisions

m
→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations, used a template for the outputs.
m (→‎{{header|ALGOL W}}: Fix bogus template markup)
m (→‎{{header|REXX}}: added/changed comments and whitespace, changed indentations, used a template for the outputs.)
Line 3,447:
This REXX version uses a simple formula to calculate the maximum value (triangle element) displayed.
<lang rexx>/*REXX program constructs & displays Floyd's triangle for any number of specified rows.*/
parse arg rowsN .; if rowsN=='' | N=="," then rowsN=5 /*Not specified? Then use the default.*/
mx=rowsN * (rowsN+1) % 2 - rowsN /*calculate maximum value of any value.*/
say 'displaying a ' rows N " row Floyd's triangle:" /*show the header for theFloyd's triangle.*/
say
#=1; do r=1 for rowsN; 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+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 it, we're all done. */</lang>
'''{{out|output''' |text=&nbsp; when using the default input:}}
<pre>
displaying a 5 row Floyd's triangle:
Line 3,467:
11 12 13 14 15
</pre>
'''{{out|output''' |text=&nbsp; when using the default input of: &nbsp; <tt> 14 </tt>}}
<pre>
displaying a 14 row Floyd's triangle:
Line 3,486:
92 93 94 95 96 97 98 99 100 101 102 103 104 105
</pre>
'''{{out|output'''|text=&nbsp; (only showing the last row) when using the input of: &nbsp; <tt> 45 </tt>}}
<pre>
··· 44 rows not shown ···
Line 3,494:
===version 3, hexadecimal===
<lang rexx>/*REXX program constructs & displays Floyd's triangle for any number of rows in base 16.*/
parse arg rowsN .; if rowsN=='' | N=="," then rowsN=6 /*Not specified? Then use the default.*/
mx=rowsN * (rowsN+1) % 2 - rowsN /*calculate maximum value of any value.*/
say 'displaying a ' rows N " row Floyd's triangle in base 16:"; say /*show triangle hdrheader.*/
say
#=1
#=1; do r=1 for rowsN; 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+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 it, we're all done. */</lang>
'''{{out|output''' |text=&nbsp; when using the default input:}}
<pre>
displaying a 6 row Floyd's triangle in base 16:
Line 3,515:
10 11 12 13 14 15
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; <tt> 23 </tt>}}
<pre>
displaying a 23 row Floyd's triangle in base 16:
Line 3,547:
This REXX version could be extended to even higher bases, all that is needed is to append more viewable characters to express "higher" numerals &nbsp; ("digits" in base '''X''').
<lang rexx>/*REXX program constructs/shows Floyd's triangle for any number of rows in any base ≤90.*/
parse arg rowsN radx . /*obtain optional arguments from the CL*/
if rows N=='' | rows N=="," then rows N= 5 /*Not specified? Then use the default.*/
if radx=='' | radx=="," then radx=10 /* " " " " " " */
mx=rowsN * (rowsN+1) % 2 - rowsN /*calculate maximum value of any value.*/
say 'displaying a ' rows N " row Floyd's triangle in base" radx':'; say /*display hdrthe header.*/
say
#=1
#=1; do r=1 for rowsN; 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+i, radx) ) ) /*build triangle row.*/
Line 3,566:
@@@= '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 3000 /*what the hey, support gihugeics*/
mxB=length(@@@) /*max base (radix) supported here*/
Line 3,577:
if pos(sigX, '-+')\==0 then x=substr(x, 2) /*X number has a leading sign? */
else sigX= /* ··· no leading sign.*/
#=0
#=0; do j=1 for length(x); _=substr(x, j, 1) /*convert X, base inB ──► base 10*/
v=pos(_, @@@) /*get the value of this "digit". */
if v==0 | v>inB then call erd x,j,inB /*is this an illegal "numeral" ? */
Line 3,590 ⟶ 3,591:
y=sigX || substr(@@@, #+1, 1)y /*prepend the sign if it existed.*/
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>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; <tt> 6 &nbsp; 2 </tt>}}
<pre>
displaying a 6 row Floyd's triangle in base 2:
Line 3,606 ⟶ 3,607:
10000 10001 10010 10011 10100 10101
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; <tt> 23 &nbsp; 2 </tt>}}
<pre>
displaying a 12 row Floyd's triangle in base 2:
Line 3,623 ⟶ 3,624:
1000011 1000100 1000101 1000110 1000111 1001000 1001001 1001010 1001011 1001100 1001101 1001110
</pre>
'''{{out|output''' |text=&nbsp; when using the input of: &nbsp; <tt> 40 &nbsp; 81 </tt>}}
<pre>
displaying a 40 row Floyd's triangle in base 81: