Van Eck sequence: Difference between revisions

→‎{{header|REXX}}: reformat and make it ooRexx compatible
m (→‎{{header|Uiua}}: Reformat)
(→‎{{header|REXX}}: reformat and make it ooRexx compatible)
 
Line 3,361:
=={{header|REXX}}==
===using a list===
This REXX version allows the specification of the   ''start''   and   ''end''   of the   ''Van Eck''   sequence   (to be displayed)   as
<br>well as the initial starting element &nbsp; (the default is zero).
<syntaxhighlight lang="rexx">/*REXX pgm generates/displays the 'start ──►--> end' elements of the Van Eck sequence.*/
parseParse argArg LOlo HIhi $d . /*obtain optional arguments from the CL*/
ifIf LOlo=='' | LOlo=="',"' thenThen LOlo= 1 /*Not specified? Then use the default.*/
ifIf HIhi=='' | HIhi=="',"' thenThen HIhi= 10 /* " " " " " " */
ifIf $d=='' | $d=="',"' thenThen $d= 0 /* " " " " " " */
dd=''
$$=; z= $ /*$$: old seq: $: initial value of seq*/
z=d do HI-1; z= wordpos( reverse(z), reverse($$) ); $$= $; $= $ z /*dd: old seq: d: initial value of seq*/
Do i=1 To hi-1
end /*HI-1*/ /*REVERSE allows backwards search in $.*/
za=wordpos(reverse(z),reverse(dd))
/*stick a fork in it, we're all done. */
zb=0
say 'terms ' LO " through " HI ' of the Van Eck sequence are: ' subword($,LO,HI-LO+1)</syntaxhighlight>
wdd=words(dd)
Do zz=wdd To 1 By -1
If z=word(dd,zz) Then Do
zb=wdd-zz+1
Leave
End
End
z=za
dd=d
d=d z
endEnd /*HI-1*/ /*REVERSE allows backwards search in $d.*/
saySay 'terms ' lo LO "' through " HI' hi ' of the Van Eck sequence are: ' subword($d,LOlo,HIhi-LOlo+1)</syntaxhighlight>
Exit
/*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 3,387 ⟶ 3,401:
 
===using a dictionary===
This REXX version &nbsp; (which uses a dictionary) &nbsp; is about &nbsp; '''20,000''' &nbsp; times faster &nbsp; (when using larger numbers) &nbsp; than
<br>using a list &nbsp; (in finding the previous location of an "old" number (term).
<syntaxhighlight lang="rexx">/*REXX pgm generates/displays the 'start ──►--? end' elements of the Van Eck sequence.*/
parseParse argArg LOlo HIhi sta . /*obtain optional arguments from the CL*/
ifIf LOlo=='' | LOlo=="',"' thenThen LOlo= 1 /*Not specified? Then use the default.*/
ifIf HIhi=='' | HIhi=="',"' thenThen HIhi= 10 /* " " " " " " */
ifIf sta=='' | sta=="',"' thenThen sta= 0 /* " " " " " " */
$d.0= sta; x= sta; @.=. /*$d.: the Van Eck sequence as a list.*/
x=sta
do #=1 for HI-1 /*X: is the last term being examined. */
a.=.
if @.x==. then do; @.x= #; $.#= 0; x= 0; end /*new term.*/
out='0'
else do; z= # - @.x; $.#= z; @.x= #; x= z; end /*old term.*/
Do n=1 For end /*#*/ hi-1 /*ZX: is the newlast term being addedexamined. to list.*/
If a.x==. Then Do LOw= LO - 1; out= $.LOw /*initialize thenew output valueterm. */
a.x=n
do j=LO to HI-1; out= out $.j /*build a list for the output display. */
d.n=0
end /*j*/ /*stick a fork in it, we're all done. */
x=0
say 'terms ' LO " through " HI ' of the Van Eck sequence are: ' out</syntaxhighlight>
End
Else Do /* old term. */
z=n-a.x
d.n=z
a.x=n
x=z
End
out=out d.n
$$=; End /*n*/ z= $ /*$$Z: old seq:the new $:term initialbeing valueadded ofTo seqlist.*/
low=lo-1
out=d.low /* initialize the output list */
Do j=lo To hi-1
out=out d.j do #=1 for HI-1 /*X:build a islist For the last term beingoutput examineddisplay. */
End /*j*/
saySay 'terms ' lo LO "' through " HI ' hi ' of the Van Eck sequence are: ' out</syntaxhighlight>
/*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}} <br><br>
 
2,299

edits