1

I am trying to find a combination of 6 over 85 possibilities. This would take o(n^6) where N is 85 and would be something like 351 billion calculations. Obviously this would take a very long time to do. There must be a way to do recursively, especially since there would be so many possible duplicate lists (since order doesn't matter) such as: 1, 2, 3, 4, 5, 6 == 1, 2, 3, 4, 6, 5 etc..

public void findOptimal6Combination() {
        for(int i = 1; i < list.size(); i++) {
            for(int j = 1; j < list.size(); j++) {
                if(i != j)
                for(int k = 1; k < list.size(); k++) {
                    if(i != k && j != k)
                    for(int l = 1; l < list.size(); l++) {
                        if(i != l && j != l && k != l)
                        for(int m = 1; m < list.size(); m++) {
                            if(i != m && j != m && k != m && l != m)
                            for(int n = 1; n < list.size(); n++) {
                                if(i != n && j != n && k != n && l != n && m != n) {
                                    current6.add(list.get(i));
                                    current6.add(list.get(j));
                                    current6.add(list.get(k));
                                    current6.add(list.get(l));
                                    current6.add(list.get(m));
                                    current6.add(list.get(n));
                                    
                                    performOperation(); //uses the current6 arraylist
                                }
                            }
                        }
                    }
                }
            }
        }
    }
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Vergyverg
  • 11
  • 1
  • 1
    If you have to test `O(N^6)` things (objects, combinations of objects, whatever), then your computation will be `O(N^6)` no matter how you iterate over those things. You could optimize the above code, but it will still be `O(N^6)` ... unless you change things so that you don't *need* to test all of the combinations. – Stephen C Aug 22 '21 at 01:18

0 Answers0