Hofstadter Figure-Figure sequences: Difference between revisions
Content added Content deleted
(Added BBC BASIC) |
(→{{header|REXX}}: re-wrote the subroutines and verification routine. -- ~~~~) |
||
Line 1,301: | Line 1,301: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
This REXX example makes use of spare arrays. |
|||
<lang rexx>/*REXX pgm to calculate & verify the Hofstadter Figure-Figure sequences.*/ |
<lang rexx>/*REXX pgm to calculate & verify the Hofstadter Figure-Figure sequences.*/ |
||
parse arg x highV . /*obtain any C.L. specifications.*/ |
parse arg x highV . /*obtain any C.L. specifications.*/ |
||
Line 1,307: | Line 1,308: | ||
if x<0 then low=abs(x) /*only show a single │X│ value.*/ |
if x<0 then low=abs(x) /*only show a single │X│ value.*/ |
||
r.=0; r.1=1; rr.=r.; rr.1=1 /*initialize the R and RR arrays.*/ |
r.=0; r.1=1; rr.=r.; rr.1=1 /*initialize the R and RR arrays.*/ |
||
s.=0; s.1=2; ss.=s.; ss.2=1 /* " |
s.=0; s.1=2; ss.=s.; ss.2=1 /* " ? S " SS " .*/ |
||
errs=0 |
errs=0 |
||
do i=low to abs(x) /*show first X values of R & S */ |
do i=low to abs(x) /*show first X values of R & S */ |
||
Line 1,317: | Line 1,318: | ||
both.=0 /*initialize the BOTH array. */ |
both.=0 /*initialize the BOTH array. */ |
||
/*build list of 1st 40 R values.*/ |
/*build list of 1st 40 R values.*/ |
||
do m=1 for 40; r=ffr(m) /*calculate 1st 40 R |
do m=1 for 40; r=ffr(m) /*calculate 1st 40 R values.*/ |
||
both.r=1 /*build the BOTH array. */ |
both.r=1 /*build the BOTH array. */ |
||
end /*m*/ |
end /*m*/ |
||
/*build list of 1st 960 S values.*/ |
|||
do n=1 for 960; s=ffs(n) |
do n=1 for 960; s=ffs(n) /*calculate 1st 960 S values.*/ |
||
if both.s then call sayErr 'duplicate number in R and S lists:' s |
if both.s then call sayErr 'duplicate number in R and S lists:' s |
||
both.s=1 |
both.s=1 /*add to the BOTH array. */ |
||
end /*n*/ |
end /*n*/ |
||
/*verify presence and uniqueness.*/ |
/*verify presence and uniqueness.*/ |
||
do v=1 for highV |
do v=1 for highV /*verify all 1 ≤ # ≤ 1k present.*/ |
||
if \both.v then call sayErr 'missing R |
if \both.v then call sayErr 'missing R │ S:' v |
||
end /*v*/ |
end /*v*/ |
||
say |
say |
||
@v='verification'; @i=" [inclusive]." /*shortcuts to shorten prog width*/ |
@v='verification'; @i=" [inclusive]." /*shortcuts to shorten prog width*/ |
||
Line 1,337: | Line 1,338: | ||
ffr: procedure expose r. s. rr. ss.; parse arg n |
ffr: procedure expose r. s. rr. ss.; parse arg n |
||
if r.n\==0 then return r.n /*Defined? Then return the value.*/ |
if r.n\==0 then return r.n /*Defined? Then return the value.*/ |
||
_ |
_=ffr(n-1)+ffs(n-1) /*calculate the FFR value. */ |
||
r.n |
r.n=_; rr._=1 /*assign the value to R and RR.*/ |
||
return _ /*return the value to the invoker*/ |
return _ /*return the value to the invoker*/ |
||
/*──────────────────────────────────FFS subroutine──────────────────────*/ |
/*──────────────────────────────────FFS subroutine──────────────────────*/ |
||
ffs: |
ffs: procedure expose r. s. rr. ss.; parse arg n |
||
do k=1 for n while s.n==0 /*search for not null R │ S num.*/ |
|||
if s.k\==0 & ffr(k)\==0 then iterate |
|||
km=k-1; _=s.km+1 /*the next SS number, possibly.*/ |
|||
_=_+rr._ /*maybe adjust for the FRR num.*/ |
|||
s.k=_; ss._=1 /*define couple of FFS numbers.*/ |
|||
end /*k*/ |
|||
return s.n /*return the value to the invoker*/ |
|||
return s.n /*return the value to the invoker*/ |
|||
/*──────────────────────────────────SAYERR subroutine───────────────────*/ |
/*──────────────────────────────────SAYERR subroutine───────────────────*/ |
||
sayErr: errs=errs+1; say; say '***error***!'; say; say arg(1); say; return</lang> |
sayErr: errs=errs+1; say; say '***error***!'; say; say arg(1); say; return</lang> |