Project Euler #173: Using up to one million tiles how many different "hollow" square laminae can be formed?

  • + 3 comments

    I got 100/100 point.

    My Solution: we need calculate how many pair (a, b) with: a^2 - b^2 <= n

    => (a - b)(a + b) <= n

    Let: a = b + k (with b > 0; k> 0; and k is even, means: k % 2 == 0)

    => k(2b + k) <= n

    => b <= (n/k - k)/2

    The result is sum of all b.

    We need 1 FOR-loop: from 1 to sqrt(n):

    long caculate(n, k) {
    	return (long) ((double) n / k - k) / 2;
    }
    // Main:
    sum = 0;
    for (int k = 2; k < sqrt(n); k += 2) {
       b = calculate(n, k);
       if (b > 0) 
       	sum += b;   
    }
    return sum;
    

    n,k,b.. is Long, not Integer!

    GOOD LUCK!!