Simple question. Hopefully a simple answer. Now I've scowled at the math function dump (Wiki for what seems like forever, looking for a way to check if a # is prime or not. I've had no luck.
Maybe I read the wiki page the wrong way, maybe I didn't. Here's an example for how I'd use a math function to check if something is odd or even:
1 | Example = math.oe( 7 ) --"oe" stands Odd, Even |
2 | if Example = = true then --the function would output true if it e=is even and false if it isn't |
3 | --do stuff |
4 | end |
There is an arithmetic operator in Lua that is called modulus(%) that divides two numbers and then returns the remainder. For example, 5%2 will return 1 because 2 goes two times into 5 and the remainder will be 1, 2*2 =4 5-4=1. So if any number divided by 2 got a remainder of 1 then the number is odd, if not then it's even. Are you with me so far?
1 | local Example = 7 |
2 | if Example% 2 = = 0 then |
3 | print ( "even" ) |
4 | else |
5 | print ( "odd" ) |
6 | end |
Use modulus %
If it goes evenly into %# then there will be no remainder
1 | print ( 4 % 2 ) --0 Even number |
2 | print ( 3 % 2 ) --1 Odd number |