Advertisement
Terrah

isprime

Mar 11th, 2015
1,419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.79 KB | None | 0 0
  1. local function IsPrime(numb) if numb<=1 or numb%2==0 then return false; end for n = 2, numb^(1/2) do if numb % n == 0 then return false; end end return true; end
  2.  
  3. local function StringToNumber(str)
  4.  
  5.     if tonumber(str)~=nil then
  6.         return tonumber(str);
  7.     end
  8.  
  9.     str = tostring(str):lower();
  10.    
  11.     local number = 0;
  12.    
  13.     for n=1,str:len() do
  14.         number = number + str:byte(n);
  15.     end
  16.  
  17.     return number;
  18. end
  19.  
  20. return function(msg,usr,chan)
  21.  
  22.         if msg==nil or msg=="" then
  23.                 msg = usr;
  24.         end
  25.  
  26.         local numb = StringToNumber(msg);
  27.  
  28.         if IsPrime(numb) then
  29.                 print("@"..usr.." " .. msg .. " (".. numb ..") is a prime number");
  30.         else
  31.                 print("@"..usr.." " .. msg .. " (".. numb ..") is NOT a prime number");
  32.         end
  33. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement