### Investigating progressive numbers, n, which are also square题号：141 难度： 60 中英对照

A positive integer, n, is divided by d and the quotient and remainder are q and r respectively. In addition d, q, and r are consecutive positive integer terms in a geometric sequence, but not necessarily in that order.

For example, 58 divided by 6 has quotient 9 and remainder 4. It can also be seen that 4, 6, 9 are consecutive terms in a geometric sequence (common ratio 3/2).
We will call such numbers, n, progressive.

Some progressive numbers, such as 9 and 10404 = 1022, happen to also be perfect squares.
The sum of all progressive perfect squares below one hundred thousand is 124657.

Find the sum of all progressive perfect squares below one trillion (1012).

### Code

public final class p141 {
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 String run(){
final long LIMIT=1000000000000L;
java.util.ArrayList<Long> ans=new java.util.ArrayList<Long>();
for(long a=2;a<10000;a++){
for(long b=1;b<a;b++){
if(a*a*a*b*b+b*b>=LIMIT) break;
if(gcd(a,b)!=1) continue;
for(long k=1;;k++){
long n=a*a*a*b*k*k+k*b*b;
if(n>=LIMIT) break;
long m=(long)(Math.sqrt(n)+1e-9);
if(m*m==n && !ans.contains(n))
}
}
}
long sum=0;
for(long x:ans) sum+=x;
return ""+sum;
}

// Returns the largest non-negative integer that divides both x and y.
public static long gcd(long x, long y) {
while (y != 0) {
long z = x % y;
x = y;
y = z;
}
return x;
}

}
878454337159
257ms