2

Suppose we have two numbers A and B. We need to find a multiple of A such the A % B is maximized One such solution can be to traverse B multiples of A? is there any other soltuion?(In case B is huge)

Suppose A=3 and B=10. then 10 multiples of 3 are 3,6,9.......30.

Max remainder will be 9%10=9.

Suppose A=14 and B=11 then 11 multiples of 14 are 14,28..........140,154.

Then max remainder 98%11=10 .

Is there any more effective way to find this?

1 Answers1

0

Let

$$d = \gcd(A,B) \tag{1}\label{eq1}$$

The maximum value is $B - d$. To see this, note that any value of $A%B$ is a multiple of $d$, so $B - d$ is the maximum possible value and, in a question I recently answered at What is maximum value of $a b\mod c$ , where $a$ and $c$ are known., I explain in some detail why this maximum will be achieved.

In your examples, you get a maximum of $B - 1$ because $d = 1$. However, if you try $A = 4$ and $B = 10$, you can see that $d = 2$ and the maximum is $10 - 2 = 8$ instead, where you use $2$ times $A$.

John Omielan
  • 52,653