 # Modulus; test and forum inquiry

I don’t know what a modulus is really even after reading the reassuring text accompanying the homework:
"If you aren’t familiar with modular arithmetic, it is pretty straightforward- the modulus operator,
in the expression x mod y, gives the remainder when x is divided by y."
I typed in “6%4” and got “2” but I have no idea if that is right or why.
This thread may be a place for those to share their attempts to clarify their understanding and share their (possibly newfound) knowledge.

If we had a pile of `33` pennies and wanted to split it into `5` equal sized piles, we would have enough pennies to create `5` piles with `6` pennies each. We would not have enough for all `5` piles to have `7` pennies, so we settle for `6` pennies in each. With `5` piles of `6` pennies each, we have used `30` of the `33` pennies. Three pennies are left over, and that is the remainder.

For non-negative numbers, the modulus operator gives the remainder from dividing the first operand by the second operand. In the case of the `33` pennies divided into `5` equal sized piles, the math would be expressed as follows:

``````>>> 33 // 5
6
>>> 33 % 5
3
``````

The first of the two operations above is integer division. The result was `6` piles.

The second lets us know how many were left over. `3` pennies are left over.

With `6 % 4`, the `4` can be divided into `6` once. `2` is left over. So:

``````>>> 6 % 4
2
``````

When the first operand is evenly divisible by the second, nothing is left over, so the modulus would be `0`. So:

``````>>> 8 % 4
0
``````

It might not be so obvious how to handle negative numbers, but here is an example.

``````>>> -5 % 4
3
``````

Basically, the above is giving us the difference between `-5` and the next lower number, `-8`, that is evenly divisible by `4`.

1 Like

I’m not quite sure if you’re asking for help or providing a place for people to ask for help, but either way, a short explanation may help. Let’s start at division. From there we will go to modulus.

Example: 19/6

Whenever we do division, we divide the dividend (19) by the divisor (6), receive the quotient (3) and are left with the remainder (1).

The difference between division and modulus is that division gives the quotient, and modulus gives the remainder.

As glenn said, it is just the remainder after you have divided the two numbers. If you had 6%4:

4 goes into 6 one time, with 2 left over. So 2 is the remainder.

19%6:

6 goes into 19 three times, with one left over, so 19%6 is 1.

1 Like

Thank you for your responses. Yes, it is a pretty simple concept. However, though I have been able to handle the problems involving the modulus so far I have now run into a difficulty with the Codecademy “Conditionals and Control Flow” excercise 8of15 #3.
I am asked to evaluate the following:
not 10 % 3 is <= 10 % 2
I entered the value “False”. To my understanding, the larger the number the smaller the remainder. Therefore, the value which returns the remainder of 3 must be less than the value which returns 2. However, my response was evaluated as incorrect.
I ran the equasions through my Python interpreter and I got the following results:
10 % 3 = 1
10 % 2 = 0

That definitely explains why my answer was evaluated as incorrect.

Now, I know this isn’t remedial math class but hopefully someone can answer me this:
if I solve for 10/x=7.3 I get .73
if I solve for 10/x=8.2 I get .82

Shouldn’t both of those be rounded down by Python and therefore be “equal” in the eyes of the interpreter? Does this have to do with the version of Python I am using (2.7.8)?

hey @Chiboli

Set `bool_three` equal to the result of `not 10 % 3 <= 10 % 2`

You are right that `10 % 3 <= 10 % 2` would evaluate to `False`, but what happens if you put `not` in front of it?

if I solve for 10/x=7.3 I get .73

If I solve for x, I get

10/x=7.3
10 = 7.3x
x = 10/7.3
x = 1.36986301369863

In python 2.7.8 (or any 2. version) division by an integer will give you integer division when using a single forward slash (`/`) and and both numbers are integers: `7/2 == 3`. If one of the number are floating point, you will get floating point division: `7.0/2 == 7/2.0 == 3.5`

Be sure to note what @dirk asked about the `not` operator.

“… what happens if you put `not` in front of it? …”

Here’s some interaction with the Python 2 interpreter in IDLE:

``````Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
>> not True
False
>>> not False
True
>>> 10 % 3 <= 10 % 2
False
>>> not 10 % 3 <= 10 % 2
True
>>>``````

@Glenn thanks for your clear explanation! I was doing some work on another course, and while I understand how modulus works with non-negative numbers, I couldn’t figure out what was happening with negative numbers… until now.

I should have paid better attention in math class 30 years ago. 1 Like

@bkglass - As a matter of fact, I was focused more on the clock than on the teacher in math class - but no problem! - the clock is an expert in modular arithmetic. They both cycle repeatedly through the same series of integers, and the treatment of negative numerators supports that cycling.

Of the three hands on the clock in my math class, the minute hand was the most interesting. Time, on a scale measured in minutes, provided the best sense how long we had been sitting in class, and how soon class would end. On the clock, the minutes cycle from 0 past the hour, to 59, continuing around to 0 again as the next hour begins.

Any integer modulo `60` yields an integer `0` through `59`. Whether we gradually increment a numerator or decrement it, the result of that numerator modulo `60` proceeds through the same cycle uninterrupted, even if that numerator goes negative.

So, if provided with a computer in my math class, I could have `while`d away the time with some programming …

``````# MinuteHandInInMathClass.py
print "Minutes in Math Class"
minutes_till_class_ends = -1
while minutes_till_class_ends:
print "Zzzzzzz .... \n"
minute_hand_now =         int(raw_input("Where is the minute hand pointing (0 - 59)? "))
minutes_ago_class_began = int(raw_input(" How many minutes ago did math class begin? "))
minutes_till_class_ends = int(raw_input("How many minutes until math class will end? ")) # enter 0 to exit
print "Class commenced at %s minutes past an hour." % ((minute_hand_now - minutes_ago_class_began) % 60)
print "Class dismisses at %s minutes past an hour." % ((minute_hand_now + minutes_till_class_ends) % 60)
else:
print "Class is now dismissed!"
``````
1 Like

@Glenn your ‘clock’ code is a good example that works the same and is the idea behind the rock, paper, scissors, lizard, Spock project I was just working on from another course I am currently following. Using modular arithmetic really cuts the amount of code needed in that project.

Thanks again for taking the time to explain this!