### Almost equilateral triangles题号：94 难度： 35 中英对照

It is easily proved that no equilateral triangle exists with integral length sides and integral area. However, the almost equilateral triangle 5-5-6 has an area of 12 square units.

We shall define an almost equilateral triangle to be a triangle for which two sides are equal and the third differs by no more than one unit.

Find the sum of the perimeters of all almost equilateral triangles with integral side lengths and area and whose perimeters do not exceed one billion (1,000,000,000).

### Code

public final class p94 {
public static void main(String[] args) {
long start=System.nanoTime();
String result = run();
long end=System.nanoTime();
System.out.println(result);
System.out.println( (end-start)/1000000 + "ms" );
}
static public int LIMIT=1000000000;
static public String run(){
long ans=0;
//a a a+1
for(long x=2,y=1;;){
if((2*x+1)%3==0){
long a=(2*x+1)/3;
long C=3*a+1;
if(C>LIMIT) break;
ans+=C;
}
//更新xy
long nx=2*x+3*y;
long ny=x+2*y;
x=nx;y=ny;
}
//a a a-1
for(long x=2,y=1;;){
if((2*x-1)%3==0){
long a=(2*x-1)/3;
if(a-1>0){
long C=3*a-1;
if(C>LIMIT) break;
ans+=C;
}
}
//更新xy
long nx=2*x+3*y;
long ny=x+2*y;
x=nx;y=ny;
}
return Long.toString(ans);
}
}
518408346
0ms