0

I am working on a problem in Number Theory which requires that I solve a rather complicated Diophantine equation. Let's call this equation f(r1, r2, ..., rk). The number of variables in the equation is itself variable. This is where I am getting tripped up on the programming.

I wish to write a java method whose signature would look like this :

int[] getExponents( int n, int k, int max );

Here, the argument k is equal to the number of arguments in our Diophantine equation f(r1, ... , rk).

This method should evaluate f(r1, ... , rk) for all combinations of r1, ..., rk such that 0 < r1 < r2 < ... < rk < max, where max is the agrument given in our method signature.

If we find r 's such that n = f(r1, ... , rk) then we wish to return the r1, ... , rk as an integer array. (The value n is given in our method signature.)

I suspect that this method will use recursion. Unfortunately, either my programming skills or my patience are not strong enough to find it.

I would be grateful to anyone able to outline such a method for me.

userZero
  • 111
  • 2

2 Answers2

1

The basic function might look something like this, assuming f(int r[]) is the signature for a function evaluating your equation.

  int[] getExponents( int n, int k, int max ){
    int[] result = new int[k];
    for(int i=0; i<k; i++)
      result[i]=i+1;
    do{
      if(f(result)==n)
        return result;
    }while(updateExponents(result,max));          
    return null;
  }

Then you need a function boolean updateExponents(result,max) that iterates through increasing sequences of integers between 0 and max. Something like this:

  private boolean updateExponents(int[] result, int max) {
    int k = result.length;
    for(int i=k-1; i>=0; i--){
      if(result[i]< max-(k-i))
      {
        result[i]++;
        for(int j=i+1; j<k; k++)
          result[j]=result[j-1]+1;
        return true;
      }
    }
    return false;
  }

Disclaimer: this code probably contains bugs, I haven't tested it or even run it, but it should be a good starting point at least.

mrip
  • 14,913
  • 4
  • 40
  • 58
  • Thank-you for your clear and precise answer. You obviously read and understood my question. I see that my question has been put on hold. I'll try working with your code. Thanks again. – userZero Oct 01 '13 at 22:09
0

Here's how to think about handling the recursion. (It's not particular to Diophantine equations; this will work for other cases where you need to find all such sequences of integers.)

The problem is to find all sequences of k integers r such that 0 < r1 < r2 < ... < rk < max.

Well, start by picking r1. The possibilities are 1, 2, ..., max - 1 (actually you can stop before max - 1. You're going to go through in a loop and try each one.

For each r1, you've reduced it to a new problem: find all sequences of k-1 integers r2, r3, ..., rk, such that r1 [fixed] < r2 < r3 < ... < rk < max. Does this look a lot like the original problem? Well, there's your recursion. The only thing different is that the lower bound is something else besides 0. So your recursive routine will need a lower bound parameter (which will be 0 the first time it's called), and the k parameter, which will be k the first time it's called, then k-1, k-2, etc.

Now you just have to figure out what to do when you get to the bottom. If k is 1, you don't need to call the recursive routine again. You just need to do something with all the numbers you've collected. So for that purpose, you'll need to maintain an array of integers. This array will start out empty. But when the first level of recursion finds an integer r1, you'll add r1 to the array. Then the second level will need to append r2 to the array; that means that your recursive method will need a parameter that holds the array of integers found thus far. Each invocation of the method will get an array of "integers found so far", and will use that array with a new integer appended when it calls itself recursively. When you get to the bottom level, you'll have a complete array of integers and you can then test your Diophantine equation to do whatever else you'd like to do with the integers.

Hopefully that will get you started.

ajb
  • 31,309
  • 3
  • 58
  • 84