Q:

Lua clear memory

I am making a simple number cracker inside of Lua and i encountered a "Stack Overflow" error(Oh the irony). I am not 100% what this error actually is as when i google it all i get is this site which iv been using for a while. I want to have a way to clear the memory in the lua file. This program runs through numbers very fast and it can run these numbers up to 2147483647 times so im assuming this is a memory issue. So is there a way to clear the memory in a lua script whilst still running the script? here is my code:
num = 0
rand = math.random(2147483647)
function Main()
    print ("Please enter your number(0 - 2147483647)")
    ui = io.read("*number")
    loop()
end
function loop()
    if rand > ui and rand ~= ui then
        rand = math.random(0, ui)
        num = num + 1
        print(rand)
    end
    if rand < ui and rand ~= ui then
        rand = math.random(ui, 2147483647)
        num = num + 1
        print(rand)
    end
    if ui ~= rand then
        loop()
    end
    if ui == rand then
        print("Number Cracked - " ..ui)
        print("It Took " ..num .." Trys To Crack Your Number")
        done = io.read()
    end
end
Main()

EDIT
Thanks to @Marc B and @Blaatz0r for commenting the answers, i was calling loop() to many times and it was causing, well a "Stack Overflow", i replace it with a while loop, thanks to @Marc B for that, here is my new code:
num = 0
rand = math.random(2147483647)
function Main()
print ("Please enter your number(0 - 2147483647)")
ui = io.read("*number")
while( ui ~= rand) do
if rand > ui and rand ~= ui then
rand = math.random(0, ui)
num = num + 1
print(rand)
end
if rand < ui and rand ~= ui then
rand = math.random(ui, 2147483647)
num = num + 1
print(rand)
end
if ui == rand then
print("Number Cracked - " ..ui)
print("It Took " ..num .." Trys To Crack Your Number")
done = io.read()
end
end
end
Main()

A:

Thanks to @Marc B and @Blaatz0r for commenting the answers, i was calling loop() to many times and it was causing, well a "Stack Overflow", i replace it with a while loop, thanks to @Marc B for that, here is my new code:
num = 0
rand = math.random(2147483647)
function Main()
    print ("Please enter your number(0 - 2147483647)")
    ui = io.read("*number")
    while( ui ~= rand) do
        if rand > ui and rand ~= ui then
            rand = math.random(0, ui)
            num = num + 1
            print(rand)
        end
        if rand < ui and rand ~= ui then
            rand = math.random(ui, 2147483647)
            num = num + 1
            print(rand)
        end
        if ui == rand then
            print("Number Cracked - " ..ui)
            print("It Took " ..num .." Trys To Crack Your Number")
            done = io.read()
        end
    end
end
Main()

