Pages

Wednesday, September 9, 2009

Euler Problem 39 solution

Time (s): ~0.009
package margusmartseppcode.From_30_to_39;

public class Problem_39 {
 // Pythagorean theorem for right angle triangle:
 // a*a+b*b==c*c where c=(p-a-b)
 // same as (MOD(p(p–2a),2(p-a))== 0)
 public static void main(String[] args) {
  int max = 0, limit = 1000, imax = 0;

  for (int i = 2; i <= limit; i += 2)
   for (int j = 2, t = 0; j <= i / 4; j++) {
    if (i * (i - 2 * j) % (2 * (i - j)) == 0)
     t++;
    if (t > imax) {
     imax = t;
     max = i;
    }
   }

  System.out.println(max);
 }
}

No comments:

Post a Comment