使用Lua实现: -- 斐波那契数列
代码语言:javascript复制function Fibonacci(n)
if n==0 then
return 0;
elseif n==1 or n==2 then
return 1;
else
return Fibonacci(n-2) Fibonacci(n-1);
end
end
print(Fibonacci(3));
-- 角谷猜想
代码语言:javascript复制function CornerValley(n)
-- if a==1 then
-- return 1;
-- end
-- if a%2 == 0 then
-- return CornerValley(a/2);
-- elseif a%2 == 1 then
-- return CornerValley(a*3 1);
-- end
while n~=1 do
if n%2 == 1 then
n = n*3 1;
else
n= n/2;
end
end
end
print(CornerValley(233));
-- 99乘法表
代码语言:javascript复制function Multiplication()
for i = 1, 9 do
str = ""
for j = 1, i do
str =str .. (i.."*"..j.."="..i*j.." ");
end
print(str);
end
end
Multiplication();
-- 冒泡排序
代码语言:javascript复制ListTable = {1,3,9,6,2,4};
function PopSort(Table)
for i = 1, #Table-1 do
for j = i, #Table do
if Table[i] > Table[j] then
Table[i],Table[j] = Table[j],Table[i];
end
end
end
end
PopSort(ListTable);
output = "";
for i = 1, #ListTable do
output = output .." ".. ListTable[i];
end
print(output);