How Does Modulus Divison Work

后端 未结 19 1543
栀梦
栀梦 2020-11-22 06:55

I don\'t really understand how modulus division works. I was calculating 27 % 16 and wound up with 11 and I don\'t understand why.

I can\'t

相关标签:
19条回答
  • 2020-11-22 07:19

    This was the best approach for me for understanding modulus operator. I will just explain to you through examples.

    16 % 3
    

    When you division these two number, remainder is the result. This is the way how i do it.

    16 % 3 = 3 + 3 = 6; 6 + 3 = 9; 9 + 3 = 12; 12 + 3 = 15
    

    So what is left to 16 is 1

    16 % 3 = 1
    

    Here is one more example: 16 % 7 = 7 + 7 = 14 what is left to 16? Is 2 16 % 7 = 2

    One more: 24 % 6 = 6 + 6 = 12; 12 + 6 = 18; 18 + 6 = 24. So remainder is zero, 24 % 6 = 0

    0 讨论(0)
  • 2020-11-22 07:20

    Lets say you have 17 mod 6.

    what total of 6 will get you the closest to 17, it will be 12 because if you go over 12 you will have 18 which is more that the question of 17 mod 6. You will then take 12 and minus from 17 which will give you your answer, in this case 5.

    17 mod 6=5

    0 讨论(0)
  • 2020-11-22 07:21

    When we divide two integers we will have an equation that looks like the following:

    A/B​​ =Q remainder R

    A is the dividend; B is the divisor; Q is the quotient and R is the remainder

    Sometimes, we are only interested in what the remainder is when we divide A by B. For these cases there is an operator called the modulo operator (abbreviated as mod).

    Examples

    16/5= 3 Remainder 1  i.e  16 Mod 5 is 1.
    0/5= 0 Remainder 0 i.e 0 Mod 5 is 0.
    -14/5= 3 Remainder 1 i.e. -14 Mod 5 is 1.
    

    See Khan Academy Article for more information.

    In Computer science, Hash table uses Mod operator to store the element where A will be the values after hashing, B will be the table size and R is the number of slots or key where element is inserted.

    See How does a hash table works for more information

    0 讨论(0)
  • 2020-11-22 07:22

    Most explanations miss one important step, let's fill the gap using another example.

    Given the following:

    Dividend: 16
    Divisor: 6
    

    The modulus function looks like this:

    16 % 6 = 4
    

    Let's determine why this is.

    First, perform integer division, which is similar to normal division, except any fractional number (a.k.a. remainder) is discarded:

    16 / 6 = 2
    

    Then, multiply the result of the above division (2) with our divisor (6):

    2 * 6 = 12
    

    Finally, subtract the result of the above multiplication (12) from our dividend (16):

    16 - 12 = 4
    

    The result of this subtraction, 4, the remainder, is the same result of our modulus above!

    0 讨论(0)
  • 2020-11-22 07:24

    I would like to add one more thing:

    it's easy to calculate modulo when dividend is greater/larger than divisor

    dividend = 5 divisor = 3

    5 % 3 = 2

    3)5(1
      3
    -----
      2
    

    but what if divisor is smaller than dividend

    dividend = 3 divisor = 5

    3 % 5 = 3 ?? how

    This is because, since 5 cannot divide 3 directly, modulo will be what dividend is

    0 讨论(0)
  • 2020-11-22 07:25

    Very simple: a % b is defined as the remainder of the division of a by b.

    See the wikipedia article for more examples.

    0 讨论(0)
提交回复
热议问题