I'm writing an algorithm which computes the solution for a pretty complex (for me) problem which I found online. I couldn't find a smart solution, so I decided to use the brute force.
as expected I get a really slow code. I would like to make it faster.
The core of the algorithm is this triple
for(int i = a; i < n; i++) for(int j = i+1; j < n; j++) for(int k = j+1; k < n; k++)
But I know it can be made faster.
Each loop performs a commutative check on the three elements indexed by the indexes
k. So if you do
check(i,j,k) you can skip
check(j,i,k) and so on.
I thought about this issue as a matrix (although this is 2D): matrix
Thank you very much