i am lost
Suppose you are in a competition to eat chocolates and the rule is:
Eat three chocolates at the same time
If you are given:
1 chocolate, you will not eat, and 1 chocolate will be left
2 chocolates, you will not eat, and 2 chocolates will be left
3 chocolates, you eat once, and 0 chocolates will be left
4 chocolates, you eat once, and 1 chocolate will be left
5 chocolates, you eat once, and 2 chocolates will be left
6 chocolates, you eat twice, and 0 chocolates will be left
7 chocolates, you eat twice, and 1 chocolate will be left
...
100 chocolates, you eat 33 times, 1 chocolate will be left
...
1502 chocolates, you eat 500 times, 2 chocolates will be left
The number of chocolates left each time is called Modulus is Mathematics, a powerful operation in computers.
As you can tell, you will always be left with less than 3 chocolates (0
or 1
or 2
) anytime.
Hence, x modulus y
is always less than y
in the range 0
to y-1
.
In programming, the modulus
operator is %
.
So, if we write the way you ate chocolates using the modulus
operator %
, it looks like:
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
7 % 3 = 1
...
100 % 3 = 1
...
1502 % 3 = 2
Now tell me, are you lost?