0

Given equation :

K = Ap + Bq + Cr + Ds.

Solution I tried :

Terms we already know : A, B, C, D, K

Finding term s given p, q, r;

p=0, q=0, r=1;
compute() => s = (K - Ap - Bq - Cr)/D;

continue until s becomes < 0; for all terms p=0, q=0, r=1...n;

like wise continue until s becomes < 0; for all terms where p=0, q=1..n, r=1...n;

and,

finally, continue until s becomes < 0; for all terms where p=1..n, q=1..n, and r=1..n;

coded 3 loops for updating p, q and r.

It takes more time to compute if K becomes larger for example 1000..., 8145, 45000 etc..;

Please do not suggest external library... I am looking for a coding solution.

Example Snippet

for (int i = 0; i < preSpecifiedIterations; i++)
{
 p = i;

 if (A * p > K)  //Exit condition ; if Ap > K
  break;

  for (int j = 0; j < preSpecifiedIterations; j++)
  {
   q = j;

   if (B * q > K) //Exit condition ; if Bq > K
    break;

   for (int k = 1; k < preSpecifiedIterations; k++)
   {
    r = k;
    // compute s given p, q, and r;
    s = (K - (A * p) - (B * q) - (C * r)) / D;

    if (s < 0) //Exit condition ; don't process if s < 0 i.e negative values
     break;
    else
     ...
   }
  }
}

Also, if one has noted : preSpecifiedIterations -> Is it possible to determine how many iterations would be required ahead of computation?

And is there any better Algorithm to solve above problem?

Thanks a lot of reading.

  • 1
    No one can tell you what the *fastest possible way* is because that will depend on many factors. A better approach is to say "I need to find the solution in less than fifty milliseconds on a 4 GHz 64 bit Windows machine", or whatever your time budget and hardware is. – Eric Lippert Apr 17 '13 at 15:35
  • Thanks for replying.. I am concern with how quickly can we get the solution i.e Algorithm optimization... like it is possible to have 3 to 6 threads.. working on different values of p (p=0, q=1..n and r=1..n) on one thread then p=1, q=1..n and r=1..n on other thread.. like wise.. but if we have a better algorithm those many iterations might not be required for any given value of K. to update when K is set to 50,000 it is taking around 10 min to complete. – user2290809 Apr 17 '13 at 16:49
  • So just to be clear, you are trying to solve a *Diophantine equation* yes? Humans have been studying Diophantine equations for 2300 years now and there is a *considerable* body of literature on them. More than can be summed up here. – Eric Lippert Apr 17 '13 at 17:12
  • thanks again.. and thanks for pointing to it.. yes it is Diophantine equation but includes more number of variables. – user2290809 Apr 18 '13 at 12:10

1 Answers1

4

This is not a very good question for StackOverflow; it's better for one of the math sites. But I'll answer it here anyways.

You certainly can do better than brute force, as you are doing here. You should be able to compute the answer in a few microseconds.

There exists a solution if and only if the greatest common divisor of (A, B, C, D) divides K evenly. That's the extended form of Bézout's identity.

In order to determine (1) what the gcd is and (2) what the values for p, q, r, s are, you use the Extended Euclidean Algorithm, which you can read about here:

http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm

My advice is to first write a program that solves simple linear Diophantine equations of the form ax + by = c. Once you've done that, then read the section of that Wikipedia article called "The case of more than two numbers", which describes how to extend the algorithm to handle your case.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Thanks for providing these details. Have a small question, Variables p, q, r, or s. should strictly be positive. How do I get only positive number using the equation. – user2290809 Apr 23 '13 at 15:48
  • @user2290809: You should probably ask at math.stackexchange.com. It has been well over fifteen years since I had to solve these sorts of problems and I am rusty. (Note that math.stackexchange.com is for math questions at any level; mathoverflow.net is for research-level math questions.) – Eric Lippert Apr 23 '13 at 16:06
  • 1
    I apologies.. for posting it here... I did ask in math.stackexchange.com here is the link "http://math.stackexchange.com/questions/366469/find-all-positive-integers-for-a-given-diophantine-equation-involving-4-or-7-var" but did not get any reply. I apologies again.. and Thank your very much for responding. – user2290809 Apr 24 '13 at 06:18