Pages

Tuesday, September 8, 2009

Euler Problem 9 solution

Time (s): ~0.001
package margusmartseppcode.From_1_to_9;

public class Problem_9 {
 static long getPythSum(long nr) {
  double a, b, c, sqn = Math.sqrt(nr);
  for (int i = 1; i < sqn; i += 2)
   for (int j = 2; j < sqn; j += 2) {
    a = Math.abs(j * j - i * i);
    b = 2 * i * j;
    c = i * i + j * j;
    if ((a + b + c) == nr)
     return (long) (a * b * c);
   }

  return -1;
 }

 public static void main(String[] args) {
  System.out.println(getPythSum(1000));
 }
}

No comments:

Post a Comment