-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
title: 'Lua' | ||
|
||
strategy: | ||
matrix: | ||
command: | ||
- title: 'Lua' | ||
command: 'lua %s' | ||
- title: 'LuaJIT' | ||
command: 'luajit %s' | ||
tags: | ||
- JIT | ||
|
||
files: | ||
- primes/Simple.lua | ||
- treap/Naive.lua | ||
- recursion/Tak.lua | ||
- mandelbrot/Simple.lua |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#!/usr/bin/env lua | ||
|
||
function index() | ||
for y = -39, 38 do | ||
io.write("\n") | ||
|
||
for x = -39, 38 do | ||
local i = mandelbrot( | ||
x / 40.0, | ||
y / 40.0 | ||
) | ||
|
||
if i == 0 then | ||
io.write("*") | ||
else | ||
io.write(" ") | ||
end | ||
end | ||
end | ||
|
||
io.write("\n") | ||
end | ||
|
||
function mandelbrot(x, y) | ||
local cr = y - 0.5 | ||
local ci = x | ||
local zi = 0.0 | ||
local zr = 0.0 | ||
local i = 0 | ||
|
||
while true do | ||
i = i + 1 | ||
|
||
local temp = zr * zi | ||
|
||
local zr2 = zr * zr | ||
local zi2 = zi * zi | ||
|
||
zr = zr2 - zi2 + cr | ||
zi = temp + temp + ci | ||
|
||
if zi2 + zr2 > 16 then | ||
return i | ||
end | ||
|
||
if i > 5000 then | ||
return 0 | ||
end | ||
end | ||
end | ||
|
||
local function main() | ||
local startTime = os.clock() | ||
|
||
index() | ||
|
||
local endTime = os.clock() | ||
local duration = (endTime - startTime) * 1000 | ||
|
||
print(string.format("Execution time: %.0fms", duration)) | ||
end | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env lua | ||
|
||
local NUMBER = 1000000 | ||
|
||
local function getLastPrime(count) | ||
local lastPrime = 2 | ||
|
||
-- Traverse each number from 3 to N, skipping even numbers | ||
for i = 3, count, 2 do | ||
local isPrime = true | ||
local limit = math.floor(math.sqrt(i)) | ||
|
||
for j = 3, limit, 2 do | ||
if i % j == 0 then | ||
isPrime = false | ||
break | ||
end | ||
end | ||
|
||
if isPrime then | ||
lastPrime = i | ||
end | ||
end | ||
|
||
return lastPrime | ||
end | ||
|
||
local function main() | ||
local startTime = os.clock() | ||
|
||
local lastPrime = getLastPrime(NUMBER) | ||
print("Last prime: " .. lastPrime) | ||
|
||
local endTime = os.clock() | ||
local durationMs = (endTime - startTime) * 1000 | ||
|
||
print("Execution time: " .. durationMs .. "ms") | ||
end | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
local function tak(x, y, z) | ||
if y < x then | ||
return tak(tak(x - 1, y, z), tak(y - 1, z, x), tak(z - 1, x, y)) | ||
else | ||
return z | ||
end | ||
end | ||
|
||
local function benchmark() | ||
local startTime = os.clock() * 1000 | ||
|
||
print(tak(30, 22, 12)) | ||
|
||
local endTime = os.clock() * 1000 | ||
local duration = endTime - startTime | ||
|
||
print("Execution time: " .. duration .. "ms") | ||
end | ||
|
||
benchmark() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
math.randomseed(os.time()) | ||
|
||
Node = {} | ||
Node.__index = Node | ||
|
||
function Node:new(x) | ||
local instance = setmetatable({}, self) | ||
instance.x = x | ||
instance.y = math.random() | ||
instance.left = nil | ||
instance.right = nil | ||
return instance | ||
end | ||
|
||
function merge(lower, greater) | ||
if lower == nil then | ||
return greater | ||
end | ||
|
||
if greater == nil then | ||
return lower | ||
end | ||
|
||
if lower.y < greater.y then | ||
lower.right = merge(lower.right, greater) | ||
return lower | ||
else | ||
greater.left = merge(lower, greater.left) | ||
return greater | ||
end | ||
end | ||
|
||
function splitBinary(orig, value) | ||
if orig == nil then | ||
return nil, nil | ||
end | ||
|
||
if orig.x < value then | ||
local rightLower, rightGreater = splitBinary(orig.right, value) | ||
orig.right = rightLower | ||
return orig, rightGreater | ||
else | ||
local leftLower, leftGreater = splitBinary(orig.left, value) | ||
orig.left = leftGreater | ||
return leftLower, orig | ||
end | ||
end | ||
|
||
function merge3(lower, equal, greater) | ||
return merge(merge(lower, equal), greater) | ||
end | ||
|
||
SplitResult = {} | ||
SplitResult.__index = SplitResult | ||
|
||
function SplitResult:new(lower, equal, greater) | ||
local instance = setmetatable({}, self) | ||
instance.lower = lower | ||
instance.equal = equal | ||
instance.greater = greater | ||
return instance | ||
end | ||
|
||
function split(orig, value) | ||
local lower, equalGreater = splitBinary(orig, value) | ||
local equal, greater = splitBinary(equalGreater, value + 1) | ||
return SplitResult:new(lower, equal, greater) | ||
end | ||
|
||
Tree = {} | ||
Tree.__index = Tree | ||
|
||
function Tree:new() | ||
local instance = setmetatable({}, self) | ||
instance.root = nil | ||
return instance | ||
end | ||
|
||
function Tree:hasValue(x) | ||
local splited = split(self.root, x) | ||
local res = splited.equal ~= nil | ||
self.root = merge3(splited.lower, splited.equal, splited.greater) | ||
return res | ||
end | ||
|
||
function Tree:insert(x) | ||
local splited = split(self.root, x) | ||
if splited.equal == nil then | ||
splited.equal = Node:new(x) | ||
end | ||
self.root = merge3(splited.lower, splited.equal, splited.greater) | ||
end | ||
|
||
function Tree:erase(x) | ||
local splited = split(self.root, x) | ||
self.root = merge(splited.lower, splited.greater) | ||
end | ||
|
||
function main() | ||
local tree = Tree:new() | ||
local cur = 5 | ||
local res = 0 | ||
|
||
for i = 1, 999999 do | ||
local a = i % 3 | ||
cur = (cur * 57 + 43) % 10007 | ||
|
||
if a == 0 then | ||
tree:insert(cur) | ||
elseif a == 1 then | ||
tree:erase(cur) | ||
elseif a == 2 then | ||
res = res + (tree:hasValue(cur) and 1 or 0) | ||
end | ||
end | ||
|
||
print(res) | ||
end | ||
|
||
local startTimeMs = math.floor(os.clock() * 1000) | ||
main() | ||
local endTimeMs = math.floor(os.clock() * 1000) | ||
local durationMs = endTimeMs - startTimeMs | ||
|
||
print("Execution time: " .. durationMs .. "ms") |