First class environments: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed indentation and whitespace, changed and added comments, changed pgm to handle larger intermediate Collatz numbers. -- ~~~~)
Line 936: Line 936:


=={{header|REXX}}==
=={{header|REXX}}==
The formatting is sensative to a terminating Collatz sequence and is shown as blanks.
<br>Column widths are automatically adjusted for their width (maximum number displayed in a column).
<lang rexx>/*REXX program illustrates first-class environments (using hailstone #s)*/
<lang rexx>/*REXX program illustrates first-class environments (using hailstone #s)*/
parse arg #envs .; env_.=; if #envs=='' then #envs=12
parse arg N .; if N=='' then N=12 /*Was N defined? No, use default.*/
w=length(N) /*the width (so far) for columns.*/
@.=
do init=1 for N; @.init=init; end /*initialize all the environments*/


do forever until @.0; @.0=1 /* ◄─── process all environments.*/
/*═════════════════════════════════════initialize (twelve) environments.*/
do init=1 for #envs; env_.init=init; end
do k=1 for N; x=hailstone(k); w=max(w,length(x))
@.k=@.k x /*where the rubber meets the road*/
end /*k*/
end /*forever*/


count=0 /* [↓] show tabular results. */
/*═════════════════════════════════════process environments until done. */
do forever until env_.0; env_.0=1
do lines=-1 until _==''; _= /*process each line for each env.*/
do k=1 for #envs
do j=1 for N /*process each environment. */
env_.k=env_.k hailstone(k) /*where the rubber meets the road*/
select /*choose how to process the line.*/
when count== 1 then _=_ right(words(@.j)-1, w)
end /*k*/
end /*forever*/
when lines==-1 then _=_ right(j, w) /*hdr.*/
when lines== 0 then _=_ right('', w, '─') /*sep.*/
otherwise _=_ right(word(@.j, lines), w)
end /*select*/
end /*j*/


if count==1 then count=2
/*═════════════════════════════════════show results in tabular form. */
_=strip(_,'T'); if _=='' then count=count+1 /*if null, bump*/
count=0; do lines=-1 until _==''; _=
do j=1 for #envs
if count==1 then _=copies(" "left('', w, "═"), N) /*foot sep*/
select
if _\=='' then say substr(_,2) /*show the (foot) counts.*/
when count== 1 then _=_ right(words(env_.j)-1,3)
end /*lines*/
when lines==-1 then _=_ right(j,3)
when lines== 0 then _=_ right('',3,'─')
otherwise _=_ right(word(env_.j,lines),3)
end /*select*/
end /*j*/

