Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How would I go about finding the multiples of x?

Asked by 5 years ago

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..

0
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. Stephenthefox 94 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

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.

:)

0
It works, thank you! ImperiumDomini2 39 — 5y
Ad

Answer this question