Skip to content

Commit c87f0cf

Browse files
committed
Add new test programs
1 parent 14f12b3 commit c87f0cf

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

tests/closures.lua

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
local count = 0;
2-
for i = 0, 100, 1 do
3-
local x = function()
4-
i = i + 1;
5-
end
6-
x();
1+
local arr = {}
2+
for i = 1, 100 do
3+
local x;
4+
x = (x or 1) + i;
5+
arr[i] = function()
6+
return x;
7+
end
78
end
8-
print(count);
9+
10+
for i, func in ipairs(arr) do
11+
print(func())
12+
end

tests/fibonacci.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Print the fibonacci sequence
2+
local function fibonacci(max)
3+
local a, b = 0, 1
4+
while a < max do
5+
print(a)
6+
a, b = b, a + b
7+
end
8+
end
9+
10+
fibonacci(1000)

tests/primes.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- print first n primes
2+
local function primes(n)
3+
local function isPrime(n)
4+
for i = 2, math.sqrt(n) do
5+
if n % i == 0 then
6+
return false
7+
end
8+
end
9+
return true
10+
end
11+
for i = 2, n do
12+
if isPrime(i) then
13+
print(i)
14+
end
15+
end
16+
end
17+
18+
primes(20)

0 commit comments

Comments
 (0)