Lucky and even lucky numbers: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations.)
Line 453:
 
=={{header|REXX}}==
This REXX version does extra error checking for the arguments.
<lang REXX>/*REXX program displays lucky or evenLucky integers (#'snumbers or a #number range).*/
parse arg bot top func _ . /*get what we need from the C.L. */
ifparse func==''arg bot thentop func='lucky' _ . /*Notobtain defined?required & Useoptional the defaultarguments. */
if func=='' then func='lucky' /*Not specified? Then use the default.*/
s=left('s',bot\==top & top\==",") /*plural results (or not plural).*/
says=left('s', funcbot\==top 'number's& top\==":,") bot top '───►' /*plural results $lucky(bot,or top,maybe func,not _plural).*/
say func 'number's":" bot top '───►' $lucky(bot, top, func, _)
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────$LUCKY subroutine─────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
$lucky: arg x,y,f,?; if y=='' | y==',' then y=x /*get some vars.*/
#=0;$lucky: arg x,y,f,?; if y=='' | $y=;="," then nyy=y<0 x /*setobtain variable:some arguments; NOY: valueset rangeY.*/
#=0; $=; ny=y<0 /*set variable NOY: value range*/
if f=='' then f='LUCKY'; lucky=f=='LUCKY' /*assume LUCKY if omitted*/
if f\=='LUCKY' & then f\=='EVENLUCKYLUCKY'; thenlucky= return(f=="LUCKY") 'function not/*assume valid: 'LUCKY if omitted. f */
if arg()>3 & ? if f\=='LUCKY' & f\=='EVENLUCKY' then return "too'function manynot arguments enteredvalid: "' ?f
if x='' if arg()>3 & ?\='' then return "1sttoo argumentmany isarguments entered: missing." ?
if x<1 ='' then return "1st argument isn't a positive integer:is missing." x
if \datatype(x,'W')<1 then return "1st argument isn't ana positive integer: " x
if \datatype(yx,'W') then return "2nd1st argument isn't an integer: " y x
if x>ay if \datatype(y,'W') then return "2nd argargument isisn't lessan thaninteger: 1st arg." y
if x>ay then return "2nd argument is less than 1st arg."
ay=abs(y); yL=ay; if y>0 then yL=y*10+y+y /*adjust the upper Y limit. */
if f=='' then f='LUCKY'; lucky=f=='LUCKY'
/* [↓] build for LUCKY | EVENLUCKY*/
do j=1 until j>=yL /*construct list of pos. integers.*/
if j//2==(\lucky) then iterate /*EVENLUCKY? Use only even ints.*/
 
if lucky then if (j+1)//6==0 then iterate /*prune if mod 6≡zero6 ≡ zero.*/
else nop /*balance the IF-THEN logic. */
else if j //8==0 then iterate /*prune next if mod 8≡08 ≡ zero.*/
#=#+1 /*bump the counter of #'s foundnumbers. */
$=$ j /*append integer to the $ list.*/
end /*j*/
q=0
z=0
do p=3 until zq==''; z q=word($,p) /*start to prune the integer list.*/
if zq># then leave /*if integer is too large, stop. */
do j=#%zq*zq by -zq to zq /*elide every ZthQth intinteger. */
$=delword($, j, 1) /*delete a particular #number. */
#=#-1 /*decrease the integer count. */
end /*j*/ /*delete from the right endside. */
end /*p*/
@.=
do k=1; parse var $ q $; if q=='' then leave; @.k=q; end /*k*/
@.0=k-1
do jm=1 for # /*restrict the found integers. */
if (\ny & (jm<x | j m>ay)) | (ny & (@.jm<x | @.jm>ay)) then @.jm=
end /*jm*/ /* [↑] a list of #s or a range. */
_=
_=
do b=1 for @.0; _=_ @.b; end /*b*/ /*buildconstruct a list. of integers.*/
return space(_) /*remove superfluous list blanks. */</lang>
'''output''' &nbsp; when the input is: &nbsp; <tt> 1 &nbsp; 20 &nbsp; lucky </tt>
<pre>
lucky numbers: 1 20 ───► 1 3 7 9 13 15 21 25 31 33 37 43 49 51 63 67 69 73 75 79
</pre>
'''output''' &nbsp; when the input is: &nbsp; <tt> 1 &nbsp; 20 &nbsp; evenLucky </tt>
<pre>
evenLucky numbers: 1 20 ───► 2 4 6 10 12 18 20 22 26 34 36 42 44 50 52 54 58 68 70 76
</pre>
'''output''' &nbsp; when the input is: &nbsp; <tt> 6000 &nbsp; -6100 &nbsp; lucky</tt>
<pre>
lucky numbers: 6000 -6100 ───► 6009 6019 6031 6049 6055 6061 6079 6093
</pre>
'''output''' &nbsp; when the input is: &nbsp; <tt> 6000 &nbsp; -6100 evenLucky&nbsp; venLucky</tt>
<pre>
evenLucky numbers: 6000 -6100 ───► 6018 6020 6022 6026 6036 6038 6050 6058 6074 6090 6092
</pre>
'''output''' &nbsp; when the input is: &nbsp; <tt> 10000 </tt>
<pre>
lucky number: 10000 ───► 115591
</pre>
'''output''' &nbsp; when the input is: &nbsp; <tt> 10000 &nbsp; , &nbsp; evenLucky </tt>
<pre>
evenLucky number: 10000 ───► 111842