Hofstadter Q sequence: Difference between revisions

→‎{{header|REXX}}: added the REXX language. -- ~~~~
(Better comments second D entry)
(→‎{{header|REXX}}: added the REXX language. -- ~~~~)
Line 1,072:
print("Q(i+1) < Q(i) for i [1..100000] is true %i times." %
sum([q.seq[i] > q.seq[i + 1] for i in range(1, 100000)]))</lang>
 
=={{header|REXX}}==
<lang rexx>
/*generate Hofstrader Q sequence for any N. */
 
q.=1 /*negative #s won't have values displayed.*/
call HofstraderQ 10
call HofstraderQ -1000; say; say '1000th value='result; say
call HofstraderQ -100000
 
downs=0
do j=2 to 100000
_=j-1
downs=downs+(q.j<q._)
end
 
say downs 'terms are less then the previous term.'
exit
 
/*─────────────────────────────────────HofstraderQ subroutine───────────*/
HofstraderQ: procedure expose q.; arg x 1 ox /*get the # to gen through*/
x=abs(x) /*use the absolute value for X. */
L=length(x) /*use for right justified output.*/
 
do j=1 for x /*generate up to and including X.*/
jm1=j-1
jm2=j-2
temp1=j-q.jm1
temp2=j-q.jm2
if j>2 then q.j=q.temp1+q.temp2
if ox>0 then say right(j,L) right(q.j,L) /*if X is positive, tell.*/
end
 
return q.x /*return the Xth term to caller.*/
</lang>
Output:
<pre style="height:40ex;overflow:scroll">
1 1
2 1
3 2
4 3
5 3
6 4
7 5
8 5
9 6
10 6
 
1000th value=502
 
49798 terms are less then the previous term.
</pre>
 
=={{header|Ruby}}==