Reputation:
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!
Upvotes: 1
Views: 201
Reputation: 32
if (i<tonumber(a)) then
should work.
You requested an input, which will be returned as a string.
Therefore, you can't do if (i<a) then
, because you're comparing number and string via <
.
You simply can't say, that 2 is less than '4'
Upvotes: 1