I made a code of tower of hanoi problem with recursion and run it on the online lua compiler. If I put the input over 14, it didn't run.
local num = io.read("*n")
local count = 0
function hanoi(n, st, mid, dst)
if n == 1 then
count = count + 1
print(st, dst)
else
hanoi(n-1, st, dst, mid)
count = count + 1
print(st, dst)
hanoi(n-1, mid, st, dst)
end
end
hanoi(num, 1, 2, 3)
print(count)
I think this can be solved by proper tail-call, but in my knowledge the proper tail call must return the same function. But in that code, there are two "hanoi" function in recursive.
So is this a proper tail call in lua?
function f(args)
return f(args_1), f(args_2)
end
And is there any way to make proper tail call of hanoi problem?