Hofstadter Figure-Figure sequences: Difference between revisions

m
→‎version 1: added/changed whitespace and comments and the link, changed a variable's name.
(Added EchoLisp)
m (→‎version 1: added/changed whitespace and comments and the link, changed a variable's name.)
Line 1,672:
 
Over a third of the program was for verification of the first thousand numbers in the Hofstadter Figure-Figure sequences.
<lang rexx>/*REXX pgmprogram calculates &and verifies the Hofstadter Figure-FigureFigure─Figure sequences.*/
parse arg x hightop bot . /*obtain anyoptional C.L.arguments specifications.from the CL*/
if x=='' then x=10;| if highx==',' then high x=1000 10 /*Not specified? Then use the defaults?default.*/
if bottop=='' | then bottop=40=',' then top=1000 /* " " " /* " " " */
lowif bot==1;'' | if x<0bot==',' then lowbot=abs(x) 40 /*only show" " " " " a single " │X│ value.*/
r.low=01; r.1=1; rr.=r.; if x<0 rr.1then low=1abs(x) /*only display a /*initialize thesingle R│X│ & RRvalue? arrays.*/
sr.=0; sr.1=2 1; rr.=r.; rr.1=1; s.=r.; s.1=2 /*initialize the R " " RR S arrayarrays. */
errs=0; $.=0
do i=low to abs(x) /*showdisplay the 1st first X values of R & S .*/
say right('R('i") =", 20) right(ffr(i), 7), /*show nice*/
right('S('i") =", 20) right(ffs(i), 7) /* R & S */
end /*i*/
if x<1 then exit /*if x ≤ 0, then we're all done.*/
do m=1 for bot; r=ffr(m); $.r=1
end /*m*/ /* [] verifycalculate the 1st 40 all 1≤#≤1kR presentvalues.*/
 
do mn=1 for top-bot; r=ffr(m); $.r=1; end /*calculate 1st 40 R values.*/ s=ffs(n)
if $.s then call ser 'duplicate number in R and S lists:' s; $.s=1
end /*n*/ /* [↑] calculate the 1st 960 S values.*/
 
do v=1 for top
@='duplicate number in R and S lists:' /* [↓] calc. 1st 960 S values.*/
do n=1 for high-bot; s=ffs(n); if \$.sv then call sErrser @ s; $.s=1; end'missing R │ S:' v
end /*v*/ /* [↑] are all 1≤ numbers ≤1k present?*/
 
/* [↓] verify all 1≤#≤1k present*/
do v=1 for high; if \$.v then call sErr 'missing R │ S:' v; end
say
if errs==0 then say 'verification completed for all numbers from 1 ──►' hightop " [inclusive]."
else say 'verification failed with' errs "errors."
exit /*stick a fork in it, we're done.*/
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────FFR subroutine──────────────────────*/
ffr: procedure expose r. s. rr.; parse arg n /*obtain the number parsefrom argthe narg.*/
if r.n\==0 then return r.n /*Defined? Then return the value.*/
_=ffr(n-1) + ffs(n-1) /*calculate the FFR & FFS values.*/
r.n=_; rr._=1; return _ /*assign value to R & RR; return.*/
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────FFS subroutine──────────────────────*/
ffs: procedure expose r. s. rr.; parse arg n /*search for ¬null R│SR or S #number.*/
if s.n==0 then do k=1 for n /* [↓] 1st IF is a shortSHORT circuitCIRCUIT*/
if s.k\==0 then if r.k\==0 then iterate
call ffr k /*define R.k via FFR subroutine*/
km=k-1; _=s.km+1 /*the next S number, possibly. */
_=_+rr._; s.k=_ /*define an element of S array.*/
end end /*k*/
return s.n /*return theS.n value to the invoker*/
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────SERR subroutine─────────────────────*/
sErrser: errs=errs+1; say '***error***!' arg(1); return</lang>
To see the talk section about this REXX program's timings, click here: &nbsp; &nbsp; [http://rosettacode.org/wiki/Talk:Hofstadter_Figure-Figure_sequences#timings_for_the_REXX_solutions timings for the REXX solutions]. <br>
 
'''output''' &nbsp; when using the defaultsdefault inputs:
<pre>
R(1) = 1 S(1) = 2