Talk:Hofstadter Figure-Figure sequences: Difference between revisions

m
→‎timings for the REXX solutions: included the latest (re-worked) version of the REXX program.
(→‎timings for the REXX solutions: added/changed whitespace and comments, change a variable's name.)
m (→‎timings for the REXX solutions: included the latest (re-worked) version of the REXX program.)
Line 36:
 
<br>I didn't expect a difference of three orders of magnitude.
<lang rexx>/*REXX program calculates and verifies the Hofstadter Figure─Figure sequences. */
call time 'Reset████████████████████████████████████████████████████████████████████████████████'
parse arg x top bot . /*obtain optional arguments from the CL*/
if x=='' | x=='",'" then x= 10 /*Not specified? Then use the default.*/
if top=='' | top=='",'" then top=1000 /* " " " " " " */
if bot=='' | bot=='",'" then bot= 40 /* " " " " " " */
low=1; if x<0 then low=abs(x) /*only display a single │X│ value? */
r.=0; r.1=1; rr.=r.; rr.1=1; s.=r.; s.1=2 /*initialize the R, RR, and S arrays.*/
errs=0; $.=0
do i=low to abs(x) /*display the 1st X values of R & S.*/
say right('R('i") =", 20) right(ffr(i),7) right('S('i") =",20) right(ffrffs(i), 7),
end right('S('/*i") =", 20) right(ffs(i), 7)*/
end /*i*/
if x<1 then exit
do m=1 for bot; r=ffr(m); $.r=1
end /*m*/ /* [↑] calculate the 1st 40 R values.*/
 
if x<1 then exit do n=1 for top-bot; /*if X isn't positive, then we're s=ffs(n)done.*/
$.=0 if $.s then call ser 'duplicate number in R and S lists:' s; /*initialize the memoization ($) array.s=1*/
do m=1 for end /*n*/ bot; r=ffr(m); $.r=1 /*calculate [↑]the first calculate theforty 1st 960R S values.*/
end /*m*/ /* [↑] ($.) is used for memoization. */
 
do v=1 /* [↓] check for topduplicate #s in R & S*/
do n=1 for top-bot; if \$.v then s=ffs(n) call ser /*calculate the value of 'missing RFFS(n). S:' v*/
if $.s then call endser 'duplicate number /*v*/in R and S lists:' s; /* [↑] are all 1≤ numbers ≤1k present?*/$.s=1
end /*mn*/ /* [↑] calculate the 1st 40 960 RS values.*/
/* [↓] check for missing values in R│S*/
do v=1 for top; if \$.v then call ser 'missing R │ S:' v
end /*v*/ /* [↑] are all 1≤ numbers ≤1k present?*/
say
if errs==0 then say 'verification completed for all numbers from 1 ──►' top " [inclusive]."
else say 'verification failed with' errs "errors."
say
say 'took' format(time('Elapsed█████████████████████████████████████████████████████████████████'),,2) "seconds."
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
ffr: procedure expose r. srr. rrs.; parse arg n /*obtain the number from the argarguments.*/
if r.n\==0 then return r.n /*DefinedIs defined? Then return the value. */
_=ffr(n-1) + ffs(n-1) /*calculate the FFR & and FFS values.*/
r.n=_; rr._=1; return _ /*assign the value to R & RR; return.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
ffs: procedure expose r. s. rr.; parse arg n /*search for ¬not null R or S number. */
if s.n==0 then do k=1 for n /* [↓] 1st IF is a SHORT CIRCUIT. */
if s.k\==0 then if r.k\==0 then iterate /*are both defined?*/
call ffr k /*define R.k via the FFR subroutine*/
km=k-1; _=s.km+1 /*calc. the next S number, possibly.*/
_=_+rr._; s.k=_ /*define an element of the S array. */
end /*k*/
return s.n /*return S.n value to the invoker. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────*/
ser: errs=errs+1; say '***error***!' arg(1); return</lang>
'''output''' &nbsp; when using the default inputs:
<pre>