Please help make Modulo Simple To Understand

154 views Asked by At

(Sorry in advance if this has been asked before, I searched and couldn't find a similar question)

So I believe modulo (%) gives me the remainder of a long division equation. so 2%4 =0r. So in simple terms a modulo equation that equals zero should be an even number. and a modulo equation that equals 1, should be an odd number? Is that correct?

Here's where I begin to confuse myself.

What about equations that equal an even or odd remainder, would that still output an equal or odd number.

For instance. 5%149 equals 4r.. the remainder is an even number, so is the output all even numbers.. or does the very fact that there is any remainder at all mean that the output will be odd numbers??

TLDR, is modulo as simple as 0r outputs even numbers. And anything with 1 or more remainder outputs odd numbers.

2

There are 2 answers

0
JJ173 On

Modulo (or modulus) is used to see the remainder of a division.

You can flip it on it's head and use multiplication to help you out as well if necessary. I've provided some examples.

Try doing something like this:

From the equation you posted in your example: 149 % 5 would give you the remainder of 4. Reason for this: The last multiple of 5 you can get before 149 is 145, and your modulus equation is telling you that you have 4 left over.

Now, if you were to do something like 150 % 5, your remainder would be 0 because 150 is a safe multiple of 5.

Some documentation should hopefully help you understand this a bit better as well: https://docs.onux.com/en-US/Developers/JavaScript-PP/Language/Reference/Expressions/arithmetic-operators/modulus

Some examples to help you understand the remainder: 10 % 5 = 0 Since 5 x 2 = 10 9 % 3 = 0 Since 9 x 3 = 9 6 % 2 = 0 Since 2 x 3 = 6 7 % 2 = 1 Since you can only multiply 2 three times to get 6, you are left with a remainder of 1.

0
cleverpiggy On

a modulo equation that equals zero should be an even number. and a modulo equation that equals 1, should be an odd number?

You've probably seen modulo as a way of testing for evenness but this is not right. It should read

a modulo by 2 operation that equals zero is an even number

ie. x % 2 == 0 implies x is even. Because x is divisible by 2. x % 3 == 0 means x is divisible by 3.

Here's one way I learned to look at it. Consider an analogue clock with n hours on in (perhaps n=12 or n=24 or some other funny clock). The first number in the modulo operation tells you how many hours forward to traverse, going round and round the circle. The second number (n) tells you how many hours is built into the clock.

Here are some examples:

You go forward 5 on a 12 hour clock and land on 5 o'clock.

5 % 12 == 5

You go forward 13, completing one full loop plus one more hour, landing on 1 o'clock.

13 % 12 == 1

You go forward 24, completing 2 full loops but landing at the starting point, 0. (ok most clocks have 12 at the top but its the same as 0.)

24 % 12 == 0

Consider a clock or spinning wheel with 4 categories.

Start at the base and take 7 steps forward. That give you one full traversal (4 steps) then 3 more steps lands you on the 3rd item.

7 % 4 == 3

You've just stepped forward 2. Because the wheel has 4 slots, the counting hasn't reset yet.

2 % 4 == 2

So to recap, the first number is the number of steps to take, the second number is the size of the clock.