--[[1MIT License23Copyright (c) 2017 Gabriel de Quadros Ligneul45Permission is hereby granted, free of charge, to any person obtaining a copy6of this software and associated documentation files (the "Software"), to deal7in the Software without restriction, including without limitation the rights8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell9copies of the Software, and to permit persons to whom the Software is10furnished to do so, subject to the following conditions:1112The above copyright notice and this permission notice shall be included in all13copies or substantial portions of the Software.1415THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE21SOFTWARE.22]]23local function prequire(name) local success, result = pcall(require, name); return success and result end24local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../bench_support")2526function test()2728local Complex={type="package"}2930local function complex(x,y)31return setmetatable({ re=x, im=y }, Complex.metatable)32end3334function Complex.conj(x,y)35return complex(x.re,-x.im)36end3738function Complex.norm2(x)39local n=Complex.mul(x,Complex.conj(x))40return n.re41end4243function Complex.abs(x)44return sqrt(Complex.norm2(x))45end4647function Complex.add(x,y)48return complex(x.re+y.re,x.im+y.im)49end5051function Complex.mul(x,y)52return complex(x.re*y.re-x.im*y.im,x.re*y.im+x.im*y.re)53end5455Complex.metatable={56__add = Complex.add,57__mul = Complex.mul,58}5960local function abs(x)61return math.sqrt(Complex.norm2(x))62end6364xmin=-2.0 xmax=2.0 ymin=-2.0 ymax=2.065N=(arg and arg[1]) or 646667function level(x,y)68local c=complex(x,y)69local l=070local z=c71repeat72z=z*z+c73l=l+174until abs(z)>2.0 or l>25575return l-176end7778dx=(xmax-xmin)/N79dy=(ymax-ymin)/N8081print("P2")82print("# mandelbrot set",xmin,xmax,ymin,ymax,N)83print(N,N,255)84local S = 085for i=1,N do86local x=xmin+(i-1)*dx87for j=1,N do88local y=ymin+(j-1)*dy89S = S + level(x,y)90end91-- if i % 10 == 0 then print(collectgarbage("count")) end92end93print(S)9495end9697bench.runCode(test, "mandel")9899100