How to see if a number is evenly divisible by 4

I am attempting to see if a function, given a number, can return true or false depending on if that number is divisible by 4. I have attempted the following

function divby4(number)
local isdivby4 = false
local loopnum = 1
while isdivby4 == false do
function divby4(number)
local isdivby4 = false
local loopnum = 1
while isdivby4 == false do
wait()
if (4*loopnum) > number then break end
local result = number/(4*loopnum)
local resultclone = tostring(result)
if string.find(resultclone,"%.") ~= nil then
loopnum = loopnum +1
else isdivby4 = true break
end
end
return isdivby4
end

and it does work, but I feel like there is a more efficient way do to this.

3 Likes
if number%4==0 then return true else return false end
9 Likes

The modulo operator will give you the remainder of a division. If that remainder equals 0, your number is evenly divisible by that number.

local n = 12
local divider = 4
print(n%divider == 0)

This will print true.

5 Likes

You could shorten this further:

local divisible = number %4 == 0
print(divisible)
7 Likes