Generator/Exponential: Difference between revisions
Content added Content deleted
(Updated both D entries) |
(→{{header|REXX}}: added the REXX language. -- ~~~~) |
||
Line 1,313: | Line 1,313: | ||
'''Sample output''' |
'''Sample output''' |
||
<pre>[529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089]</pre> |
<pre>[529, 576, 625, 676, 784, 841, 900, 961, 1024, 1089]</pre> |
||
=={{header|REXX}}== |
|||
Code was added to support listing of so many values, the default is 0 (zero). |
|||
<lang rexx> |
|||
/*REXX program to show how to use a generator (also known as iterators).*/ |
|||
/*────── in general, these generators can work */ |
|||
/*────── on any numbers, not just integers. */ |
|||
/*Generator (POWERS): generate (integer) powers */ |
|||
/*Generator (SQUARES): generate squares */ |
|||
/*Generator (CUBES): generate cubes */ |
|||
/*Generator (SQNCUBE): generate squares that have no cubes. */ |
|||
gen_.= /*have generators from scratch. */ |
|||
/*how_many: show that many vals.*/ |
|||
parse arg how_many; how_many=pick1(how_many 0) |
|||
do j=1 to how_many; call tell 'squares' ,gen_squares(j) ; end |
|||
do j=1 to how_many; call tell 'cubes' ,gen_cubes(j) ; end |
|||
do j=1 to how_many; call tell 'sq¬cubes',gen_sqNcubes(j) ; end |
|||
if how_many>0 then say 'dropping 1st --> 20th values.' |
|||
do j=1 to 20; drop gen_.sqNcubes.j ; end |
|||
do j=20+1 for 10 ; call tell 'sq¬cubes',gen_sqNcubes(j) ; end |
|||
exit |
|||
/*─────────────────────────────────────generate powers iterator─────────*/ |
|||
gen_powers: procedure expose gen_.; parse arg x,p; if x=='' then return |
|||
if gen_.powers.x.p=='' then gen_.powers.x.p=x**p |
|||
return gen_.powers.x.p |
|||
/*─────────────────────────────────────gen squares iterator─────────────*/ |
|||
gen_squares: procedure expose gen_.; parse arg x;if x=='' then return |
|||
if gen_.squares.x=='' then do |
|||
call gen_powers x,2 |
|||
gen_.squares.x=gen_.powers.x.2 |
|||
end |
|||
return gen_.squares.x |
|||
/*─────────────────────────────────────gen cubes iterator───────────────*/ |
|||
gen_cubes: procedure expose gen_.; parse arg x; if x=='' then return |
|||
if gen_.cubes.j=='' then do |
|||
call gen_powers x,3 |
|||
gen_.cubes.x=gen_.powers.x.3 |
|||
end |
|||
return gen_.cubes.x |
|||
/*─────────────────────────────────────gen squares not cubes iterator───*/ |
|||
gen_sqNcubes: procedure expose gen_.; parse arg x; if x=='' then return |
|||
s=1 |
|||
if gen_.sqNcubes.x=='' then do j=1 to x |
|||
if gen_.sqNcubes\=='' then do |
|||
sq=sq+1 |
|||
iterate |
|||
end |
|||
do s=s+1 /*slow way to weed out cubes*/ |
|||
?=gen_squares(s) |
|||
do c=1 until gen_cubes(c)>? |
|||
if gen_cubes(c)==? then iterate s |
|||
end /*c*/ |
|||
leave |
|||
end /*s*/ |
|||
gen_.sqNcubes.x=? |
|||
gen_.sqNcubes.x=gen_.squares.s |
|||
end /*j*/ |
|||
return gen_.sqNcubes.x |
|||
/*─────────────────────────────────────"1─liner" subroutines────────────*/ |
|||
tell: if j==1 then say /*format args to be aligned up. */ |
|||
say right(arg(1),20) right(j,5) right(arg(2),20); return |
|||
pick1: return word(arg(1) arg(2),1) |
|||
</lang> |
|||
Output when using the default (no input): |
|||
<pre style="height:30ex;overflow:scroll"> |
|||
sq¬cubes 21 529 |
|||
sq¬cubes 22 576 |
|||
sq¬cubes 23 625 |
|||
sq¬cubes 24 676 |
|||
sq¬cubes 25 784 |
|||
sq¬cubes 26 841 |
|||
sq¬cubes 27 900 |
|||
sq¬cubes 28 961 |
|||
sq¬cubes 29 1024 |
|||
sq¬cubes 30 1089 |
|||
</pre> |
|||
Output when using <tt> 10 </tt> for input: |
|||
<pre style="height:30ex;overflow:scroll"> |
|||
squares 1 1 |
|||
squares 2 4 |
|||
squares 3 9 |
|||
squares 4 16 |
|||
squares 5 25 |
|||
squares 6 36 |
|||
squares 7 49 |
|||
squares 8 64 |
|||
squares 9 81 |
|||
squares 10 100 |
|||
cubes 1 1 |
|||
cubes 2 8 |
|||
cubes 3 27 |
|||
cubes 4 64 |
|||
cubes 5 125 |
|||
cubes 6 216 |
|||
cubes 7 343 |
|||
cubes 8 512 |
|||
cubes 9 729 |
|||
cubes 10 1000 |
|||
sq¬cubes 1 4 |
|||
sq¬cubes 2 9 |
|||
sq¬cubes 3 16 |
|||
sq¬cubes 4 25 |
|||
sq¬cubes 5 36 |
|||
sq¬cubes 6 49 |
|||
sq¬cubes 7 81 |
|||
sq¬cubes 8 100 |
|||
sq¬cubes 9 121 |
|||
sq¬cubes 10 144 |
|||
dropping 1st --> 20th values. |
|||
sq¬cubes 21 529 |
|||
sq¬cubes 22 576 |
|||
sq¬cubes 23 625 |
|||
sq¬cubes 24 676 |
|||
sq¬cubes 25 784 |
|||
sq¬cubes 26 841 |
|||
sq¬cubes 27 900 |
|||
sq¬cubes 28 961 |
|||
sq¬cubes 29 1024 |
|||
sq¬cubes 30 1089 |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |