-
-
Notifications
You must be signed in to change notification settings - Fork 63
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
10 changed files
with
171 additions
and
16 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,13 @@ | ||
local function ack(m, n) | ||
if m == 0 then | ||
return n + 1 | ||
end | ||
if n == 0 then | ||
return ack(m - 1, 1) | ||
end | ||
return ack(m - 1, ack(m, n - 1)) | ||
end | ||
|
||
local res = ack(3,10) | ||
print(res) | ||
assert(res == 8189) |
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,11 @@ | ||
local function fibmod(n, m) | ||
local a, b = 0, 1 | ||
for i=1,n do | ||
a, b = b, (a + b) % m | ||
end | ||
return a | ||
end | ||
|
||
local res = fibmod(100000000, 1000000000000) | ||
print(res) | ||
assert(res == 167760546875) |
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,57 @@ | ||
local function heapsort(a) | ||
local n = #a | ||
local j, i, t | ||
local l = math.floor(n / 2) + 1 | ||
local k = n | ||
while true do | ||
if l > 1 then | ||
l = l - 1 | ||
t = a[l] | ||
else | ||
t = a[k] | ||
a[k] = a[1] | ||
k = k - 1 | ||
if k == 1 then | ||
a[1] = t | ||
return | ||
end | ||
end | ||
i = l | ||
j = l * 2 | ||
while j <= k do | ||
if j < k and a[j] < a[j+1] then | ||
j = j + 1 | ||
end | ||
if t < a[j] then | ||
a[i] = a[j] | ||
i = j | ||
j = j + i | ||
else | ||
j = k + 1 | ||
end | ||
end | ||
a[i] = t | ||
end | ||
end | ||
|
||
local function random_int(seed) | ||
return (214013 * seed + 2531011) % 2147483648 | ||
end | ||
|
||
local N = 1000000 | ||
local a = {} | ||
local rand = 123456789 | ||
for i=1,N do | ||
rand = random_int(rand) | ||
a[i] = rand | ||
end | ||
|
||
heapsort(a) | ||
|
||
local sum = 0.0 | ||
for i=1,N-1 do | ||
assert(a[i] <= a[i+1]) | ||
sum = sum + (a[i+1] - a[i]) | ||
end | ||
print(sum) | ||
assert(sum == 2147480127.0) |
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,43 @@ | ||
local function mandel(width) | ||
local height, wscale = width, 2.0/width | ||
local m, limit2 = 50, 4.0 | ||
local sum = 0 | ||
for y=0,height-1 do | ||
local Ci = 2.0*y / height - 1 | ||
for xb=0,width-1,8 do | ||
local bits = 0 | ||
local xbb = xb+7 | ||
local xblimit | ||
if xbb < width then | ||
xblimit = xbb | ||
else | ||
xblimit = width-1 | ||
end | ||
for x=xb,xblimit do | ||
bits = bits + bits | ||
local Zr, Zi, Zrq, Ziq = 0.0, 0.0, 0.0, 0.0 | ||
local Cr = x * wscale - 1.5 | ||
for i=1,m do | ||
local Zri = 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 | ||
sum = sum + bits | ||
end | ||
end | ||
return sum | ||
end | ||
|
||
local res = mandel(1024) | ||
print(res) | ||
assert(res == 20164264) |
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,21 @@ | ||
local function sieve(N) | ||
local is_prime = {} | ||
is_prime[1] = false | ||
for n=2,N do | ||
is_prime[n] = true | ||
end | ||
local nprimes = 0 | ||
for n=2,N do | ||
if is_prime[n] then | ||
nprimes = nprimes + 1 | ||
for m=n+n,N,n do | ||
is_prime[m] = false | ||
end | ||
end | ||
end | ||
return nprimes | ||
end | ||
|
||
local res = sieve(10000000) | ||
print(res) | ||
assert(res == 664579) |
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
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