New answers tagged


You could use java 8's Streams, which will do short-circuiting under the hood for you. To get around some boxing/unboxing you can define this internal interface : private interface SolutionTest { boolean test(int a, int b); } With this in place you can introduce these helper methods : private boolean isPossibleSolution(int c, int d) { return ...


I'm not sure if this is what you mean, but you can do such a thing with a labeled break/continue ( main: for (int d = 0; d <= max; d++) { subMain: for (int c = 0; c < d; c++) { boolean possibleSolution = test1(c,d); if(!possibleSolution) continue main;...


Based on your question break; will NOT work for you. You want to skip all further steps in this cycle, do c++ and go for a new round. This is done using continue. Break will end inner cycle do d++ and start inner cycle from 0.


Measure first instead of making assumptions. The sequence of 30 tests "if (possibleSolution)" is something that a good compiler should be able to optimise away. At the first "if (possibleSolution)" a good compiler would generate code that doesn't only skip one call, but figures out immediately that the second, third, and so on if are also going to fail, so ...


Replace If(pass) pass=testN(c,d); With If(!testN(c,d)) {break;} This way you get to a new cycle right away.

Top 50 recent answers are included