Hofstadter Q sequence: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Lua version)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(4 intermediate revisions by 4 users not shown)
Line 1,505:
<pre>The first 10 terms are: 1 1 2 3 3 4 5 5 6 6
The 1000th term is: 502</pre>
 
=={{header|EasyLang}}==
{{trans|Lua}}
<syntaxhighlight>
proc hofstadter limit . q[] .
q[] = [ 1 1 ]
for n = 3 to limit
q[] &= q[n - q[n - 1]] + q[n - q[n - 2]]
.
.
proc count . q[] cnt .
for i = 2 to len q[]
if q[i] < q[i - 1]
cnt += 1
.
.
.
hofstadter 100000 hofq[]
for i = 1 to 10
write hofq[i] & " "
.
print ""
print hofq[1000]
count hofq[] cnt
print cnt
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,962 ⟶ 1,988:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Hofstadter_Q_sequence}}
 
'''Solution'''
 
The following function calculate the given number of terms of the Hofstadter Q sequence:
 
[[File:Fōrmulæ - Hofstadter Q sequence 01.png]]
 
'''Case 1''' First 10 terms
 
[[File:Fōrmulæ - Hofstadter Q sequence 02.png]]
 
[[File:Fōrmulæ - Hofstadter Q sequence 03.png]]
 
'''Case 2''' Confirm and display that the 1000th term is 502
 
[[File:Fōrmulæ - Hofstadter Q sequence 04.png]]
 
[[File:Fōrmulæ - Hofstadter Q sequence 05.png]]
 
'''Case 3''' Count and display how many times a member of the sequence is less than its preceding term for terms up to and including the 100,000th term.
 
[[File:Fōrmulæ - Hofstadter Q sequence 06.png]]
 
[[File:Fōrmulæ - Hofstadter Q sequence 07.png]]
 
=={{header|Go}}==
Line 2,637 ⟶ 2,687:
<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>
 
Line 3,857 ⟶ 3,928:
q(n) < q(n-1) for n = 2 .. 100000: 49798
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program hofstadter_q;
q := [1,1];
loop for n in [3..100000] do
q(n) := q(n-q(n-1)) + q(n-q(n-2));
end loop;
 
print("First 10 terms: " + q(1..10));
print("1000th term: " + q(1000));
print("q(x) < q(x-1): " + #[x : x in [2..#q] | q(x) < q(x-1)]);
end program;</syntaxhighlight>
{{out}}
<pre>First 10 terms: [1 1 2 3 3 4 5 5 6 6]
1000th term: 502
q(x) < q(x-1): 49798</pre>
 
=={{header|Sidef}}==
Line 4,126 ⟶ 4,213:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var N = 1e5
var q = List.filled(N + 1, 0)
q[1] = 1
9,482

edits