0

What I want is to find a distinct list of decimals that add to 1 given that I'm using three variables and each variable can increase by 0.05.

The table of values would look something like this:

Var1   Var2   Var3   Total
 0.0    0.05   0.95   1
 0.20   0.40   0.40   1
 0.15   0.75   0.10   1
 0.50   0.25   0.25   1
 0.80   0.20   0.00   1

And so on... I think you get the idea.

resonance
  • 1
  • 4

1 Answers1

0

The first thing to realize is that since they all sum to 1, var3 is a function of var2 + var1. So you really have two variables here.

You can brute force this by just looping var1 and var2 blindly and keeping the values:

List<int[]> sets = new ArrayList<>();

for(int var1 = 0; var1 <= 100; var1 += 5) {
    for(int var2 = 0; var2 <= 100; var2 += 5) {
        int var3 = 100 - (var1 + var2);
        if(var1 + var2 + var3 == 100 && (var1 != var2) && (var1 != var3) && (var2 != var3)) {
             int[] set = new int[] { var1, var2, var3 };
             sets.add(set);
        }
    }
}

Now this is riddled with inefficiencies that hopefully you can spot and optimize, but this should be an effective way to do it. Note that I used integers so we didn't end up with weird floating point rounding errors. Modify your output accordingly.

corsiKa
  • 81,495
  • 25
  • 153
  • 204