How should I use Modulo Operator (%/Mod).. D: please explain to me using simple numbers and porn metaphor please..
all I understand is ; let's say I have 5 cocks around me, but I can only use 3 cocks simultaneously at one time. means there's 2 cocks are left unattended. 5 % 3 = 2.. But isn't that the same with subtraction? D: can't I just 5 - 3? ( and still get the same answer 2 )..
help me.. Im just stupid ;3; I don't know what it does.. and I can't think of why it exists, and when I'm gonna use it.
In FA, you can get professional help from other peoples. more than you possibly can in the class. these peoples really helped me doing my homework since 2005..
all I understand is ; let's say I have 5 cocks around me, but I can only use 3 cocks simultaneously at one time. means there's 2 cocks are left unattended. 5 % 3 = 2.. But isn't that the same with subtraction? D: can't I just 5 - 3? ( and still get the same answer 2 )..
help me.. Im just stupid ;3; I don't know what it does.. and I can't think of why it exists, and when I'm gonna use it.
In FA, you can get professional help from other peoples. more than you possibly can in the class. these peoples really helped me doing my homework since 2005..
Category All / All
Species Wolf
Size 1024 x 768px
File Size 175.1 kB
Listed in Folders
...but seriously, you don't always know the left number (that'd be the x in x%y); say you want to print all the multiples of 7 from 1 to 1000 (trivial, but still); you'd loop from 1 to 1000 and print the loop variable if it's modulo with 7 is 0 (no remainder, meaning it's evenly divisible by 7)
say you have 7 furs to make happy and very messy in one day but you wanted to spend the same amount of time with them. There are 24 hours in a day and you only get paid by the hour so you would go 24mod7 (also known as 24%7) which would mean there were 3 hours left that you could use to get cleaned up between sessions =3 and make sure to clean up good because you could get tips too ^^
Anyone can tell you what it does. But here's my most common use of it.
Example: loop through an array and print each element 1000 times, wrapping around when reaching the end of the array. Here is that in Java:
public class Test
{
public static void main(String[] args)
{
String[] days = { "Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun" };
for (int i = 0; i < 1000; ++i)
{
System.out.println(days[i % days.length]);
}
}
}
You will get:
Mon
Tue
Wed
Thur
Fri
Sat
Sun
Mon
Tue
Wed
Thur
Fri
Sat
Sun
Mon
Tue
......
Yes, you can do this via subtractions, but the modulo operator is more efficient.
Example: loop through an array and print each element 1000 times, wrapping around when reaching the end of the array. Here is that in Java:
public class Test
{
public static void main(String[] args)
{
String[] days = { "Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun" };
for (int i = 0; i < 1000; ++i)
{
System.out.println(days[i % days.length]);
}
}
}
You will get:
Mon
Tue
Wed
Thur
Fri
Sat
Sun
Mon
Tue
Wed
Thur
Fri
Sat
Sun
Mon
Tue
......
Yes, you can do this via subtractions, but the modulo operator is more efficient.
Modulo is basically used to check if a number divided by another number leaves a remainder. 4/2 would leave a remainder of 0. So, 4%2 = 0. Just like in the first example of 10%4=2. 2 is the remainder when 10 is divided by 4. The main use of modulo is for sorting algorithms. A good way of sorting things is to place them into equally divided buckets. You can use modulo to do that.
i use it in some simple timing programs to track a whenever a specific tick has occurred.. maybe i would like something to fire every 5 ticks, i would look for whenever the tick is divisible by 5, modulo makes that incredibly easy. whenever tick mod 5 = 0 said event would happen.. I'm sure it has other uses but this is the one i use it most often for
One thing you can use this for is indexing around a circle. Say you've got 5 dicks around you, indexed from 0 to 4, and you decide to suck every other one in order. So you get from the 0th, through the 2nd, to the 4th pretty simply (current index + 2), but then you get to the 6th. There ain't that many cocks around you, though. So you start indexing from the beginning ((4 + 2) mod 5 = 1), getting you the 1-indexed dick to suck next.
And I guess you could understand modulo in one of two ways:
n mod m = n - floor(n/m)*m
or a function like this
mod(n,m){
while(n>=m) n-=m
while(n<0) n+=m
return n
}
There's also some slightly different variant in terms of dealing with negative numbers which may give negative numbers as a result (basically cares more about distance from 0 than which is bigger). But I think it's totally useless since all I ever use modulo for is indexing. Unfortunately, some languages use that by default.
And I guess you could understand modulo in one of two ways:
n mod m = n - floor(n/m)*m
or a function like this
mod(n,m){
while(n>=m) n-=m
while(n<0) n+=m
return n
}
There's also some slightly different variant in terms of dealing with negative numbers which may give negative numbers as a result (basically cares more about distance from 0 than which is bigger). But I think it's totally useless since all I ever use modulo for is indexing. Unfortunately, some languages use that by default.
The modulo operater gives the remainder of a division. Consider 10 / 3. 3 goes into 10 completely three times, with a remainder of 1. The modulo operator returns that remainder. So 10 % 3 = 1.
15 / 4 = 3 and remainder 3
15 % 4 = 3
25 / 2 = 12 remainder 1
25 % 2 = 1
And so on and so forth. If it divides evenly (without leaving a decimal or fraction, just an integer), then the modulo operator will return 1. So any even number modulo'd by 2 will equal 0, because there is no remainder.
It's used a lot for when you want to do something every so often in a loop. Say that you want to do something every fourth iteration. You make an if statement with (iterationCount % 4 == 0) as the condition, and put what you want to do within the block if the if statement. That will activate every fourth iteration.
15 / 4 = 3 and remainder 3
15 % 4 = 3
25 / 2 = 12 remainder 1
25 % 2 = 1
And so on and so forth. If it divides evenly (without leaving a decimal or fraction, just an integer), then the modulo operator will return 1. So any even number modulo'd by 2 will equal 0, because there is no remainder.
It's used a lot for when you want to do something every so often in a loop. Say that you want to do something every fourth iteration. You make an if statement with (iterationCount % 4 == 0) as the condition, and put what you want to do within the block if the if statement. That will activate every fourth iteration.
in elementary school we devided the numbers and everythign that remain called we 'Rest' (i am from german) and modulo give out this 'rest' mean:
5 % 3 = 2
cause 5 : 3 = 1 'Rest' 2 (is cause 5 contain 1 times 3 but then you got still 2 remaining)
another example 10 % 2 = 0 cause in 10 is 5 times the 2 and nothing remains.
well, you can use modulo for example to filter out numbers, we have written a program that filter out any number that contain a 7 or is devidable by 7
namespace filter
{
class Program
{
static void Main(string[] args)
{
int number;
for (number = 0; number <= 250; number++)
{
if ((number % 7 != 0) && (number / 10 % 10 != 7) && (number % 10 != 7))
{
Console.Write("\t{0} ", number);
}
}
}
}
}
the output wiill be:
1 2 3 4 5 6 8 9 10
11 12 13 15 16 18 19 20 22 23
24 25 26 29 30 31 32 33 34 36
38 39 40 41 43 44 45 46 48 50
51 52 53 54 55 58 59 60 61 62
64 65 66 68 69 80 81 82 83 85
86 88 89 90 92 93 94 95 96 99
100 101 102 103 104 106 108 109 110 111
113 114 115 116 118 120 121 122 123 124
125 128 129 130 131 132 134 135 136 138
139 141 142 143 144 145 146 148 149 150
151 152 153 155 156 158 159 160 162 163
164 165 166 169 180 181 183 184 185 186
188 190 191 192 193 194 195 198 199 200
201 202 204 205 206 208 209 211 212 213
214 215 216 218 219 220 221 222 223 225
226 228 229 230 232 233 234 235 236 239
240 241 242 243 244 246 248 249 250
please look close at if ((number % 7 != 0) && (number / 10 % 10 != 7) && (number % 10 != 7))
first this : (number % 7 != 0) means, if number is not devidable through 7 then set to true, cause if you will get 0 from modulo then is number devidable through 7 and will this will change to false
then : (number / 10 % 10 != 7)
number is an integer number so everything after . will be removed, anyway this will check if the second char in the number is a 7.
first devide the number by 10, make a 172 to a 17 cause its an integer number (so 17 isntead of 17.2 cause integer doesn't know numbers with a dot)
so if you got a 17 the monolo 10 so 17 % 10 will give out 7 and that is not != 7 (!= means is not, so the oposite from =)
last thing is checking if first char is a 7, so just the number % 10 != 7
and cause everything is connected with a && (and) means if anny of the controls give out a FALSE the number will not be printed, so you got a program that will check.
1. Is number devidable through 7?
2. contain the number on the first char a 7?
3. contain the number on the second char a 7?
so then, hope this kinda complicated explaination will help you in any kind.
try it out this simple C# console program, learn by testing^^
5 % 3 = 2
cause 5 : 3 = 1 'Rest' 2 (is cause 5 contain 1 times 3 but then you got still 2 remaining)
another example 10 % 2 = 0 cause in 10 is 5 times the 2 and nothing remains.
well, you can use modulo for example to filter out numbers, we have written a program that filter out any number that contain a 7 or is devidable by 7
namespace filter
{
class Program
{
static void Main(string[] args)
{
int number;
for (number = 0; number <= 250; number++)
{
if ((number % 7 != 0) && (number / 10 % 10 != 7) && (number % 10 != 7))
{
Console.Write("\t{0} ", number);
}
}
}
}
}
the output wiill be:
1 2 3 4 5 6 8 9 10
11 12 13 15 16 18 19 20 22 23
24 25 26 29 30 31 32 33 34 36
38 39 40 41 43 44 45 46 48 50
51 52 53 54 55 58 59 60 61 62
64 65 66 68 69 80 81 82 83 85
86 88 89 90 92 93 94 95 96 99
100 101 102 103 104 106 108 109 110 111
113 114 115 116 118 120 121 122 123 124
125 128 129 130 131 132 134 135 136 138
139 141 142 143 144 145 146 148 149 150
151 152 153 155 156 158 159 160 162 163
164 165 166 169 180 181 183 184 185 186
188 190 191 192 193 194 195 198 199 200
201 202 204 205 206 208 209 211 212 213
214 215 216 218 219 220 221 222 223 225
226 228 229 230 232 233 234 235 236 239
240 241 242 243 244 246 248 249 250
please look close at if ((number % 7 != 0) && (number / 10 % 10 != 7) && (number % 10 != 7))
first this : (number % 7 != 0) means, if number is not devidable through 7 then set to true, cause if you will get 0 from modulo then is number devidable through 7 and will this will change to false
then : (number / 10 % 10 != 7)
number is an integer number so everything after . will be removed, anyway this will check if the second char in the number is a 7.
first devide the number by 10, make a 172 to a 17 cause its an integer number (so 17 isntead of 17.2 cause integer doesn't know numbers with a dot)
so if you got a 17 the monolo 10 so 17 % 10 will give out 7 and that is not != 7 (!= means is not, so the oposite from =)
last thing is checking if first char is a 7, so just the number % 10 != 7
and cause everything is connected with a && (and) means if anny of the controls give out a FALSE the number will not be printed, so you got a program that will check.
1. Is number devidable through 7?
2. contain the number on the first char a 7?
3. contain the number on the second char a 7?
so then, hope this kinda complicated explaination will help you in any kind.
try it out this simple C# console program, learn by testing^^
And you use it when you want to figure out how far off of a division something is. It's useful for calculating checksums and things like that. Credit card numbers, for instance in the case of a 16-digit credit card number, have 15 real digits and a check digit. You take the first digit, add it to 3*second digit, add to third digit, add to 3*fourth digit, etc until the 15th digit. Then you take that number, mod 10 it, and the 16th digit of the credit card number must be the difference so the final sum is equally divisible by 10.
FA+

Comments