We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. All Contests
  2. ProjectEuler+
  3. Project Euler #173: Using up to one million tiles how many different "hollow" square laminae can be formed?
  4. Discussions

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

Problem
Submissions
Leaderboard
Discussions

    You are viewing a single comment's thread. Return to all comments →

  • trungdovan87
    5 years ago+ 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!!

    8|
    Permalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature