Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solve mod equation in JS

I need to find an x value that satisfied:

x * a % m == b
x * c % n == d

Is there a smarter way to find the solution without iterating all the possibilites ?

function solve()
{
    for (x=0;x<n*m-1;x++)
    {
        if((x * a % m == b) && (x * c % n == d))
             return true;
    }
    return false;
}
like image 473
Blair d Avatar asked Feb 21 '26 12:02

Blair d


1 Answers

As there is no direct correlation between these two equations (excepted the unkown X), I don't think that you can solve it in a really clever way. However, perhaps should you give a look to https://en.wikipedia.org/wiki/Chinese_remainder_theorem. This will not provide an answer to the topic itself, but could lead you to find some alternatives.

like image 173
MedAl Avatar answered Feb 23 '26 02:02

MedAl