# How to solve modulo equation for divisor?

How would I solve an equation of this form?

$3 \% x = 1$

I can see that 2 is a valid solution for x and I think is the only answer.

This is different though:

$3 \% x = 3$

The answer is any integer greater than 3.

And this one doesn't seem to have a valid solution:

$3 \% x = 2$

Is there some standard process for solving equations like this?

I'm ultimately trying to solve nested modulos like the below, but can ask a second question if its out of scope!

$(3 \% x) \% 2 = 1$

Thanks!

Edit: I can solve congruences in the form of $x\%5=2$, which I know the answer to be $x\equiv 2+5Z$. I can also solve nested congruences like this: $((x\%7)\%5)\%2=1$. I also know about modulus multiplicative inversion, the chinese remainder theorem and the extended euclidean algorithm. However, I don't know any techniques to solve the form in the question I've asked.

• See here on Math StackExchange itself. – Shailesh Sep 15 '15 at 2:56
• Maybe I'm missing it, but I don't see how the info on that page relates to this problem, other than perhaps the name of some books that might have related info. – Alan Wolfe Sep 16 '15 at 4:05
• I can solve congruence in the form of $x\%5=2$, which I know the answer to be $x \equiv 2+5\mathbb{Z}$. I can also solve nested congruences like this: $((x \% 7) \% 5) \%2 = 1$. I also know about modulus multiplicative inversion, the chinese remainder theorem and the extended euclidean algorithm. However, I don't know any techniques to solve the form in the question I've asked, where the divisor is the variable. Any tips on that? The page you link to doesn't explain anything regarding that, at least that I can see. – Alan Wolfe Sep 16 '15 at 4:10
• Ok. Give me a few hours. I'll post it. Right now I'm posting thru cell phone – Shailesh Sep 16 '15 at 4:11
• aw, thank you for your help! – Alan Wolfe Sep 16 '15 at 4:12

## 1 Answer

Outline

Using your notation, a%x = b is same as $x|(b-a)$. So 3%x = 1 is same as $x|2$ so clearly you got $x = 2$ as a solution. Now you can extend the concept for nested %s. In the specific question, it is easy to see that 3%x is odd. You can expand from there.

• do you mean $x|(a-b)$? $3\%x=1$ using b-a becomes $x|-2$ not $x|2$. Assuming a-b is correct, check out this case $3\%x=2$. That becomes $x|1$, which would suggest that $3\%1=2$, which isn't correct. Also check this case out: $8\%x=0$. That becomes $x|8$ and by your logic if i'm following it correctly, would mean x = 8, but x could also be 4 or 2. What do you think? Thanks for the help so far Shailesh! – Alan Wolfe Sep 16 '15 at 14:29
• @AlanWolfe (1) $x|2$ or $x|(-2)$ should not matter - they can be considered equivalent. (2). Consider the meaning of 3%x = 2. If x > 3, how can 3 and 2 be valid remainders when divided by x ? Then x = 3, x = 2 will also not work. Therefore no solution (3) When $x|8$, then obviously 2, 4 and 8 divide 8, so they are all solutions. Hope this helps. – Shailesh Sep 16 '15 at 15:36
• OK sounds good. I think I understand now. Thank you! – Alan Wolfe Sep 16 '15 at 15:46
• The answer seems to be: when you have $a \% x = b$, that is the same as $x | (b-a)$ where $x > b$. You might end up with a finite number of results as an answer, an infinite number of results, or an empty set (no solution). All "edge cases" seem to be covered though. – Alan Wolfe Sep 16 '15 at 17:42