The Student Room Group

Linear Congruence Equation

If I have an equation of the form An + B X mod 2X,
where A, B and X are all coprime to each other. X is a power of 2 if that makes any difference.

Is there a simple formula I can use to calculate the first solution? First solution meaning in the range [0, 2X]


I've been googling modular arithmetic, linear congruence, even tried Chinese Remainder Theorem for the last hour and keep going nowhere.

Am I missing something obvious or is there no neat solution?
(edited 7 years ago)
Original post by bencrossley1
If I have an equation of the form An + B X mod 2X,
where A, B and X are all coprime to each other. X is a power of 2 if that makes any difference.

Is there a simple formula I can use to calculate the first solution? First solution meaning in the range [0, 2X]


I've been googling modular arithmetic, linear congruence, even tried Chinese Remainder Theorem for the last hour and keep going nowhere.

Am I missing something obvious or is there no neat solution?


It's quite simple. Let's do an example, 3n + 5 = 4 (mod 8).
Notice that any number that is congruent to 4 (mod 8) can be written as 8m + 4 for some m.
Hence 3n + 5 = 8m + 4 -> 8m - 3n = 1. We can solve this Diophantine equation either using the Euclidean algorithm or just trial and error, to get m = 2 and n = 5 as an initial solution. Thus the "first solution" for n is 5.

The procedure is, of course, the same regardless of the values of A, B, and X.
Original post by HapaxOromenon3
It's quite simple. Let's do an example, 3n + 5 = 4 (mod 8).
Notice that any number that is congruent to 4 (mod 8) can be written as 8m + 4 for some m.
Hence 3n + 5 = 8m + 4 -> 8m - 3n = 1. We can solve this Diophantine equation either using the Euclidean algorithm or just trial and error, to get m = 2 and n = 5 as an initial solution. Thus the "first solution" for n is 5.

The procedure is, of course, the same regardless of the values of A, B, and X.


I don't wish to use the Euclidean algorithm or trial and error. I am after a deterministic formula. Thanks for putting in the time though!

I'm doing some work which would be much faster given a formula, For example:
n=7 solves 2187n + 17459 32768 mod 65536
therefore
n=7 solves 6561n + 85145 131072 mod 262144

The numbers I intend to work with are only increasing. So T&E is a complete no go, and I dislike algorithms. Or at least, for what I am doing they will not be of use.
(edited 7 years ago)
Original post by bencrossley1
I don't wish to use the Euclidean algorithm or trial and error. I am after a deterministic formula. Thanks for putting in the time though!

I'm doing some work which would be much faster given a formula, For example:
n=7 solves 2187n + 17459 32768 mod 65536
therefore
n=7 solves 6561n + 85145 131072 mod 262144

The numbers I intend to work with are only increasing. So T&E is a complete no go, and I dislike algorithms. Or at least, for what I am doing they will not be of use.


If you don't want to use the Euclidean Algorithm, which is the only thing that can help you, simply for ideological reasons, then you are simply an idiot. There is no explicit formula for solving the type of equation that you have described.
Original post by HapaxOromenon3
If you don't want to use the Euclidean Algorithm, which is the only thing that can help you, simply for ideological reasons, then you are simply an idiot. There is no explicit formula for solving the type of equation that you have described.


Calling me an idiot is unwarranted.

Given the general expression An + B C mod D

yes I would agree, no deterministic solution.

Given the added criteria that A = 3^a, B is not divisible by 2 or 3, C = 2^c and D = 2^c+1 it is possible that under these conditions that a formula may exist.

I already have a formula for a=0 and a=1. By inspection I expect to be able to generate further formulas and eventually a general one for any a.
Original post by bencrossley1
Calling me an idiot is unwarranted.

Given the general expression An + B C mod D

yes I would agree, no deterministic solution.

Given the added criteria that A = 3^a, B is not divisible by 2 or 3, C = 2^c and D = 2^c+1 it is possible that under these conditions that a formula may exist.

I already have a formula for a=0 and a=1. By inspection I expect to be able to generate further formulas and eventually a general one for any a.


Lol.


Posted from TSR Mobile

Quick Reply

Latest