Modulo operator

1.7k views Asked by At

9%4 = 1

1%4 and the answer is 1

2%4 and the answer is 2

Answer found. - CLOSED.

4

There are 4 answers

0
Cacho Santa On BEST ANSWER

The meaning of Mod is that you take the remainder after doing the division.

1 fits zero times in 4, so the remainder is 1.

Here the wikipedia definition that explains in a little more detail:

In mathematics the result of the modulo operation is the remainder of an arithmetic division. As is well known, an arithmetic division of two integers produces a quotient and a remainder.

0
Praveen Kumar Purushothaman On

The modulo operator gives the remainder of the division. Other than 1, anything divides 1 gives you 1 as the remainder.

i.e.,

  ______
4 | 1  ( 0
    0
  ------
    1

4 goes 0 times in 1. So putting a 0 in the division, will give you 1 as the remainder.

0
hakki On

First see this answer: https://stackoverflow.com/a/17525046/1848929 then

Link says

The "mod" operator in computer languages is simply the remainder. For example,

17 mod 3 = 2

because

17 / 3 = 5 rem 2

which in turn means

17 = 3 * 5 + 2

There are some tricky issues when negative numbers are used, but that shouldn't ordinarily be necessary.

In math (number theory), the term is used a little differently. The "modulus" is actually not the remainder, but the number you are dividing by; and "mod" is not an operator, but a label telling "in what sense two quantities are considered congruent, or equal." For example, we would say

17 = 11 (mod 3)

(read as "17 is congruent to 11, modulo 3"), meaning that 17 and 11 both leave the SAME remainder when divided by 3. You probably won't see this usage if you are only reading about programming, but it's worth being aware of if you look deeper into the math behind it.

Here are three references in archives:

Modulus Operator Problem Link

Mod Link

Mod Function and Negative Numbers Link

Deeper: http://mathworld.wolfram.com/Congruence.html

If two numbers b and c have the property that their difference b-c is integrally divisible by a number m (i.e., (b-c)/m is an integer), then b and c are said to be "congruent modulo m." The number m is called the modulus, and the statement "b is congruent to c (modulo m)" is written mathematically as

b=c (mod m). (1) If b-c is not integrally divisible by m, then it is said that "b is not congruent to c (modulo m)," which is written

b≢c (mod m). (2) The explicit "(mod m)" is sometimes omitted when the modulus m is understood by context, so in such cases, care must be taken not to confuse the symbol = with the equivalence sign.

The quantity b is sometimes called the "base," and the quantity c is called the residue or remainder. There are several types of residues. The common residue defined to be nonnegative and smaller than m, while the minimal residue is c or c-m, whichever is smaller in absolute value.

CongruenceClockMinutes Congruence arithmetic is perhaps most familiar as a generalization of the arithmetic of the clock. Since there are 60 minutes in an hour, "minute arithmetic" uses a modulus of m=60. If one starts at 40 minutes past the hour and then waits another 35 minutes, 40+35=15 (mod 60), so the current time would be 15 minutes past the (next) hour.

CongruenceClockHours Similarly, "hour arithmetic" on a 12-hour clock uses a modulus of m=12, so 10 o'clock (a.m.) plus five hours gives 10+5=3 (mod 12), or 3 o'clock (p.m.)

Congruences satisfy a number of important properties, and are extremely useful in many areas of number theory. Using congruences, simple divisibility tests to check whether a given number is divisible by another number can sometimes be derived. For example, if the sum of a number's digits is divisible by 3 (9), then the original number is divisible by 3 (9).

Congruences also have their limitations. For example, if a=b and c=d (mod n), then it follows that a^x=b^x, but usually not that x^c=x^d or a^c=b^d. In addition, by "rolling over," congruences discard absolute information. For example, knowing the number of minutes past the hour is useful, but knowing the hour the minutes are past is often more useful still.

Let a=a^' (mod m) and b=b^' (mod m), then important properties of congruences include the following, where => means "implies":

  1. Equivalence: a=b (mod 0)=>a=b (which can be regarded as a definition).

  2. Determination: either a=b (mod m) or a≢b (mod m).

  3. Reflexivity: a=a (mod m).

  4. Symmetry: a=b (mod m)=>b=a (mod m).

  5. Transitivity: a=b (mod m) and b=c (mod m)=>a=c (mod m).

  6. a+b=a^'+b^' (mod m).

  7. a-b=a^'-b^' (mod m).

  8. ab=a^'b^' (mod m).

  9. a=b (mod m)=>ka=kb (mod m).

  10. a=b (mod m)=>a^n=b^n (mod m).

  11. a=b (mod m_1) and a=b (mod m_2)=>a=b (mod [m_1,m_2]), where [m_1,m_2] is the least common multiple.

  12. ak=bk (mod m)=>a=b (mod m/((k,m))), where (k,m) is the greatest common divisor.

  13. If a=b (mod m), then P(a)=P(b) (mod m), for P(x) a polynomial.

Properties (6-8) can be proved simply by defining

a = a^'+rm (3) b = b^'+sm, (4) where r and s are integers. Then

a+b = a^'+b^'+(r+s)m (5) a-b = a^'-b^'+(r-s)m (6) ab = a^'b^'+(a^'s+b^'r+rsm)m, (7) so the properties are true.

Congruences also apply to fractions. For example, note that

2×4=1 3×3=2 6×6=1 (mod 7), (8) so

1/2=4 1/4=2 2/3=3 1/6=6 (mod 7). (9) To find p/q (mod m) where (q,m)=1 (i.e., q and m are relatively prime), use an algorithm similar to the greedy algorithm. Let q_0=q and find

p_0=[m/(q_0)], (10) where [x] is the ceiling function, then compute

q_1=q_0p_0 (mod m). (11) Iterate until q_n=1, then

p/q=pproduct_(i=0)^(n-1)p_i (mod m). (12) This method always works for m prime, and sometimes even for m composite. However, for a composite m, the method can fail by reaching 0 (Conway and Guy 1996).

Finding a fractional congruence is equivalent to solving a corresponding linear congruence equation

ax=b (mod m). (13) A fractional congruence of a unit fraction is known as a modular inverse. A fractional congruence can be found in the Wolfram Language using the following function:

FractionalMod[r_Rational, m_Integer] := Mod[ Numerator[r]PowerMod[Denominator[r], -1, m], m] or using the undocumented syntax PolynomialMod[r, m] for r an explicit rational number.

0
Ilario On

The modulo operator gives you the remainder of the integer division. The integer result of 1/4 is 0, not 0.25.
So 1%4 = 1 - (1/4)*4 = 1 - 0*4 = 1 - 0 = 1 (and in general n%m = n - (n/m)*m)