Say I need to find the multiples of 5, to check and see if a players level is a multiple of 5. How would I do this? I've done searches on many websites and I can't see that anybody's asked this really..
How would I go about finding the multiples of x?
1 answer
You can simply have an arithmetic check on the number you want to check if its a multiple or not.
The arithmetic operation is called "modulus", essentially what it does it checks the remainder of the division between 2 numbers and if it is 0 it means it is a multiple of that number ( the divisor ).
Here is an example with code:
function returnIfMultiple(a, multiple)
if ((a % multiple) == 0) then
print(a .. " is a multiple of " .. multiple .. " !");
else
print(a .. " is not a multiple of " .. multiple .. " !");
end;
end;
As you can clearly see you give this function a number and the multiple you want to check with, in your example you want to check if a number is multiple of 5. You would simply do the mathematic operation 10%5 for example and that would return you 0 implying that 10 is indeed a multiple of 5. If you try to do the same operation with other number such as 11, the remainder that will be returned by the modulus operation will not be 0 therefore it is not a multiple of 5.
Make sure to ask any questions if you have any.
:)
1 comment
It works, thank you!
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Divide x by 5, check if theres a decimal. If there is, then its not a multiple, if theres not, then it is a multiple. Ta-Da.