I am trying to make this program to factor a number work in lua, and everything works except for this one line of code in it. Here's the code:
function factor(a)
print("factoring: " .. a)
print()
totali = 0
totaldiv = 0
for i = 1, a do
if (a%i == 0) then
if (i<a) then
totaldiv = totaldiv + 1
end
print(i)
i = i + 1
totali = totali + 1
else
i = i + 1
end
end
if totali == 2 then
print("That is a prime number!")
elseif totaldiv == a then
print("That is a perfect number!")
end
end
io.write("Enter a number to factor: ")
some = io.read()
factor(some)
io.read()
The offensive line is if (i<a) then from what I've seen.
What am I doing wrong?
Thanks!
io.readwill be a string, yes. You need to convert it to a number if you want that (lua will do that implicitly in some cases too).io.readand string values. Fix your post, clearly explain the exact problem and I might be able to help more.factor(some)withfactor(some + 0)orfactor(tonumber(some))