### Right triangles with integer coordinates题号：91 难度： 25 中英对照

The points P (x1, y1) and Q (x2, y2) are plotted at integer co-ordinates and are joined to the origin, O(0,0), to form ΔOPQ.

There are exactly fourteen triangles containing a right angle that can be formed when each co-ordinate lies between 0 and 2 inclusive; that is,
0 ≤ x1, y1, x2, y2 ≤ 2.

Given that 0 ≤ x1, y1, x2, y2 ≤ 50, how many right triangles can be formed?

### Code

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public final class p91 {
private static final int MAX_N=50;
public static void main(String[] args){
long start=System.nanoTime();
long result = run();
long end=System.nanoTime();
System.out.println(result);
System.out.println( (end-start)/1000000 + "ms" );
}
public static long run(){
long count=0;
for(int x1=0;x1<=MAX_N;x1++){
for(int y1=0;y1<=MAX_N;y1++){
for(int x2=0;x2<=MAX_N;x2++){
for(int y2=0;y2<=MAX_N;y2++){
if(x1*y2>x2*y1&&isRight(x1,y1,x2,y2)){
count++;
}
}
}
}
}
return count;
}
public static boolean isRight(int x1,int y1,int x2,int y2){

int dx = x2 - x1;
int dy = y2 - y1;
return x1*x1 + y1*y1 + x2*x2 + y2*y2 == dx*dx + dy*dy
|| x1*x1 + y1*y1 + dx*dx + dy*dy == x2*x2 + y2*y2
|| x2*x2 + y2*y2 + dx*dx + dy*dy == x1*x1 + y1*y1;
}

}

14234
28ms