If a is coprime in M, then a * k = a * k 'mod M, if and only if k = k' mod M, so you can also use a = 1, up to M. This also covers all cases, when M is prime, since all numbers except 0 are then coprime to M.
If a and M are not compatible, then they have a common coefficient, say b, so a = x * b and M = y * b. In this case, everything that is multiplied by a will also be divisible by b mod M, and you can also work with mod y, not mod M, so nothing will work if you do not use it with M.
So, for this problem, you can save some time by leaving a = 1 and trying all possible values ββof M.
If you, for example, using 32-bit integers and really computing not (a * k) mod M, but ((a * k) mod 2 ^ 32) mod M, you could find cases where values ββother than 1, better than a = 1 due to what happens in (a * k) mod 2 ^ 32.
mcdowella
source share