Anonymous user
Floyd's triangle: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, changed indentations, used a template for the outputs.
Thundergnat (talk | contribs) 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
mx=
say 'displaying a '
say
#=1; do r=1 for
do #=# for r; i=i+1 /*start to construct a row of triangle.*/
_=_ right(#, length( mx+i ) )
end /*#*/
say substr(_, 2) /*remove 1st leading blank in the line.*/
end /*r*/ /*stick a fork in it, we're all done. */</lang>
<pre>
displaying a 5 row Floyd's triangle:
Line 3,467:
11 12 13 14 15
</pre>
<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>
<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
mx=
say 'displaying a '
say
do #=# for r; i=i+1
_=_ right( d2x(#), length( d2x(mx+i)
end /*#*/
say substr(_, 2)
end /*r*/
<pre>
displaying a 6 row Floyd's triangle in base 16:
Line 3,515:
10 11 12 13 14 15
</pre>
<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 ("digits" in base '''X''').
<lang rexx>/*REXX program constructs/shows Floyd's triangle for any number of rows in any base ≤90.*/
parse arg
if
if radx=='' | radx=="," then radx=10 /* " " " " " " */
mx=
say 'displaying a '
say
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.*/
/* [↑]
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
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>
<pre>
displaying a 6 row Floyd's triangle in base 2:
Line 3,606 ⟶ 3,607:
10000 10001 10010 10011 10100 10101
</pre>
<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>
<pre>
displaying a 40 row Floyd's triangle in base 81:
|