编辑代码

function solve(xs)
    local done, i, length
    done = false
    i = 2
    --print(done)
    repeat
        if (#xs % i == 0) then
            length = math.floor(#xs / i)
            --print('len=' .. length)
            local j = 1
            local continue = true
            while (j <= length and continue) do
                for k = 1,  i - 1 do
                    --print(xs[j] .. ',' .. xs[j + k * length])
                    if (xs[j] ~= xs[j + k * length]) 
                    then 
                        continue = false; 
                        --print('con=')
                        --print(continue)
                        break;
                    end
                end
                j = j + 1
                --print('length=' .. length .. " j=" .. j)
            end
            if (continue) then done = true end
        end
        i = i + 1
        --print(done)
    until done or i >= #xs
    if (done) then 
        res = 'res=' .. length
        return res
    else return 0 end
end
print(solve({1,2,1,2}))
print(solve({1,2,1,1,2,1}))
print(solve({1,2,1,1,1,2,1,0}))