-- The Computer Language Benchmarks Game
-- http://benchmarksgame.alioth.debian.org/
-- contributed by Mike Pall

local arg1 = arg and arg[1] or 100 
local width :integer = tonumber(arg1)
local height :integer, wscale :number = width, 2.0/width
local m:integer, limit2 :number = 50, 4.0
local write, char = io.write, string.char

--write("P4\n", width, " ", height, "\n")
t1 = os.time()
for y=0,height-1 do
  local Ci :number = 2.0*y / height - 1
  for xb=0,width-1,8 do
    local bits :integer = 0
    local xbb :integer = xb+7
    local xblimit :integer
    if xbb < width then
      xblimit = xbb
    else
      xblimit = width-1
    end
    for x=xb,xblimit do
      bits = bits + bits
      local Zr :number, Zi :number, Zrq :number, Ziq :number = 0.0, 0.0, 0.0, 0.0
      local Cr :number = x * wscale - 1.5
      for i=1,m do
        local Zri :number = Zr*Zi
        Zr = Zrq - Ziq + Cr
        Zi = Zri + Zri + Ci
        Zrq = Zr*Zr
        Ziq = Zi*Zi
        if Zrq + Ziq > limit2 then
          bits = bits + 1
          break
        end
      end
    end
    if xbb >= width then
      for x=width,xbb do bits = bits + bits + 1 end
    end
    -- write(char(255-bits))
    --print(bits);
  end
end
t2 = os.time()
print(t2-t1)
