27 lines
1.1 KiB
Plaintext
27 lines
1.1 KiB
Plaintext
do --[[ find elements of the Van Eck Sequence - first term is 0, following
|
|
terms are 0 if the previous was the first appearance of the element
|
|
or how far back in the sequence the last element appeared otherwise
|
|
]]
|
|
local fmt = require( "fmt" ) -- RC formatting module
|
|
|
|
local function VanEck( n : number ) : table -- returns the first n elements of the Van Eck sequence
|
|
local result <const> = {} for i = 1, n do result[ i ] = 0 end
|
|
local pos <const> = {} for i = 1, n + 1 do pos[ i ] = 0 end
|
|
for i = 2, n do
|
|
local j <const> = i - 1
|
|
local prev <const> = result[ j ] + 1; -- prev is indexed from 1, not 0
|
|
if pos[ prev ] != 0 then
|
|
-- not a new element
|
|
result[ i ] = j - pos[ prev ]
|
|
end
|
|
pos[ prev ] = j
|
|
end
|
|
return result
|
|
end
|
|
|
|
local veSeq <const> = VanEck( 1000 ) -- construct the first 1000 terms of the sequence
|
|
-- show the first and last 10 elements
|
|
fmt.lprint( veSeq:slice( 1, 10 ) )
|
|
fmt.lprint( veSeq:slice( # veSeq - 9, # veSeq ) )
|
|
end
|