RosettaCodeData/Task/Hofstadter-Q-sequence/Pluto/hofstadter-q-sequence.pluto
2026-04-30 12:34:36 -04:00

18 lines
466 B
Text

require "table2"
local fmt = require "fmt"
local N = 100_000
local q = table.rep(N, 0)
q[0] = 0
q[1] = 1
q[2] = 1
for n = 3, N do q[n] = q[n - q[n-1]] + q[n - q[n-2]] end
print("The first ten terms of the Hofstadter Q sequence are:")
fmt.lprint(q:slice(1, 10))
print($"\nThe thousandth term is {q[1000]}.")
local flips = 0
for n = 2, N do
if (q[n] < q[n-1]) then flips += 1 end
end
print($"\nThere are {fmt.int(flips)} flips in the first {fmt.int(N)} terms.")