While another commenter pointed out that this can be accomplished with a 3-deep nested for
loop, there might be a more elegant solution, perhaps using a library like combinatoricslib. You could then do this:
ICombinatoricsVector<BigDecimal> originalVector =
Factory.createVector(new BigDecimal[] {
new BigDecimal("0.0"),
new BigDecimal("0.1"),
new BigDecimal("0.2"),
new BigDecimal("0.3"),
new BigDecimal("0.4"),
new BigDecimal("0.5"),
new BigDecimal("0.6"),
new BigDecimal("0.7"),
new BigDecimal("0.8"),
new BigDecimal("0.9")
});
Generator<BigDecimal> gen = Factory.createPermutationWithRepetitionGenerator(originalVector, 3);
for (ICombinatoricsVector<BigDecimal> perm : gen) {
// values are in perm.getValue(0), perm.getValue(1), and perm.getValue(2)
}
Afterward, you could iterate through each permutation and check the sum with a for
loop of depth 1.
Note that I have replaced float
/double
with BigDecimal
in order to address floating point precision/representation issues. Further note that I have used the String
constructor of BigDecimal
to avoid an insipid form of the same precision/representation issues.