-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonacci.lua
More file actions
35 lines (29 loc) · 861 Bytes
/
Copy pathfibonacci.lua
File metadata and controls
35 lines (29 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function main()
local index = tonumber(arg[1]);
assertIndex(index);
print(fibonacci(index));
end
function fibonacci(index)
local initial = initiateMatrix();
local current = initiateMatrix();
for i = 3, index do
current = multiply(current, initial);
end
return current[1][1];
end
function initiateMatrix()
return {{ 1, 1 }, { 1, 0 }};
end
function multiply(a, b)
local topLeft = a[1][1] * b[1][1] + a[1][2] * b[2][1];
local topRight = a[1][1] * b[1][2] + a[1][2] * b[2][2];
local bottomLeft = a[2][1] * b[1][1] + a[2][2] * b[2][1];
local bottomRight = a[2][1] * b[1][2] + a[2][2] * b[2][2];
return {{ topLeft, topRight }, { bottomLeft, bottomRight }};
end
function assertIndex(index)
if (index < 1 or index > 68) then
error("Index must be a positive integer.");
end
end
main()