I’m solving some Euler problems, and the % operator is very useful. I take it to mean ‘false is a number is divisible by n’, or:
if(6 % 2){
//won't be done
So what’s the opposite of it? Some variant of !%
I’m solving some Euler problems, and the % operator is very useful. I take it to mean ‘false is a number is divisible by n’, or:
if(6 % 2){
//won't be done
So what’s the opposite of it? Some variant of !%
It's called the Modulus operator, and it returns the remainder of an equation. Ex:
9 % 2 = 1
Check the docs for a more in depth example: http://msdn.microsoft.com/en-us/library/h6zfzfy7%28v=vs.80%29.aspx
The opposite would be a division operator applied to integers, thus ignoring the remainder.
Ex:
(int)9 / (int)2 = 4
Well as they above explain, its a remainder.
if(6 % 2) { // 6 divided by 2 and the remainder is 0, because 6/3 is 2.
}
Actually, this is not valid in C#. So you’re probably using UnityScript and unity script converts this into something like if((6%2)!=0)
That being said:
If you use modulo, the correct way of doing it is
if((6 % 2) == 0)) // will enter
if((6 % 2) != 0)) // will not enter
or
int remain = 7 % 2; // remain will be 1, because 6 dived through 2 is 3, 1 remains
if(remain!=0)
if(remain==0) // depending what you want
I think there is not really an opposite of %, because x%y mean a number (except when y=0 - division by zero error). 6!%2 for example, does not make too much sense for me (looks like 'if is not possible to divide 6 with 2'). The simplest solution for you to use % is:
if(n%y== z) {
//won't be done
} else {
//well, done
}
Actually:
if(n%2== 0) { // Divisible by 2
//won't be done
} else { // opposite
//well, done
}