lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


You can make the code ~20% faster using local variables:
  
  function sieve(n)
    local x = {}
    local iter = 0
    repeat
      x[1] = 0
      local i = 2
      repeat
        x[i] = 1
        i = i + 1
      until i > n
      local p = 2
      while(p * p <= n) do
        local j = p
        while(j <= n) do
          x[j] = 0
          j = j + p
        end
        repeat
          p = p + 1
        until x[p] == 1
      end
      iter = iter + 1
    until iter == 101
  end

-- Roberto