Jump to content




Number Factorizer

lua

  • You cannot reply to this topic
No replies to this topic

#1 jimthenerd1999

  • Members
  • 5 posts

Posted 15 March 2014 - 06:23 PM

I made a program which factorizes a number.

Here s the source code:


function getFactor(n)
for i = 2, n-1 do
  if n % i == 0 then
   return i;
  end
end
return -1;
end
n = io.read("Enter a number: ");
result = "" .. n .. "= ";
count = 0;
while true do
f = getFactor(n);
if (f == -1) then
  if (count == 0) then
   result = result .. n;
   break;
  else
   result = result .. "*" .. n;
   break;
  end
end
if (count == 0) then
  result = result .. f;
else
  result = result .. "*" .. f;
end
n = n / f;
count = count + 1;

end

print(result);

Hope it can help you.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users