当前位置:首页 » Project Euler » 详细页

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).

Solution

根据$n=d\times q+r$得到$r< d$。 不失一般性的假设$d< \sqrt{n}$,则对于$d$和$q$可以任意交换。所以: $$r< d< q$$ 则可以设: $$r=r,d=r\times c,q=r\times c^2$$ 一方面,$c$应当是有理数,否则$d,q$不是整数。则记$c=\frac{a}{b}$,且$a,b$互素。 另一方面,由于$r< d< q$可以得到$c>1$,所以$a>b$。 最后,考察$q$,注意到$b^2$必须整除$r$,否则$q$不是整数。 则$r=k\times b^2$ 于是: $$n=r^2\times c^3+r=k^2\times a^3\times b +k\times b^2$$ 循环枚举的边界是: $$2\leq a < 10000$$ $$1\leq b < a$$ $$1\leq k$$

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))
    					ans.add(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