Hofstadter Q sequence: Difference between revisions

m
→‎{{header|REXX}}: implemented better output formats.
(→‎{{header|REXX}}: simplified the code, added/changed whitespace and comments, added commas to larger numbers.)
m (→‎{{header|REXX}}: implemented better output formats.)
Line 2,322:
if d=='' | d=="," then d= -1000000 /* " " " " " " */
@.= 1; ac= abs(c) /* [↑] negative #'s don't show values.*/
call HofstadterQ a; say
call HofstadterQ b; say; 'HofstadterQ ' say commas(abs(b))th(b) " 'valueterm is: ' " commas(result); say
call HofstadterQ c; say
downs= 0; do j=2 for ac-1; jm= j - 1
downs= downs + (@.j<@.jm)
end /*j*/
 
say commas(downs) ' HofstatdterQ terms are less then the previous term,' ,
' HofstatdterQ('commas(ac) || th(ac)") "term is: " commas(@.ac)
call HofstadterQ d; ad= abs(d); say
say 'The ' commas(ad) || th(ad) ' HofstatdterQ term is: ' commas(@.ad)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
HofstadterQ: procedure expose @.; parse arg x 1 ox /*get number to generate through.*/
/* [↑] OX is the same as X. */
x= abs(x); w= length( commas(x) ) /*use the absolute value; forget Xlength. */
w= length(x) do j=1 for x /*use for[↓] use rightshort─circuit justifiedIF output.test*/
doif j=1>2 for xthen if @.j==1 then do; jm1= j - 1; jm2= j - /* [↓] use short─circuit IF test*/2
if j>2 then if @.j==1 then do; jm1= j - 1; jm2 one= j - 2@.jm1; two= j - @.jm2
one@.j= j - @.jm1;one + two= j - @.jm2two
@.j= @.one + @.twoend
if ox>0 then say 'HofstadterQ('right(j, w)"): " endright(@.j,max(w,length(@.j)))
end if ox>0 then say right(j,w) right(@.j,w) /*display the number if OX > 0. j*/
end /*j*/
return @.x /*return the │X│th term to caller*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 2,352 ⟶ 2,351:
{{out|output|text=&nbsp; when using the internal default inputs:}}
<pre>
HofstadterQ( 1): 1
1 1
HofstadterQ( 2): 1
2 1
HofstadterQ( 3): 2
3 2
HofstadterQ( 4): 3
4 3
HofstadterQ( 5): 3
5 3
HofstadterQ( 6): 4
6 4
HofstadterQ( 7): 5
7 5
HofstadterQ( 8): 5
8 5
HofstadterQ( 9): 6
9 6
HofstadterQ(10): 6
 
HofstadterQ 1,000th value term is: 502
 
49,798 HofstatdterQ terms are less then the previous term, HofstatdterQ(100,000th) term is: 48,157
 
The 1,000,000th HofstatdterQ term is: 512,066
</pre>
 
Line 2,380 ⟶ 2,379:
if d=='' | d=="," then d= -1000000 /* " " " " " " */
@.= 1; ac= abs(c) /* [↑] negative #'s don't show values.*/
call HofstadterQ a; say
call HofstadterQ b; say; 'HofstadterQ ' say commas(abs(b))th(b) " 'valueterm is: ' " commas(result); say
call HofstadterQ c; say
downs= 0; do j=2 for ac-1; jm= j - 1
downs= downs + (@.j<@.jm)
end /*j*/
 
say commas(downs) ' HofstatdterQ terms are less then the previous term,' ,
' HofstatdterQ('commas(ac) || th(ac)") "term is: " commas(@.ac)
call HofstadterQ d; ad= abs(d); say
say 'The ' commas(ad) || th(ad) ' HofstatdterQ term is: ' commas(@.ad)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
HofstadterQ: procedure expose @.; parse arg x 1 ox /*get number to generate through.*/
/* [↑] OX is the same as X. */
x= abs(x); w= length( commas(x) ) /*use the absolute value; forget Xlength. */
do j=1 end for /*j*/x
w= length(x) /*use for right justified output.*/
if j>2 do then if @.j==1 forthen x @.j= @(j - @(j-1)) + @(j - @(j-2))
if jox>2 0 then say if @.'HofstadterQ('right(j==1, w)"): then" right(@.j= @,max(w,length(j - @(.j-1)) + @(j - @(j-2))
if ox>0 then say right(j, w) right(@.j, w) end /*display if X > 0 j*/
end /*j*/
return @.x /*return the │X│th term to caller*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
Line 2,418 ⟶ 2,416:
if d=='' | d=="," then d= -1000000 /* " " " " " " */
@.= 0; @.1= 1; @.2= 1; ac= abs(c) /* [↑] negative #'s don't show values.*/
call HofstadterQ a; say
call HofstadterQ b; say; 'HofstadterQ ' say commas(abs(b))th(b) " 'valueterm is: ' " commas(result); say
call HofstadterQ c; say
downs= 0; do j=2 for ac-1; jm= j - 1
downs= downs + (@.j<@.jm)
end /*j*/
 
say commas(downs) ' HofstatdterQ terms are less then the previous term,' ,
' HofstatdterQ('commas(ac) || th(ac)") "term is: " commas(@.ac)
call HofstadterQ d; ad= abs(d); say
say 'The ' commas(ad) || th(ad) ' HofstatdterQ term is: ' commas(@.ad)
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
HofstadterQ: procedure expose @.; parse arg x 1 ox /*get number to generate through.*/
/* [↑] OX is the same as X. */
x= abs(x); w= length( commas(x) ) /*use the absolute value; forget Xlength. */
do j=1 end for /*j*/x
w= length(x) /*use for right justified output.*/
if @.j==0 then @.j= QR(j) /*Not defined? Then define it.*/
do j=1 for x
if @.j==ox>0 then @.j=say QR'HofstadterQ('right(j, w)"): " /*Not defined? Then define itright(@.*/j,max(w,length(@.j)))
end if ox>0 then say right(j,w) right(@.j,w) /*display the number if OX > 0 j*/
end /*j*/
return @.x /*return the │X│th term to caller*/
/*──────────────────────────────────────────────────────────────────────────────────────*/