Jump to content

Hofstadter Q sequence: Difference between revisions

no edit summary
imported>Maxima enthusiast
No edit summary
Line 2,661:
<pre>>> sum(diff(Qsequence(100000))<0)
ans = 49798
</pre>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that return the terms of the Hofstadter Q sequence */
hofstadter(n):=block(
if member(n,[1,2]) then L[n]:1 else L[n]:L[n-L[n-1]]+L[n-L[n-2]],
L[n])$
 
/* Test cases */
/* First ten terms */
makelist(hofstadter(i),i,1,10);
 
/* 1000th term */
last(makelist(hofstadter(i),i,1,1000));
</syntaxhighlight>
{{out}}
<pre>
[1,1,2,3,3,4,5,5,6,6]
 
502
</pre>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.