Nested for loops - how to ignore certain combinations?
<p>I'm doing some kind of brute force attack to solve a problem. Theoratically it is a working solution, it also really is, but it takes rather long.</p>

<p>I have 7 nested for loops within each other, but I only need the combinations of the 'for variables' in which none are repeated. So e.g. 1,2,3,4,5,6,7 is allowed, but 1,1,3,4,5,6,7 should be ignored. I'm currently using a function to check for duplicate items in an array:</p>

<p><a href="http://www.groggyjava.tv/freebies/duplicates.html" rel="nofollow">http://www.groggyjava.tv/freebies/duplicates.html</a></p>

<p>However I think I'd be better off if I could immediately ignore those combinations instead of using that function over and over for <em>every</em> single combination. Now I'm evaluating 14^7 = 105.413.504 combinations, but only 14 nPr 7 = 17.297.280 of them are necessary.</p>

<p>My code currently is (in which hgd is the duplicate test function, with returns true if no duplicates):</p>

<pre><code>for(var a=0;a&lt;14;a++) {
    for(var b=0;b&lt;14;b++) {if(hgd([a,b])) {
        for(var c=0;c&lt;14;c++) {if(hgd([a,b,c])) {
            for(var d=0;d&lt;14;d++) {if(hgd([a,b,c,d])) {
                for(var e=0;e&lt;14;e++) {if(hgd([a,b,c,d,e])) {
                    for(var f=0;f&lt;14;f++) {if(hgd([a,b,c,d,e,f])) {
                        for(var g=0;g&lt;14;g++) {if(hgd([a,b,c,d,e,f,g])) {
                            check([a,b,c,d,e,f,g]);
                        }}
                    }}
                }}
            }}
        }}
    }}
}
</code></pre>

<p>How could I make this faster, is there another solution instead of for loops perhaps?</p>

<p>Thanks.</p>
