# Is it possible to do modulo of a fraction

I am trying to figure out how to take the modulo of a fraction.

For example: 1/2 mod 3.

When I type it in google calculator I get 1/2. Can anyone explain to me how to do the calculation?

• You can interpret that as the modular inverse of $2$ modulo $3$ (which is $2$). Is that what you intend? Jul 11, 2014 at 20:08
• I would not write $1/2$ to denote the inverse of $2$; instead I would write $2^{-1}$. Jul 11, 2014 at 20:25
• I just now saw what a modular inverse is. Thanks! Jul 11, 2014 at 20:29
• A similar question with a good answer: Finding modular of a fraction Sep 15, 2018 at 12:19

One can perform arithmetic of fractions mod $$\,m\,$$ as long as all fractions $$\,a/b\,$$ have denominator $$\,b\,$$ coprime to $$\,m,\,$$ since then, by Bezout, $$\,b\,$$ is invertible mod $$\,m\,$$ so the fraction has the unique denotation $$\,x = a/b = ab^{-1}$$ (the unique solution of $$\,bx = a).\,$$ The usual rules of fraction arithmetic remain true (as long as one restricts to such fractions), e.g. from a prior answer:

Hint $$\,\ {\rm mod}\ 13\!:\ \dfrac{41}7 \equiv \dfrac{28}7 \equiv 4\ \$$ by $$\ \ 41\equiv 41\!-\!13 \equiv 28$$

Alternatively $$\,\ \dfrac{41}{7}\equiv\dfrac{(-2)(-1)}{-6}\equiv \dfrac{-2}{-2}\dfrac{12}3\equiv 4\ \$$ by $$\ \ \begin{eqnarray}41&&\!\!\equiv\ \ 2\\ 7 &&\!\!\equiv -6\end{eqnarray}$$

Alternatively $$\,\ \dfrac{41}{7}\equiv \dfrac{2}7\equiv \dfrac{4}{14}\equiv \dfrac{4}1\$$ by Gauss's Algorithm.

Such twiddling (adding/subtracting the modulus from numerator or denominator till things divide or factor nicely) works quite well for small numbers. For larger numbers one can invert the denominator by the Extended Euclidean Algorithm, or Gauss's algorithm if the modulus is prime.

Beware $$\$$ The use of fractions in modular arithmetic is valid only when the denominator is invertible, i.e. coprime to the modulus. Otherwise the quotient need not be unique, for example mod $$\rm\:10,\:$$ $$\rm\:4\,x\equiv 2\:$$ has solutions $$\rm\:x\equiv 3,8,\:$$ so the "fraction" $$\rm\:x \equiv 2/4\pmod{10}\,$$ cannot designate a unique solution of $$\,4x\equiv 2.\,$$ Indeed, the solution is $$\rm\:x\equiv 1/2\equiv 3\pmod 5,\,$$ which requires canceling $$\,2\,$$ everywhere, i.e. from the modulus too, since $$\rm\:10\:|\:4x-2\iff5\:|\:2x-1.\:$$

Generally the grade-school rules of fraction arithmetic apply universally (i.e. in all rings) where the denominators are invertible. This fundamental property will be clarified conceptually when one learns in university algebra about the universal properties of fractions rings and localizations.

• I'm assuming you had in mind $\frac{28}{7}\equiv 4$, not $\frac{28}{7}=4$, since of course $4\mod 13\neq 4$. Jan 29, 2015 at 0:26
• Thanks for the answer. In the question, it is 1/2 % 3. Following what you said, 3: 1/2 => 2/4 => 2. So 2 is the remainder. But when we do the actual division process, the remainder is 1/2(not 2) for a quotient 0. Right? How 2 is the answer? May 29, 2017 at 18:48
• @user3219492 What is "the actual division process" that you refer to? May 29, 2017 at 18:55
• Divident = quotient*divisor + remainder is what I call as actual division process. Here Divident is 0.5, quotient is 0, divisor is 3 and remainder is 0.5 May 29, 2017 at 18:59
• (Euclidean) division with remainder is normally defined only for integers, but you appear to be attempting to (mis)apply it to fractions (or reals). The meaning of modular fractions is explained in my answer. May 29, 2017 at 19:03

One natural way to define the modular function is $$a \pmod b = a-b\left\lfloor \frac{a}{b}\right\rfloor$$ where $\lfloor \cdot \rfloor$ denotes the floor function. This is the approach used in the influential book Concrete Mathematics by Graham, Knuth, Patashnik.

This will give you $1/2 \pmod 3 = 1/2$.

My favorite way to find inverse modulo $n$ (if they exist) is from the Euclidean algorithm since that's the standard way of proving inverses exist. By the EA you can find integers $x,y\in\Bbb Z$ such that

$$ax+by=c\tag{*}$$

with $c=(a,b)$. Then if we want an inverse for $a\mod b$ we check $(a,b)=1$ using the Euclidean algorithm, naturally, and as a by-product we get the inverse is the $x$ from the expression $(*)$ comes along for free.

• Thanks, @Hakim! I didn't realize you could right align things like that for the star tag! Jul 11, 2014 at 21:04
• You're welcome @Adam ;-) Jul 11, 2014 at 21:13

Think of a set of numbers {0, 1, 2, 3, 4, 5, 6}. Z7 (or modulo 7) Such that we have two operations + and *. If we add two numbers, the result never escapes the set. If we multiply two numbers the result still does not escape the set.

For Example: 3 + 4 = 7, but 7 mod 7 = 0, thus 3 + 4 mod 7 = 0 mod 7, and 0 is an element of the set. 3 * 4 mod 7 = 12 mod 7 = 5 mod 7, and 5 is an element of the set.

Now 1/2 means the inverse of 2 (in the multiplicative sense) This means that 2 times what number gives yields 1 or remainder 1? ( think of the normal integers. 5 * 1/5 = 1 )

1/2 mod 7 is the same as 4 mod 7 because 2 * 4 mod 7 = 8 mod 7 = 1 mod 7. So we do not think of the 1/2 as a "fraction" in the traditional sense, but as the multiplicative inverse within the modulo set that yields a remainder of 1.

It is important to note that a multiplicative inverse exits in Zm for an element a, only if the gcd(a,m) = 1. In other words the two numbers are relatively prime.

For example.

Consider Z6

{0, 1, 2, 3, 4, 5}

3 has no inverse in this set. the gcd(3, 6) = 3. If you multiply 3 by any other element in the set and take the modulos you never get 1.

I think it's more typical for computers to consider the mod operation as meaning "add/subtract three a bunch until the number gets near zero, then output". This would explain why Google gives you $\frac12 \text{mod } 3 = \frac12$.

But I agree with Daniel and 162520 that the 'right' answer is $2$.

By that I mean: If there was a theorem that said "Consider $\frac1n \text{mod } p$" and I wanted to look at the $n=2$ and $p=3$ case, then without further context I would assume that $2^{-1}=2$ was what was intended.

Finding the inverse of p^n mod q where p,q are relatively-prime primes using Fermat’s little equation, p^(-n) mod q = p^x mod q where x = (-n) mod (q-1). For example, 3^(-1) mod 5 = 3^[(5-1)-1] mod 5 = 3^3 mod 5 = 2. Another example, 7^(-1) mod 13 = 7^11 mod 13 = 2 (done by calculator). The formula is useful when one is working algebraically, rather than with specific numbers.