Hofstadter Figure-Figure sequences: Difference between revisions

Content added Content deleted
(Added Java implementation)
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Line 1,234:
S: [2, 4, 5, 6, 8, 9, 10, 11, 13, 14]
True
</pre>
 
=={{header|REXX}}==
<lang rexx>/*REXX pgm to calculate & verify the Hofstadter Figure-Figure sequences.*/
errs=0 /*number of errors found so far. */
parse arg x highV . /*obtain any C.L. specifications.*/
if x=='' then x=10; if highV=='' then highV=1000 /*use the defaults?*/
r.=0; r.1=1 /*initialize the R array. */
s.=0; s.1=2 /* " " R " */
do i=1 to x /*show 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*/
/*═══════════════════════════════════════verify 1st 1k: unique & present*/
@r= /*initialize the R list to null.*/
@s= /* " " S " " " */
/*build list of 1st 40 R values.*/
do m=1 for 40; r=ffr(m) /*calculate 1st 40 R entries.*/
if has(r,@r) then say '***error!*** duplicate R:' r
@r=@r r /*append R to the @r list. */
end /*m*/
/*build list of 1st 960 S values.*/
do n=1 for 960; s=ffs(n) /*calculate 1st 960 S entries.*/
if has(s,@s) then say '***error!*** duplicate S:' s
@s=@s s /*append S to the @s list. */
end /*n*/
/*verify presence and uniqueness.*/
do v=1 for highV /*verify all 1 ≤ # ≤ 1k present.*/
hasR=has(v,@r)\==0 /*does V exist in the R list? */
hasS=has(v,@S)\==0 /*does V exist in the S list? */
if hasR & hasS then say '***error!*** duplicate R & S:' v
if \(hasR | hasS) then say '***error!*** missing R & S:' v
end /*v*/
say
@v='verification'; @i=" [inclusive]." /*shortcuts to shorten prog width*/
if errs==0 then say @v 'completed for all numbers from 1 ──►' highV @i
else say @v 'failed with' errs "errors."
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────FFR subroutine──────────────────────*/
ffr: procedure expose r. s.; parse arg n
if r.n==0 then r.n=ffr(n-1)+ffs(n-1)
return r.n
/*──────────────────────────────────FFS subroutine──────────────────────*/
ffs: procedure expose r. s.; parse arg n; if s.n\==0 then return s.n
@=r.1; do j=2 while r.j\==0; @=@ r.j; end /*j*/ /*build R list*/
 
do k=1 for n /*search for a not null R number.*/
if s.k\==0 then iterate /*number not null, keep looking. */
km=k-1 /*calculate previous S index num.*/
p=s.km+1 /*previous S number plus 1. */
if has(p,@) then p=p+1 /*bump if number in the R list.*/
s.k=p /*not in R, so we found a number.*/
end /*k*/
 
return s.n
/*──────────────────────────────────HAS subroutine──────────────────────*/
has: return wordpos(arg(1),arg(2))\==0
/*──────────────────────────────────SAYERR subroutine───────────────────*/
sayerr: errs=errs+1; say; say '***error***!'; say; say arg(1); say; return</lang>
'''output''' when using the defaults
<pre style="overflow:scroll">
R(1) = 1 S(1) = 2
R(2) = 3 S(2) = 4
R(3) = 7 S(3) = 5
R(4) = 12 S(4) = 6
R(5) = 18 S(5) = 8
R(6) = 26 S(6) = 9
R(7) = 35 S(7) = 10
R(8) = 45 S(8) = 11
R(9) = 56 S(9) = 13
R(10) = 69 S(10) = 14
 
verification completed for all numbers from 1 ──► 1000 [inclusive].
</pre>