You are here:

Advanced Math/Modulo Function

Advertisement


Question
QUESTION: I am experimenting with abstract math (for fun, not for school), and I am curious about the modulo operator (%) that is commonly used in programming.  Is there some kind of more elementary function that makes it up, or is it a purely arbitrary construction?  I guess another way to express it is this:  If I want to graph (for example) y = x % 3 but my calculator doesn’t have a modulus operator, is there some way I can get the same result using some combination of the other available function operators? (+ - × ÷ log exp sin etc.)

ANSWER: The modulus operation gives the remainder. Your example of y = x % 3 means that y is the remainder when you divide x by 3.

So 14 mod 6 is 2, because 14/2 is 2 with remainder 2., i.e. 14 = 2*6 + 2.

There are two ways to do this using the standard functions.

First, you can divide and then multiply:

You would type 14/6 to get 2.33333.

Then subtract away the integer part to get 0.333333.

Then multiply back by 6 to get 2. That's the remainder.


The other method is subtracting 6 repeatedly: 14 - 6 = 8. 8 - 6 = 2. You stop when you get a number less than 6 (so it is 2 again).

---------- FOLLOW-UP ----------

QUESTION: Thank you.  I get the part about doing it with specific numbers such as 14 and 6, but is there a general way to do it with variables such as x and y?  In the case of 14 mod 6, it’s visibly obvious how much of an integer to subtract (2) before multiplying by 6 to obtain a remainder of 2.  But in the case of y = x mod c, how do you know how much of an integer to subtract from x ÷ c before multiplying by c to obtain the remainder?

Answer
If you want to compute y = x % m, you are trying to divide x by m.

You can do x/m, then subtract away the whole-number-part (like 2.3333 becomes 0.3333), then multiply by m again.

You can subtract m from x repeatedly until you get a number between 0 and m-1.

Advanced Math

All Answers


Answers by Expert:


Ask Experts

Volunteer


Clyde Oliver

Expertise

I can answer all questions up to, and including, graduate level mathematics. I am more likely to prefer questions beyond the level of calculus. I can answer any questions, from basic elementary number theory like how to prove the first three digits of powers of 2 repeat (they do, with period 100, starting at 8), all the way to advanced mathematics like proving Egorov's theorem or finding phase transitions in random networks.

Experience

I am a PhD educated mathematician working in research at a major university.

Organizations
AMS

Publications
Various research journals of mathematics. Various talks & presentations (some short, some long), about either interesting classical material or about research work.

Education/Credentials
BA mathematics & physics, PhD mathematics from a top 20 US school.

Awards and Honors
Various honors related to grades, various fellowships & scholarships, awards for contributions to mathematics and education at my schools, etc.

Past/Present Clients
In the past, and as my career progresses, I have worked and continue to work as an educator and mentor to students of varying age levels, skill levels, and educational levels.

©2016 About.com. All rights reserved.