if count==1 then count=2
_=strip(_,'T')
if _=='' then count=count+1
if count==1 then _=copies(' ═══',#envs)
if _\=='' then say substr(_,2)
end /*lines*/
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're done.*/

/*─────────────────────────────────────HAILSTONE (Collatz) subroutine───*/
/*─────────────────────────────────────HAILSTONE (Collatz) subroutine───*/
hailstone: procedure expose env_.; arg n; _=word(env_.n,words(env_.n))
hailstone: procedure expose @.; parse arg y; _=word(@.y, words(@.y))
if _==1 then return ''; env_.0=0; if _//2==0 then return _%2; return _*3+1</lang>
if _==1 then return '' ; @.0=0; if _//2==0 then return _%2; return _*3+1</lang>
'''output''' using the default input:
{{out}}
<pre>
<pre>
1 2 3 4 5 6 7 8 9 10 11 12
1 2 3 4 5 6 7 8 9 10 11 12
Line 997: Line 996:
═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══
═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══ ═══
0 1 7 2 5 8 16 3 19 6 14 9
0 1 7 2 5 8 16 3 19 6 14 9
</pre>
'''output''' when using the input of: &nbsp; <tt> 60 </tt>
<pre style="height:90ex;overflow:scroll">
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ──── ────
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
1 10 2 16 3 22 4 28 5 34 6 40 7 46 8 52 9 58 10 64 11 70 12 76 13 82 14 88 15 94 16 100 17 106 18 112 19 118 20 124 21 130 22 136 23 142 24 148 25 154 26 160 27 166 28 172 29 178 30
5 1 8 10 11 2 14 16 17 3 20 22 23 4 26 28 29 5 32 34 35 6 38 40 41 7 44 46 47 8 50 52 53 9 56 58 59 10 62 64 65 11 68 70 71 12 74 76 77 13 80 82 83 14 86 88 89 15
16 4 5 34 1 7 8 52 10 10 11 70 2 13 14 88 16 16 17 106 3 19 20 124 22 22 23 142 4 25 26 160 28 28 29 178 5 31 32 196 34 34 35 214 6 37 38 232 40 40 41 250 7 43 44 268 46
8 2 16 17 22 4 26 5 5 34 35 1 40 7 44 8 8 52 53 10 58 10 62 11 11 70 71 2 76 13 80 14 14 88 89 16 94 16 98 17 17 106 107 3 112 19 116 20 20 124 125 22 130 22 134 23
4 1 8 52 11 2 13 16 16 17 106 20 22 22 4 4 26 160 5 29 5 31 34 34 35 214 1 38 40 40 7 7 44 268 8 47 8 49 52 52 53 322 10 56 58 58 10 10 62 376 11 65 11 67 70
2 4 26 34 1 40 8 8 52 53 10 11 11 2 2 13 80 16 88 16 94 17 17 106 107 19 20 20 22 22 22 134 4 142 4 148 26 26 160 161 5 28 29 29 5 5 31 188 34 196 34 202 35
1 2 13 17 20 4 4 26 160 5 34 34 1 1 40 40 8 44 8 47 52 52 53 322 58 10 10 11 11 11 67 2 71 2 74 13 13 80 484 16 14 88 88 16 16 94 94 17 98 17 101 106
1 40 52 10 2 2 13 80 16 17 17 20 20 4 22 4 142 26 26 160 161 29 5 5 34 34 34 202 1 214 1 37 40 40 40 242 8 7 44 44 8 8 47 47 52 49 52 304 53
20 26 5 1 1 40 40 8 52 52 10 10 2 11 2 71 13 13 80 484 88 16 16 17 17 17 101 107 112 20 20 20 121 4 22 22 22 4 4 142 142 26 148 26 152 160
10 13 16 20 20 4 26 26 5 5 1 34 1 214 40 40 40 242 44 8 8 52 52 52 304 322 56 10 10 10 364 2 11 11 11 2 2 71 71 13 74 13 76 80
5 40 8 10 10 2 13 13 16 16 17 107 20 20 20 121 22 4 4 26 26 26 152 161 28 5 5 5 182 1 34 34 34 1 1 214 214 40 37 40 38 40
16 20 4 5 5 1 40 40 8 8 52 322 10 10 10 364 11 2 2 13 13 13 76 484 14 16 16 16 91 17 17 17 107 107 20 112 20 19 20
8 10 2 16 16 20 20 4 4 26 161 5 5 5 182 34 1 1 40 40 40 38 242 7 8 8 8 274 52 52 52 322 322 10 56 10 58 10
4 5 1 8 8 10 10 2 2 13 484 16 16 16 91 17 20 20 20 19 121 22 4 4 4 137 26 26 26 161 161 5 28 5 29 5
2 16 4 4 5 5 1 1 40 242 8 8 8 274 52 10 10 10 58 364 11 2 2 2 412 13 13 13 484 484 16 14 16 88 16
1 8 2 2 16 16 20 121 4 4 4 137 26 5 5 5 29 182 34 1 1 1 206 40 40 40 242 242 8 7 8 44 8
4 1 1 8 8 10 364 2 2 2 412 13 16 16 16 88 91 17 103 20 20 20 121 121 4 22 4 22 4
2 4 4 5 182 1 1 1 206 40 8 8 8 44 274 52 310 10 10 10 364 364 2 11 2 11 2
1 2 2 16 91 103 20 4 4 4 22 137 26 155 5 5 5 182 182 1 34 1 34 1
1 1 8 274 310 10 2 2 2 11 412 13 466 16 16 16 91 91 17 17
4 137 155 5 1 1 1 34 206 40 233 8 8 8 274 274 52 52
2 412 466 16 17 103 20 700 4 4 4 137 137 26 26
1 206 233 8 52 310 10 350 2 2 2 412 412 13 13
103 700 4 26 155 5 175 1 1 1 206 206 40 40
310 350 2 13 466 16 526 103 103 20 20
155 175 1 40 233 8 263 310 310 10 10
466 526 20 700 4 790 155 155 5 5
233 263 10 350 2 395 466 466 16 16
700 790 5 175 1 1186 233 233 8 8
350 395 16 526 593 700 700 4 4
175 1186 8 263 1780 350 350 2 2
526 593 4 790 890 175 175 1 1
263 1780 2 395 445 526 526
790 890 1 1186 1336 263 263
395 445 593 668 790 790
1186 1336 1780 334 395 395
593 668 890 167 1186 1186
1780 334 445 502 593 593
890 167 1336 251 1780 1780
445 502 668 754 890 890
1336 251 334 377 445 445
668 754 167 1132 1336 1336
334 377 502 566 668 668
167 1132 251 283 334 334
502 566 754 850 167 167
251 283 377 425 502 502
754 850 1132 1276 251 251
377 425 566 638 754 754
1132 1276 283 319 377 377
566 638 850 958 1132 1132
283 319 425 479 566 566
850 958 1276 1438 283 283
425 479 638 719 850 850
1276 1438 319 2158 425 425
638 719 958 1079 1276 1276
319 2158 479 3238 638 638
958 1079 1438 1619 319 319
479 3238 719 4858 958 958
1438 1619 2158 2429 479 479
719 4858 1079 7288 1438 1438
2158 2429 3238 3644 719 719
1079 7288 1619 1822 2158 2158
3238 3644 4858 911 1079 1079
1619 1822 2429 2734 3238 3238
4858 911 7288 1367 1619 1619
2429 2734 3644 4102 4858 4858
7288 1367 1822 2051 2429 2429
3644 4102 911 6154 7288 7288
1822 2051 2734 3077 3644 3644
911 6154 1367 9232 1822 1822
2734 3077 4102 4616 911 911
1367 9232 2051 2308 2734 2734
4102 4616 6154 1154 1367 1367
2051 2308 3077 577 4102 4102
6154 1154 9232 1732 2051 2051
3077 577 4616 866 6154 6154
9232 1732 2308 433 3077 3077
4616 866 1154 1300 9232 9232
2308 433 577 650 4616 4616
1154 1300 1732 325 2308 2308
577 650 866 976 1154 1154
1732 325 433 488 577 577
866 976 1300 244 1732 1732
433 488 650 122 866 866
1300 244 325 61 433 433
650 122 976 184 1300 1300
325 61 488 92 650 650
976 184 244 46 325 325
488 92 122 23 976 976
244 46 61 70 488 488
122 23 184 35 244 244
61 70 92 106 122 122
184 35 46 53 61 61
92 106 23 160 184 184
46 53 70 80 92 92
23 160 35 40 46 46
70 80 106 20 23 23
35 40 53 10 70 70
106 20 160 5 35 35
53 10 80 16 106 106
160 5 40 8 53 53
80 16 20 4 160 160
40 8 10 2 80 80
20 4 5 1 40 40
10 2 16 20 20
5 1 8 10 10
16 4 5 5
8 2 16 16
4 1 8 8
2 4 4
1 2 2
1 1
════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════ ════
0 1 7 2 5 8 16 3 19 6 14 9 9 17 17 4 12 20 20 7 7 15 15 10 23 10 111 18 18 18 106 5 26 13 13 21 21 21 34 8 109 8 29 16 16 16 104 11 24 24 24 11 11 112 112 19 32 19 32 19

</pre>
</pre>