Project Euler #9 implementation in Java Question:


  Special Pythagorean triplet, Euler Problem 9
  
  A Pythagorean triplet is a set of three natural numbers, \$a&lt;b&lt;c\$, for which, $$a^2+b^2=c^2$$ For example, \$3^2+4^2=9+16=25=5^2\$.  
  
  There exists exactly one Pythagorean triplet for which \$a+b+c=1000\$.
  
  Find the product \$abc\$.


I got the code to work using algebraic simplifications shown below.


  Given:
  
  \$num=1000\$
  
  \$a+b+c=1000\$
  
  \$a^2+b^2+c^2+2ab+2bc+2ca=num^2\$
  
  \$a^2+b^2=c^2\$


Thus:

$$2c^2+2ab+2bc+2ca=num^2$$
$$c(c+b)+a(b+c)=\frac{num^2}{2}=500num$$
$$(a+c)(b+c)=500num$$
$$(num-b)(num-a)=500num$$
$$num-b=\frac{500num}{num-a}$$
$$b=num-\frac{500num}{num-a}$$

So, I scan through \$a\$ and if I receive an integer value for \$b\$ we have found \$b\$, and thus \$c\$ can be found through the equation \$c=num-a-b\$.

The upper limit of \$a\$ is found by assuming that \$a\$ is the smallest among the three integers,  \$a\$, \$b\$, and \$c\$ such that \$a&lt;b&lt;c\$. Following this assumption, let's find the max condition by rewriting \$b\$ and \$c\$ in terms of \$a\$. So, \$b = a+n\$ and \$c = a+k\$ where \$n&lt;k\$, so that \$a&lt;b&lt;c\$ remains true.

Now:

$$a+b+c=num$$
$$a+(a+n)+(a+k)=num$$
$$3a+n+k=num$$
$$3a=num-(n+k)$$
$$a=\frac{num-(n + k)}{3}$$

Now, the max acceptable value of \$a\$ according to the equation could be when \$n=1\$, and \$k=2\$ since \$a&lt;b&lt;c\$. In other words, the difference between the values is 1.

Therefore we loop \$a\$ up to \$\frac{num}{3} - 1\$

My code manages a runtime of ~0.166526 ms:

public class Java {

public static void main(String[] args) {


    double num = 1000.0;
    double b = 0;

    for(int a = 2 ; a &lt;= (num/3 - 1); a++){
          b = ( num - ( 500*num / (num-a)));

          if(b == (int)b){
              System.out.println((int)(a*b*(num-a-b)));
              break;
          }

    }


}


Any suggestions for setting a lower limit for \$a\$ and probably speeding up the algorithm? The upper limit seems odd since I just used \$a+b+c=1000\$ when finding it instead of the Pythagorean Theorem.
There isn't much code, so that leaves little to be reviewed. I have not considered the mathematical side of this. This is also a code stub, so when reviewed out of context it is hard to do a good review. 


Comment it – As a code stub it is hard to understand what you are trying to achieve, and as such I would problably leave a comment stating what you actually wanted to achieve. Maybe just a reference to "Special Pythagorean triplet, Euler Problem 9", or similar. And possibly some of the justification of your formulas
Do standard indentation – The class and the method is at the same level, which is confusing. Do indent, but keep it a standard width, like 2 or 4 spaces. The same applies for how many lines you use between statements, ending braces, methods. Keep it clean(er)
Keep computation in loop conditions to a minimum – In your case it will most likely be optimised by the compiler, but having computational stuff in the loop conditions, like (num/3 - 1), should be avoided. Do the calculation once in front of the loop, and make it a simple comparision in the loop condition
Take care when using parenthesis – You have some unneccessary parenthesis, and some which is kind of strange, I would have written it: b = num - (500*num) / (num-a) which emphasizes that the denominator and the dividend are calculated before the actual division
Comparing doubles and int is dangerous – In most languages a double variable is usually not checked directly for equality as the imprecision in the storage format could leave to interesting effects. See Spot the defect: rounding, part two. However this might not apply to you as you are mainly interested in a whole number and the decimal parts of it...
Variable naming – Nothing indicates that num is a constant, neither in the naming nor in the declaration (i.e. you could use final). And what does num mean? Is it a number, is it a constant?
Consider using a function – In the problem description it indicates that for other numbers you could find other pythagorean triples, which could be extended into making a method like findPythagoreanTriplet(number) which could either return the first triplet, or a list of triplets. Using a function would allow for extending the code stub into maybe doing something more useful like finding other triplets matching some criteria, or similar
Computation vs presentation – An alternative view on creating a function, is that you could then split the computation from the presentation, which usually is a good thing to do. This also according to the principle of single concern for a method
Jammed if conditions – The last few days I've seen a lot of if(someCondition){, which in my view would like nicer as if (someCondition) {. Having a little space here and there increases readability, asOpposedToNotHavingSpaces...


Do be aware that this code review is making some assumptions, and might be a little overkill for something which you might just have thrown together to solve an immediate problem you had. However, becoming a robust and good programmer, starts by doing it right from the smallest through to the largest projects.