For the first, you can break it into several cycles, for example change:
for(int i = 0; i < 1000; i++) for(int j = 0; j < 1000; j++) { for(int k = 0; k < 1000; k++) { if(k==i || k == j) continue;
in
for(int i = 0; i < 1000; i++) for(int j = 0; j < 1000; j++) { for(int k = 0; k < min(i, j); k++) { // other code } for(int k = min(i, j) + 1; k < max(i, j); k++) { // other code } for(int k = max(i, j) + 1; k < 1000; k++) { // other code } }
To remove the second, you can save the previous total and use it in the conditions of the for loop, that is:
int left_side = 1, right_side = 0; for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) { for(int k = 0; k < min(i, j) && left_side >= right_side; k++) { // other code (calculate a, b, c, d) left_side = a * a + b * b + c * c; right_side = d * d; } for(int k = min(i, j) + 1; k < max(i, j) && left_side >= right_side; k++) { // same as in previous loop } for(int k = max(i, j) + 1; k < N && left_side >= right_side; k++) { // same as in previous loop } }
Implementing min and max without branching can also be a daunting task. Perhaps this version is better:
int i, j, k, left_side = 1, right_side = 0; for(i = 0; i < N; i++) { // this loop covers the case where j < i for(j = 0; j < i; j++) { k = 0; for(; k < j && left_side >= right_side; k++) { // other code (calculate a, b, c, d) left_side = a * a + b * b + c * c; right_side = d * d; } k++; // skip k == j for(; k < i && left_side >= right_side; k++) { // same as in previous loop } k++; // skip k == i for(; k < N && left_side >= right_side; k++) { // same as in previous loop } } j++; // skip j == i // and now, j > i for(; j < N; j++) { k = 0; for(; k < i && left_side >= right_side; k++) { // other code (calculate a, b, c, d) left_side = a * a + b * b + c * c; right_side = d * d; } k++; // skip k == i for(; k < j && left_side >= right_side; k++) { // same as in previous loop } k++; // skip k == j for(; k < N && left_side >= right_side; k++) { // same as in previous loop } } }