Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
39
Task/Hofstadter-Q-sequence/Icon/hofstadter-q-sequence.icon
Normal file
39
Task/Hofstadter-Q-sequence/Icon/hofstadter-q-sequence.icon
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
link printf
|
||||
|
||||
procedure main()
|
||||
|
||||
V := [1, 1, 2, 3, 3, 4, 5, 5, 6, 6]
|
||||
every i := 1 to *V do
|
||||
if Q(i) ~= V[i] then stop("Assertion failure for position ",i)
|
||||
printf("Q(1 to %d) - verified.\n",*V)
|
||||
|
||||
q := Q(n := 1000)
|
||||
v := 502
|
||||
printf("Q[%d]=%d - %s.\n",n,v,if q = v then "verified" else "failed")
|
||||
|
||||
invcount := 0
|
||||
every i := 2 to (n := 100000) do
|
||||
if Q(i) < Q(i-1) then {
|
||||
printf("Q(%d)=%d < Q(%d)=%d\n",i,Q(i),i-1,Q(i-1))
|
||||
invcount +:= 1
|
||||
}
|
||||
printf("There were %d inversions in Q up to %d\n",invcount,n)
|
||||
end
|
||||
|
||||
|
||||
|
||||
procedure Q(n) #: Hofstader Q sequence
|
||||
static S
|
||||
initial S := [1,1]
|
||||
|
||||
if q := S[n] then return q
|
||||
else {
|
||||
q := Q(n - Q(n - 1)) + Q(n - Q(n - 2))
|
||||
if *S = n - 1 then {
|
||||
put(S,q)
|
||||
return q
|
||||
}
|
||||
else
|
||||
runerr(500,n)
|
||||
}
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue